1

I want to change a website by adding some ReactJS elements onto it. I'm running a content script where I add the react script as well as index.html and a console.js, the later two works fine.

I'm adding both console and react with the same method with the only difference being that I declare the type of react to be text/jsx.

I have tried adding the JSXTransformer.js and react.js both via the html document and:

var a = document.createElement('script');
a.src = chrome.extension.getURL('build/JSXTransformer.js');
a.onload = function() {
    this.parentNode.removeChild(this);
};
(document.head||document.documentElement).appendChild(a);

But I didn't notice any difference with either.

Some code:

contentScript.js

// Loads the html document onto the page.
var xmlHttp = null;

xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", chrome.extension.getURL ("index.html"), false );
xmlHttp.send( null );

var inject  = document.createElement('div');

inject.innerHTML = xmlHttp.responseText


document.body.insertBefore (inject, document.body.firstChild);


// Adds the react.jsx
var jsx = document.createElement('script');

jsx.src = chrome.extension.getURL('React.jsx');

// Adding type="text/jsx"
jsx.type="text/jsx";
jsx.onload = function() {
    this.parentNode.removeChild(this);
};
(document.head||document.documentElement).appendChild(jsx);


// Adds the cosole.js
var js= document.createElement('script');

js.src = chrome.extension.getURL('console.js');

js.onload = function() {
    this.parentNode.removeChild(this);
};
(document.head||document.documentElement).appendChild(js);

manifest.json

{
"name":"Test react",
"description":"",
"version":"1",
"web_accessible_resources": ["index.html","React.jsx","console.js","build/react.js","build/JSXTransformer.js"],
"manifest_version":2,

"content_scripts": [
    {
      "matches": ["http://stackoverflow.com/*"],
      "js": ["contentScript.js"]


    }
  ]

}

React.jsx

/*** @jsx Reaxt.DOM */
var Start = React.createClass({
  render: function() {
    return (
      <div>

        React is working fine
      </div>
    );
  }
});

React.render(<Start/>, document.getElementById("test"));

Index.html:

<!DOCTYPE html>
<html>
<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.3/react.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.3/JSXTransformer.js"></script>
  <meta charset="UTF-8">


</head>
<body>


  <h1> Hallo World</h1>
  <div  id="test"></div>


</body>
</html>

console.js

console.log("Normal JS is working fine");
6
  • 1. Your index.html tries to load react.js and JSXTransformer.js from an external CDN, this will work only on sites that allow this particular CDN. 2. If reactjs posts/pushes some data to its API server you'll need a CSP rule. Commented Sep 23, 2015 at 9:11
  • Thank you for the answer, do you know if it is possible to load the script some other way even if the site don't allow the CDN, for example via javascript? Commented Sep 23, 2015 at 13:19
  • Simply load the scripts from your extension package. You're manually assigning the innerHTML anyway so you can manually insert two <script> elements with src set to chrome.extension.getURL("script1.js") for example. Moreover these scripts are already listed in "web_accessible_resources", so you should actually use them. Just don't forget to investigate the CSP issue I mentioned. Commented Sep 23, 2015 at 13:24
  • I don't seem to have any success with the changes I have tried out, would you care to give an example how one might make a CSP rule so the script would for example work on this site? Commented Sep 24, 2015 at 8:48
  • CSP rule might be not needed if the script doesn't make any requests, you can see it in the devtools Network panel and I think the console log would contain an error. Commented Sep 24, 2015 at 8:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.