Skip to content

Commit c19a4d3

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

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

shells/chrome/src/detector.js

Lines changed: 12 additions & 3 deletions
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) {
40+
const source = ';(' + fn.toString() + ')(window)'
3941
const script = document.createElement('script')
40-
script.textContent = ';(' + fn.toString() + ')(window)'
41-
document.documentElement.appendChild(script)
42-
script.parentNode.removeChild(script)
42+
43+
if (isFirefox) {
44+
/* eslint-disable no-eval */
45+
window.eval(source)
46+
/* eslint-enable no-eval */
47+
} else {
48+
script.textContent = source
49+
document.documentElement.appendChild(script)
50+
script.parentNode.removeChild(script)
51+
}
4352
}

shells/chrome/src/hook.js

Lines changed: 13 additions & 4 deletions
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)
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

Lines changed: 1 addition & 0 deletions
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)