Last active
April 12, 2021 13:11
-
-
Save FlyInk13/8761273d90df753e0a5d361e8d885bc9 to your computer and use it in GitHub Desktop.
Пример пользовательского скрипта с React
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name React Userscript | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description shows how to use babel compiler | |
// @author You | |
// @require https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.18.2/babel.js | |
// @require https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/6.16.0/polyfill.js | |
// @require https://unpkg.com/react@16/umd/react.development.js | |
// @require https://unpkg.com/react-dom@16/umd/react-dom.development.js | |
// @require https://ifx.su/~va | |
// @match https://vk.com/* | |
// @noframes | |
// ==/UserScript== | |
var inline_src = (<><![CDATA[ | |
class App extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = {}; | |
} | |
static searchElement(elements) { | |
elements.forEach((el) => { | |
if (!el || !el.querySelector) { | |
return; | |
} | |
const action_search = el.querySelector('.im-action_search:not(.im-action_stat)'); | |
if (!action_search) { | |
return; | |
} | |
const action_stat = action_search.cloneNode(); | |
action_stat.textContent = 'Test menu'; | |
action_stat.classNames += ' im-action_stat'; | |
action_stat.onclick = App.onMenuClick; | |
action_search.parentElement.insertBefore(action_stat, action_search); | |
}) | |
} | |
static initSearch() { | |
App.searchElement([document.body]); | |
new MutationObserver((mutations) => { | |
App.searchElement(mutations); | |
}).observe(document.body, { | |
subtree: true, | |
childList: true, | |
}); | |
} | |
static onMenuClick() { | |
alert('OK'); | |
} | |
render() { | |
return ( | |
<div> Hello React UserScript! </div> | |
); | |
} | |
} | |
App.initSearch(); | |
]]></>).toString(); | |
var source = Babel.transform(inline_src, { presets: [ "es2015", "es2016", "react" ] }); | |
eval(source.code); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment