Felix F. Felix F. - 1 year ago 83
Javascript Question

jQuery in Chrome Extension - Refused Inline Script eventhough Included properly

I'm trying to implement jQuery in my Chrome Extension in order to include the firebase.js in my background.js (which seems already weird), however chrome refuses to execute code from inside the jquery.js.


$.getScript('firebase.js', function()
// code depending on firebase.js, jquery is just needed here to include firebase.js


<script src="jquery-3.0.0.min.js"></script>
<script src="firebase.js"></script>
<script src="popup.js"></script>
<script src="background.js"></script>
//stuff happens


"background": {
"scripts": ["background.js"],
"persistent": true

So for me it looks like it is included properly, however I am getting this, which is very weird, since it is inside the jquery library:

jquery-3.0.0.min.js:2 Refused to execute inline script because it violates the following Content Security Policy

Isn't there another method on how to include Firebase.js in the background.js? It makes no sense that it doesn't work there, it is included in my popup.html.

Answer Source

The error comes from

$.getScript('firebase.js', function() { ...

jQuery's getScript will fetch the code in the file and then create a <script> element with the code as its text. That would be an inline script that is forbidden by the Content Security Policy.

If you want to include a file just before your code in background.js you can simply put:

"scripts": ["firebase.js", "background.js"],

in your manifest.json.