Skip to content

Commit e92ed0b

Browse files
authored
fix: use runtime.getURL instead of extension.getURL (#20)
1 parent ee2436c commit e92ed0b

File tree

2 files changed

+10
-17
lines changed

2 files changed

+10
-17
lines changed

src/background.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { IS_FIREFOX, isSupportExecutionVersion } from './utils'
1+
import { IS_CHROME, IS_FIREFOX, isSupportExecutionVersion } from './utils'
22
const browser = require('webextension-polyfill')
33

44
const tabsStorage = {}
55

6-
if (!IS_FIREFOX && isSupportExecutionVersion) {
6+
if (IS_CHROME && isSupportExecutionVersion) {
77
/**
88
* equivalent logic for Firefox is in content.js
99
* Manifest V3 method of injecting content scripts (not yet supported in Firefox)

src/content.js

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,22 @@
1-
import { IS_FIREFOX, isSupportExecutionVersion } from './utils'
1+
import { IS_CHROME, isSupportExecutionVersion } from './utils'
22
const browser = require('webextension-polyfill')
33

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) {
136
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)
1610
document.documentElement.appendChild(script)
1711
script.parentNode.removeChild(script)
1812
}
1913

2014
// 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'))
2317
}
2418

2519
// content script logic
26-
2720
browser.runtime.onMessage.addListener(messageFromBackground)
2821

2922
function messageFromBackground (message) {

0 commit comments

Comments
 (0)