Skip to content

Commit 874b22b

Browse files
dr-itzyyx990803
authored andcommitted
Fix inject problems (#385)
* inject: prevent global variable 'script' Once the Vue panel is open, a variable window.script appears with the injected script tag. Fix it by wrapping it in an anonymous function. * Only inject script if document is a HTMLDocument Fixes #356
1 parent 5d72aea commit 874b22b

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

shells/chrome/src/detector.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ function detect (win) {
2828
}
2929

3030
// inject the hook
31-
const script = document.createElement('script')
32-
script.textContent = ';(' + detect.toString() + ')(window)'
33-
document.documentElement.appendChild(script)
34-
script.parentNode.removeChild(script)
31+
if (document instanceof HTMLDocument) {
32+
const script = document.createElement('script')
33+
script.textContent = ';(' + detect.toString() + ')(window)'
34+
document.documentElement.appendChild(script)
35+
script.parentNode.removeChild(script)
36+
}

shells/chrome/src/devtools.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ initDevTools({
5959

6060
function injectScript (scriptName, cb) {
6161
const src = `
62-
var script = document.constructor.prototype.createElement.call(document, 'script');
63-
script.src = "${scriptName}";
64-
document.documentElement.appendChild(script);
65-
script.parentNode.removeChild(script);
62+
(function() {
63+
var script = document.constructor.prototype.createElement.call(document, 'script');
64+
script.src = "${scriptName}";
65+
document.documentElement.appendChild(script);
66+
script.parentNode.removeChild(script);
67+
})()
6668
`
6769
chrome.devtools.inspectedWindow.eval(src, function (res, err) {
6870
if (err) {

shells/chrome/src/hook.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
import { installHook } from 'src/backend/hook'
33

44
// inject the hook
5-
const script = document.createElement('script')
6-
script.textContent = ';(' + installHook.toString() + ')(window)'
7-
document.documentElement.appendChild(script)
8-
script.parentNode.removeChild(script)
5+
if (document instanceof HTMLDocument) {
6+
const script = document.createElement('script')
7+
script.textContent = ';(' + installHook.toString() + ')(window)'
8+
document.documentElement.appendChild(script)
9+
script.parentNode.removeChild(script)
10+
}

0 commit comments

Comments
 (0)