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