-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
detector.js
99 lines (84 loc) · 2.34 KB
/
detector.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { installToast } from '@back/toast'
import { isFirefox } from '@vue-devtools/shared-utils'
window.addEventListener('message', e => {
if (e.source === window && e.data.vueDetected) {
chrome.runtime.sendMessage(e.data)
}
})
function detect (win) {
let delay = 1000
let detectRemainingTries = 10
function runDetect () {
// Method 1: Check Nuxt
const nuxtDetected = !!(window.__NUXT__ || window.$nuxt)
if (nuxtDetected) {
let Vue
if (window.$nuxt) {
Vue = window.$nuxt.$root && window.$nuxt.$root.constructor
}
win.postMessage({
devtoolsEnabled: (/* Vue 2 */ Vue && Vue.config.devtools) ||
(/* Vue 3.2.14+ */ window.__VUE_DEVTOOLS_GLOBAL_HOOK__ && window.__VUE_DEVTOOLS_GLOBAL_HOOK__.enabled),
vueDetected: true,
nuxtDetected: true,
}, '*')
return
}
// Method 2: Check Vue 3
const vueDetected = !!(window.__VUE__)
if (vueDetected) {
win.postMessage({
devtoolsEnabled: /* Vue 3.2.14+ */ window.__VUE_DEVTOOLS_GLOBAL_HOOK__ && window.__VUE_DEVTOOLS_GLOBAL_HOOK__.enabled,
vueDetected: true,
}, '*')
return
}
// Method 3: Scan all elements inside document
const all = document.querySelectorAll('*')
let el
for (let i = 0; i < all.length; i++) {
if (all[i].__vue__) {
el = all[i]
break
}
}
if (el) {
let Vue = Object.getPrototypeOf(el.__vue__).constructor
while (Vue.super) {
Vue = Vue.super
}
win.postMessage({
devtoolsEnabled: Vue.config.devtools,
vueDetected: true,
}, '*')
return
}
if (detectRemainingTries > 0) {
detectRemainingTries--
setTimeout(() => {
runDetect()
}, delay)
delay *= 5
}
}
setTimeout(() => {
runDetect()
}, 100)
}
// inject the hook
if (document instanceof HTMLDocument) {
installScript(detect)
installScript(installToast)
}
function installScript (fn) {
const source = ';(' + fn.toString() + ')(window)'
if (isFirefox) {
// eslint-disable-next-line no-eval
window.eval(source) // in Firefox, this evaluates on the content window
} else {
const script = document.createElement('script')
script.textContent = source
document.documentElement.appendChild(script)
script.parentNode.removeChild(script)
}
}