|
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() { |
|
|
|
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 && Vue.config.devtools) |
|
|| ( window.__VUE_DEVTOOLS_GLOBAL_HOOK__ && window.__VUE_DEVTOOLS_GLOBAL_HOOK__.enabled), |
|
vueDetected: true, |
|
nuxtDetected: true, |
|
}, '*') |
|
|
|
return |
|
} |
|
|
|
|
|
const vueDetected = !!(window.__VUE__) |
|
if (vueDetected) { |
|
win.postMessage({ |
|
devtoolsEnabled: window.__VUE_DEVTOOLS_GLOBAL_HOOK__ && window.__VUE_DEVTOOLS_GLOBAL_HOOK__.enabled, |
|
vueDetected: true, |
|
}, '*') |
|
|
|
return |
|
} |
|
|
|
|
|
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) |
|
} |
|
|
|
|
|
if (document instanceof HTMLDocument) { |
|
installScript(detect) |
|
installScript(installToast) |
|
} |
|
|
|
function installScript(fn) { |
|
const source = `;(${fn.toString()})(window)` |
|
|
|
if (isFirefox) { |
|
|
|
window.eval(source) |
|
} |
|
else { |
|
const script = document.createElement('script') |
|
script.textContent = source |
|
document.documentElement.appendChild(script) |
|
script.parentNode.removeChild(script) |
|
} |
|
} |
|
|