Skip to content

Commit 6ea0abf

Browse files
chearonAkryum
authored andcommitted
fix: Don't break when CSP is on in Firefox (#616) (#621)
* Don't break when CSP is on in Firefox (#616) * fix: eslint disable comments * feat: $isFirefox
1 parent bafea44 commit 6ea0abf

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

Diff for: shells/chrome/src/detector.js

+12-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,15 @@ 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-next-line no-eval
44+
window.eval(source) // in Firefox, this evaluates on the content window
45+
} else {
46+
const script = document.createElement('script')
47+
script.textContent = source
48+
document.documentElement.appendChild(script)
49+
script.parentNode.removeChild(script)
50+
}
4351
}

Diff for: shells/chrome/src/hook.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
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-next-line no-eval
11+
window.eval(source) // in Firefox, this evaluates on the content window
12+
} else {
13+
const script = document.createElement('script')
14+
script.textContent = source
15+
document.documentElement.appendChild(script)
16+
script.parentNode.removeChild(script)
17+
}
1018
}

Diff for: src/devtools/env.js

+2
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.indexOf('Firefox') > -1
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
@@ -16,6 +17,7 @@ export function initEnv (Vue) {
1617

1718
Object.defineProperties(Vue.prototype, {
1819
'$isChrome': { get: () => isChrome },
20+
'$isFirefox': { get: () => isFirefox },
1921
'$isWindows': { get: () => isWindows },
2022
'$isMac': { get: () => isMac },
2123
'$isLinux': { get: () => isLinux },

0 commit comments

Comments
 (0)