|
1 | | -import { IS_FIREFOX, isSupportExecutionVersion } from './utils' |
| 1 | +import { IS_CHROME, isSupportExecutionVersion } from './utils' |
2 | 2 | const browser = require('webextension-polyfill') |
3 | 3 |
|
4 | | -// injecting the script, inspire by react-devtools-extensions |
5 | | -function injectScriptSync (src) { |
6 | | - let code = '' |
7 | | - const request = new XMLHttpRequest() |
8 | | - request.addEventListener('load', function () { |
9 | | - code = this.responseText |
10 | | - }) |
11 | | - request.open('GET', src, false) |
12 | | - request.send() |
| 4 | +// injecting the script |
| 5 | +function injectScript (src) { |
13 | 6 | const script = document.createElement('script') |
14 | | - script.textContent = code |
15 | | - // This script runs before the <head> element is created, so we add the script to <html> instead. |
| 7 | + script.setAttribute('defer', 'defer') |
| 8 | + script.setAttribute('type', 'text/javascript') |
| 9 | + script.setAttribute('src', src) |
16 | 10 | document.documentElement.appendChild(script) |
17 | 11 | script.parentNode.removeChild(script) |
18 | 12 | } |
19 | 13 |
|
20 | 14 | // equivalent logic for other browser is in background.js |
21 | | -if (IS_FIREFOX || !isSupportExecutionVersion) { |
22 | | - injectScriptSync(browser.extension.getURL('injected.js')) |
| 15 | +if (!IS_CHROME || !isSupportExecutionVersion) { |
| 16 | + injectScript(browser.runtime.getURL('injected.js')) |
23 | 17 | } |
24 | 18 |
|
25 | 19 | // content script logic |
26 | | - |
27 | 20 | browser.runtime.onMessage.addListener(messageFromBackground) |
28 | 21 |
|
29 | 22 | function messageFromBackground (message) { |
|
0 commit comments