Skip to content

Commit 4934fd1

Browse files
committed
feat: inject script
1 parent 840aedb commit 4934fd1

File tree

6 files changed

+74
-4
lines changed

6 files changed

+74
-4
lines changed

packages/webext/app/pages/panel.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
<title>Vite DevTools Panel</title>
55
</head>
66
<body>
7-
<script src="../../dist/panel.mjs" type="module"></script>
7+
<script src="../../dist/devtools-panel.mjs" type="module"></script>
88
</body>
99
</html>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
function injectScript(scriptName: string, cb: () => void) {
2+
const src = `
3+
(function() {
4+
var script = document.constructor.prototype.createElement.call(document, 'script');
5+
script.src = "${scriptName}";
6+
script.type = "module";
7+
document.documentElement.appendChild(script);
8+
script.parentNode.removeChild(script);
9+
})()
10+
`
11+
let timeoutId: number = null!
12+
function inspect() {
13+
clearTimeout(timeoutId)
14+
chrome.devtools.inspectedWindow.eval(src, (res, err) => {
15+
if (err) {
16+
timeoutId = window.setTimeout(() => {
17+
inspect()
18+
}, 100)
19+
return
20+
}
21+
22+
cb()
23+
})
24+
}
25+
inspect()
26+
}
27+
28+
function init() {
29+
// inject script to window
30+
injectScript(chrome.runtime.getURL('dist/inject.mjs'), async () => {
31+
})
32+
33+
chrome.devtools.network.onNavigated.addListener(() => {
34+
injectScript(chrome.runtime.getURL('dist/inject.mjs'), () => {
35+
})
36+
})
37+
}
38+
39+
init()
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
(async () => {
22
// eslint-disable-next-line no-undef
33
const script = chrome.runtime.getURL('dist/content-scripts.mjs')
4-
console.log('scripts', script)
54
await import(script)
65
})()

packages/webext/app/scripts/inject.ts

Whitespace-only changes.

packages/webext/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@
2020
],
2121
"sideEffects": false,
2222
"scripts": {
23-
"build:extension": "tsdown",
23+
"build": "tsdown",
2424
"watch": "tsdown --watch",
25-
"prepack": "pnpm build:extension"
25+
"prepack": "pnpm build"
2626
},
2727
"dependencies": {
2828
"webext-bridge": "catalog:deps"
2929
},
3030
"devDependencies": {
31+
"@types/chrome": "catalog:types",
3132
"tsdown": "catalog:build",
3233
"tsx": "catalog:build"
3334
}

pnpm-lock.yaml

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)