Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using react js in Chrome extension

I am following this tutorial to build a new tab chrome extension .. https://facebook.github.io/react/docs/tutorial.html

but when I attach

<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.min.js"></script>

for rendering JSX using babel as suggested by the tutorial I get this error from the browser console.

"browser.min.js:4 Uncaught EvalError: Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: "script-src 'self' blob: filesystem: chrome-extension-resource:"." screenshot of the error

I know this is violating CSP directive but then again how can I use the latest reactJS using babel?

like image 465
Ahsan Rocky Avatar asked Feb 05 '16 16:02

Ahsan Rocky


People also ask

Can I use React in Chrome extension?

React Developer Tools is a Chrome DevTools extension for the open-source React JavaScript library. It allows you to inspect the React component hierarchies in the Chrome Developer Tools. You will get two new tabs in your Chrome DevTools: "⚛️ Components" and "⚛️ Profiler".

How do I create a Chrome extension With React?

Open Your Extension in Chrome In the top-right corner, turn on developer mode. This will then render two buttons in the top-left corner. Load the unpacked extension and the packed extension. Click on “Load unpacked extension,” and select your build folder.


3 Answers

In your manifest.json file, you can try setting

"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"

I got the answer from here. This got rid of the error for me, but I'm not knowledgeable enough about it to know about other implications, sorry.

like image 82
Villager Avatar answered Oct 19 '22 19:10

Villager


Adding INLINE_RUNTIME_CHUNK=false in my .env file totally fixed it for me.

like image 45
Scaraux Avatar answered Oct 19 '22 20:10

Scaraux


For anyone coming here, Bruno's answer is more correct. Use the "production" mode. E.g. in Angular

ng build --prod

like image 2
Tobin Avatar answered Oct 19 '22 21:10

Tobin