# Chrome is undefined when injecting script tags from extension

**URL:** <https://discourse.mozilla.org/t/chrome-is-undefined-when-injecting-script-tags-from-extension/37945>\
**Category:** Development\
**Created:** [April 8, 2019, 8:44pm UTC](https://discourse.mozilla.org/t/chrome-is-undefined-when-injecting-script-tags-from-extension/37945 "2019-04-08T20:44:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ak1](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@ak1](https://discourse.mozilla.org/u/ak1)\
**Post date:** [April 8, 2019, 8:44pm UTC](https://discourse.mozilla.org/t/chrome-is-undefined-when-injecting-script-tags-from-extension/37945/1 "2019-04-08T20:44:27Z")

</div>

In Chrome, I can load my standard create-react-app javascript files and the chrome variable is still set. However, in Firefox it seems to be undefined in the scripts that are loaded:

I have a content-script that just loads other content scripts dynamically:

```
function addToHead(s, toload) {
    s.src = chrome.extension.getURL(toload);
    s.href = chrome.extension.getURL(toload);
    (document.head || document.documentElement).appendChild(s);
}
// React app..
var toload = ["chunk.js", "runtime.js", "main.js"];
toload.forEach(function (toload) {
    console.log("Loading js: " + toload);
    var s = document.createElement('script');
    addToHead(s, toload);
})
```

---

<div class="post-metadata">

**Author:** ![sylvaing](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/sylvaing/32/19043_2.png) [@sylvaing](https://discourse.mozilla.org/u/sylvaing)\
**Post date:** [April 8, 2019, 8:57pm UTC](https://discourse.mozilla.org/t/chrome-is-undefined-when-injecting-script-tags-from-extension/37945/2 "2019-04-08T20:57:34Z")

</div>

In Firefox, you can use the keyword browser instead of chrome.

> **[Chrome incompatibilities - Mozilla | MDN](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Chrome_incompatibilities)**
>
> The WebExtension APIs aim to provide compatibility across all the main browsers, so extensions should run on any browser with minimal changes.

---

<div class="post-metadata">

**Author:** ![ak1](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@ak1](https://discourse.mozilla.org/u/ak1)\
**Post date:** [April 8, 2019, 9:17pm UTC](https://discourse.mozilla.org/t/chrome-is-undefined-when-injecting-script-tags-from-extension/37945/3 "2019-04-08T21:17:53Z")

</div>

`chrome.extension.getURL` works in the above code, so it is not that.

The issue seems to be that the “extension” context is lost when I inject a script like `chunk.js` in Firebox, but not in Chrome.

---

<div class="post-metadata">

**Author:** ![jscher2000](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/jscher2000/32/28202_2.png) [@jscher2000](https://discourse.mozilla.org/u/jscher2000)\
**Post date:** [April 8, 2019, 10:23pm UTC](https://discourse.mozilla.org/t/chrome-is-undefined-when-injecting-script-tags-from-extension/37945/5 "2019-04-08T22:23:32Z")

</div>

> [@ak1](#):
>
> `chrome.extension.getURL` works in the above code, so it is not that.
> 
> The issue seems to be that the “extension” context is lost when I inject a script like `chunk.js` in Firebox, but not in Chrome.

You could try the runtime API to see whether it works any better:

> **[runtime.getURL() - Mozilla | MDN](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/getURL)**
>
> Given a relative path from the manifest.json to a resource packaged with the extension, return a fully-qualified URL.

[https://developer.chrome.com/extensions/runtime#method-getURL](https://developer.chrome.com/extensions/runtime#method-getURL)

* * *

Or are you saying that the content scripts are added, and the problem is using chrome.something in the added script?

Does it work any better using executeScript()?

> **[tabs.executeScript() - Mozilla | MDN](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs/executeScript)**
>
> Injects JavaScript code into a page.
