I am having trouble to inject react element via chrome extension. Although it may be duplicated, i have already read some questions in terms of injecting react element via chrome extension and tried a various methods to inject react element. but it is still not working.
Following my code
manifest.json
{
"manifest_version": 2,
"name": "__MSG_name__",
"version": "1.0.0",
"description": "__MSG_description__",
"author": "Seokjun Hong",
"default_locale": "en",
"browser_action": {
"default_title": "__MSG_name__"
},
"web_accessible_resources": [
"js/jquery-2.2.3.min.js",
"html/emoji_box.html",
"js/vendor/browser.min.js",
"js/vendor/react.min.js",
"js/vendor/react-dom.min.js",
"js/vendor/react-with-addons.min.js",
"js/test.js"
],
"content_scripts": [{
"matches": [
"https://github.com/*",
"https://gist.github.com/*"
],
"js": [
"js/jquery-2.2.3.min.js",
"js/content_script.js",
"js/semantic.min.js",
"js/test.js"
],
"css": [
"css/semantic.min.css"
],
"run_at": "document_idle",
"all_frames": false
}]
}
test.js (react element)
/** @jsx React.DOM */
var Counter = React.createClass({
getInitialState: function () {
return { count: 0 };
},
handleClick: function () {
this.setState({
count: this.state.count + 1,
});
},
render: function () {
return (
<button onClick={this.handleClick}>
Click me! Number of clicks: {this.state.count}
</button>
);
}
});
ReactDOM.render(
<Counter />,
document.getElementsByClassName('timeline-comment')[0]
);
content_script.js
(function() {
//append react script, following trial
})();
I have tried to inject react script into content.
TRIAL1: Injecting script tag using createElement
// Adds the react.jsx
var jsx = document.createElement('script');
jsx.src = chrome.extension.getURL('js/test.js');
jsx.type="text/babel";
jsx.onload = function() {
this.parentNode.removeChild(this);
};
(document.head||document.documentElement).appendChild(jsx);
I also tried following code
$(document.head).append('<script src="' + chrome.extension.getURL('js/test.js') + '" type="text/babel"></script>')
TRIAL2: Loading script using getScript of jQuery
$.getScript(chrome.extension.getURL('js/test.js'), function(){
});
In this case, I also tried to load all script to execute react code, e.g., browser.min.js
, react.min.js
, react-dom.min.js
.
Various trial returns errors, e.g., Unexpected Token '<'
or script not loading.
Everything is not working. i don't know how to inject react element.