Skip to content
This repository was archived by the owner on Apr 30, 2026. It is now read-only.

Commit 559e8bf

Browse files
committed
Use Promise to inject scripts to avoid inject order issue
Fix #37
1 parent 8bb5ba4 commit 559e8bf

1 file changed

Lines changed: 36 additions & 25 deletions

File tree

src/js/run.js

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
import 'chrome-extension-async'
22
import { getHosts, getHostKey, findMatchedHosts } from 'libs'
33

4-
const injectedSet = new Set()
5-
64
const baseURL = chrome.runtime.getURL('base.js')
75

8-
const injectScript = (src, where) => {
9-
if (injectedSet.has(src)) {
10-
return
11-
}
12-
injectedSet.add(src)
13-
if (!injectedSet.has(baseURL)) {
14-
injectScript(baseURL)
15-
}
16-
const elm = document.createElement('script')
17-
elm.src = src
18-
document[where || 'head'].appendChild(elm)
6+
const catchErr = (e) => {
7+
console.error('Failed to inject scripts:', e)
8+
}
9+
10+
const injectScriptPromise = (src, where) => {
11+
return new Promise((resolve, reject) => {
12+
const elm = document.createElement('script')
13+
document[where || 'head'].appendChild(elm)
14+
elm.onload = () => {
15+
resolve(`Inject ${src} complete!`)
16+
}
17+
elm.src = src
18+
})
1919
}
2020

21-
const executeScript = (customjs) => {
21+
const extractScripts = (customjs, injections) => {
2222
if (!customjs) {
2323
return
2424
}
@@ -28,34 +28,45 @@ const executeScript = (customjs) => {
2828
}
2929

3030
// base.js to provide useful functions
31+
injections.add(baseURL)
3132

3233
// Predefined include
3334
if (include) {
34-
injectScript('https://ajax.googleapis.com/ajax/libs' + include)
35+
injections.add('https://ajax.googleapis.com/ajax/libs' + include)
3536
}
3637

3738
// Extra include
3839
(extra || '').split(';').map(x => x.trim()).forEach((line) => {
3940
if (line && line.substr(0, 1) !== '#') {
40-
injectScript(line)
41+
injections.add(line)
4142
}
4243
})
4344

44-
// User defined Script
45-
if (source) {
46-
setTimeout(function () {
47-
injectScript(source, 'body')
48-
}, 250)
49-
}
45+
return source
5046
}
5147

5248
const loadScripts = async (location) => {
5349
const hosts = await getHosts()
5450
const matchedHosts = findMatchedHosts(hosts, location)
55-
matchedHosts.forEach((host) => {
56-
const hostKey = getHostKey(host)
57-
chrome.storage.sync.get(hostKey, (obj) => executeScript(obj[hostKey]))
51+
const injections = new Set()
52+
Promise.all(matchedHosts.map(
53+
async (host) => {
54+
const hostKey = getHostKey(host)
55+
const obj = await chrome.storage.sync.get(hostKey)
56+
return extractScripts(obj[hostKey], injections)
57+
}
58+
))
59+
.then((values) => {
60+
return Promise.all([ ...injections ].map(
61+
(src) => injectScriptPromise(src)
62+
))
63+
.then(() => values)
64+
.catch(catchErr)
5865
})
66+
.then((values) => values.map(
67+
(src) => injectScriptPromise(src, 'body')
68+
))
69+
.catch(catchErr)
5970
}
6071

6172
loadScripts(window.location)

0 commit comments

Comments
 (0)