-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
based on #2136
- Loading branch information
Showing
7 changed files
with
181 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { installToast } from '@back/toast' | ||
|
||
function sendMessage (message) { | ||
window.postMessage({ | ||
key: '_vue-devtools-send-message', | ||
message, | ||
}) | ||
} | ||
|
||
function detect () { | ||
let delay = 1000 | ||
let detectRemainingTries = 10 | ||
|
||
function runDetect () { | ||
// Method 1: Check Nuxt.js | ||
const nuxtDetected = !!(window.__NUXT__ || window.$nuxt) | ||
|
||
if (nuxtDetected) { | ||
let Vue | ||
|
||
if (window.$nuxt) { | ||
Vue = window.$nuxt.$root && window.$nuxt.$root.constructor | ||
} | ||
|
||
sendMessage({ | ||
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) { | ||
sendMessage({ | ||
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 | ||
} | ||
sendMessage({ | ||
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) { | ||
detect() | ||
installToast(window) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,12 @@ | ||
import { installToast } from '@back/toast' | ||
import { isFirefox } from '@utils/env' | ||
|
||
window.addEventListener('message', e => { | ||
if (e.source === window && e.data.vueDetected) { | ||
chrome.runtime.sendMessage(e.data) | ||
window.addEventListener('message', function (event) { | ||
if (event.data.key === '_vue-devtools-send-message') { | ||
chrome.runtime.sendMessage(event.data.message) | ||
} | ||
}) | ||
|
||
function detect (win) { | ||
setTimeout(() => { | ||
// Method 1: Check Nuxt.js | ||
const nuxtDetected = Boolean(window.__NUXT__ || window.$nuxt) | ||
|
||
if (nuxtDetected) { | ||
let Vue | ||
|
||
if (window.$nuxt) { | ||
Vue = window.$nuxt.$root.constructor | ||
} | ||
|
||
win.postMessage({ | ||
devtoolsEnabled: Vue && Vue.config.devtools, | ||
vueDetected: true, | ||
nuxtDetected: true | ||
}, '*') | ||
|
||
return | ||
} | ||
}, false) | ||
|
||
// Method 2: 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 | ||
}, '*') | ||
} | ||
}, 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) | ||
} | ||
const script = document.createElement('script') | ||
script.src = chrome.runtime.getURL('build/detector-exec.js') | ||
script.onload = () => { | ||
script.remove() | ||
} | ||
;(document.head || document.documentElement).appendChild(script) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { installHook } from '@back/hook' | ||
|
||
installHook(window) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,6 @@ | ||
// This script is injected into every page. | ||
import { installHook } from '@back/hook' | ||
import { isFirefox } from '@utils/env' | ||
|
||
// inject the hook | ||
if (document instanceof HTMLDocument) { | ||
const source = ';(' + installHook.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) | ||
} | ||
const script = document.createElement('script') | ||
script.src = chrome.runtime.getURL('build/hook-exec.js') | ||
script.onload = () => { | ||
script.remove() | ||
} | ||
;(document.head || document.documentElement).appendChild(script) |
Oops, something went wrong.