Skip to content

Commit d289cea

Browse files
committed
Don't break when CSP is on in Firefox (#616)
1 parent bd8f4ad commit d289cea

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

shells/chrome/src/detector.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { installToast } from 'src/backend/toast'
2+
import { isFirefox } from 'src/devtools/env'
23

34
window.addEventListener('message', e => {
45
if (e.source === window && e.data.vueDetected) {
@@ -36,8 +37,16 @@ if (document instanceof HTMLDocument) {
3637
}
3738

3839
function installScript (fn) {
39-
const script = document.createElement('script')
40-
script.textContent = ';(' + fn.toString() + ')(window)'
41-
document.documentElement.appendChild(script)
42-
script.parentNode.removeChild(script)
40+
const source = ';(' + fn.toString() + ')(window)'
41+
42+
if (isFirefox) {
43+
/* eslint-disable no-eval */
44+
window.eval(source) // in Firefox, this evaluates on the content window
45+
/* eslint-enable no-eval */
46+
} else {
47+
const script = document.createElement('script')
48+
script.textContent = source
49+
document.documentElement.appendChild(script)
50+
script.parentNode.removeChild(script)
51+
}
4352
}

shells/chrome/src/hook.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
// This script is injected into every page.
22
import { installHook } from 'src/backend/hook'
3+
import { isFirefox } from 'src/devtools/env'
34

45
// inject the hook
56
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)
7+
const source = ';(' + installHook.toString() + ')(window)'
8+
9+
if (isFirefox) {
10+
/* eslint-disable no-eval */
11+
window.eval(source) // in Firefox, this evaluates on the content window
12+
/* eslint-enable no-eval */
13+
} else {
14+
const script = document.createElement('script')
15+
script.textContent = source
16+
document.documentElement.appendChild(script)
17+
script.parentNode.removeChild(script)
18+
}
1019
}

src/devtools/env.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export const isChrome = typeof chrome !== 'undefined' && !!chrome.devtools
2+
export const isFirefox = navigator.userAgent.startsWith('Mozilla')
23
export const isWindows = navigator.platform.indexOf('Win') === 0
34
export const isMac = navigator.platform === 'MacIntel'
45
export const isLinux = navigator.platform.indexOf('Linux') === 0

0 commit comments

Comments
 (0)