Skip to content

Commit

Permalink
refactor: migrate to manifest v3
Browse files Browse the repository at this point in the history
based on #2136
  • Loading branch information
Akryum authored and yyx990803 committed Sep 11, 2024
1 parent 7075a12 commit 38684af
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 146 deletions.
45 changes: 28 additions & 17 deletions packages/shell-chrome/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
"name": "Vue.js devtools",
"version": "5.3.4",
"version_name": "5.3.4",
"description": "Chrome and Firefox DevTools extension for debugging Vue.js applications.",
"manifest_version": 2,
"description": "DevTools extension for debugging Vue.js applications. This is version 5.",
"manifest_version": 3,
"icons": {
"16": "icons/16.png",
"48": "icons/48.png",
"128": "icons/128.png"
},
"browser_action": {
"action": {
"default_icon": {
"16": "icons/16-gray.png",
"48": "icons/48-gray.png",
Expand All @@ -19,23 +19,31 @@
"default_popup": "popups/not-found.html"
},
"web_accessible_resources": [
"devtools.html",
"devtools-background.html",
"build/backend.js"
{
"resources": [
"devtools.html",
"devtools-background.html",
"build/backend.js",
"build/proxy.js",
"build/hook-exec.js",
"build/detector-exec.js"
],
"matches": [
"<all_urls>"
],
"extension_ids": []
}
],
"devtools_page": "devtools-background.html",
"background": {
"scripts": [
"build/background.js"
],
"persistent": false
"service_worker": "build/service-worker.js"
},
"permissions": [
"http://*/*",
"https://*/*",
"file:///*",
"contextMenus",
"storage"
"storage",
"scripting"
],
"host_permissions": [
"<all_urls>"
],
"content_scripts": [
{
Expand All @@ -56,5 +64,8 @@
],
"run_at": "document_idle"
}
]
}
],
"content_security_policy": {
"extension_pages": "script-src 'self'; object-src 'self'"
}
}
85 changes: 85 additions & 0 deletions packages/shell-chrome/src/detector-exec.js
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)
}
77 changes: 9 additions & 68 deletions packages/shell-chrome/src/detector.js
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)
3 changes: 3 additions & 0 deletions packages/shell-chrome/src/hook-exec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { installHook } from '@back/hook'

installHook(window)
22 changes: 5 additions & 17 deletions packages/shell-chrome/src/hook.js
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)
Loading

0 comments on commit 38684af

Please sign in to comment.