启用 Vue devtools

为所有网站启用 Vue devtools

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name               Vue devtools enabler
// @name:zh-CN         启用 Vue devtools
// @namespace          https://greasyfork.org/users/667968-pyudng
// @version            0.1.2
// @description        Enable Vue devtools on all websites
// @description:zh-CN  为所有网站启用 Vue devtools
// @author             PY-DNG
// @license            GPL-3.0-or-later
// @match              http*://*/*
// @icon               https://cn.vuejs.org/logo.svg
// @grant              GM_log
// @run-at             document-start
// ==/UserScript==

/* eslint-disable no-multi-spaces */
/* eslint-disable no-return-assign */

(async function __MAIN__() {
    'use strict';

    // 获取window
    const win = typeof unsafeWindow !== 'undefined' ? unsafeWindow : window;

    // 监听和修改window.__VUE_DEVTOOLS_GLOBAL_HOOK__
    const VueDevtoolsGlobalHook = await (function() {
        const { promise, resolve } = Promise.withResolvers();
        check();
        return promise;

        function check() {
            if (win.hasOwnProperty('__VUE_DEVTOOLS_GLOBAL_HOOK__')) {
                const VHook = win.__VUE_DEVTOOLS_GLOBAL_HOOK__;
                VHook.enabled = true;
                resolve(VHook);
            } else {
                setTimeout(check);
            }
        }
    }) ();

    // Vue实例处理器
    const VueDealer = {
        roots: new Set(),

        vue2: {
            connect(app) {
                const root = app.$root;
                if (VueDealer.roots.has(root)) { return; }

                let Vue = root.constructor;
                while (Vue.super) { Vue = Vue.super; }
                Vue.config.devtools = true;
                VueDevtoolsGlobalHook.emit("init", Vue);

                VueDealer.roots.add(root);
            },
        },

        vue3: {
            connect(app) {
                if (VueDealer.roots.has(app)) { return; }
                if (!Array.isArray(VueDevtoolsGlobalHook.apps)) { return; }
                if (VueDevtoolsGlobalHook.apps.includes(app)) { return; }

                const version = app.version;
                const types = {
                    Fragment: undefined,
                    Text: undefined,
                    Comment: undefined,
                    Static: undefined
                };
                VueDevtoolsGlobalHook.emit("app:init", app, version, types);
                const unmount = app.unmount;
                app.unmount = function() {
                    VueDevtoolsGlobalHook.emit("app:unmount", app);
                    unmount.call(app);
                };

                VueDealer.roots.add(app);
            }
        }
    };

    // 每当DOM变化时,都遍历所有元素,寻找Vue实例
    const observer = new MutationObserver(() => {
        for (const element of document.querySelectorAll('*')) {
            if (element.__vue__?._isVue) {
                VueDealer.vue2.connect(element.__vue__);
            }
            if (element.__vue_app__?.version) {
                VueDealer.vue3.connect(element.__vue_app__);
            }
        }
    });
    observer.observe(document, {
        attributes: true,
        childList: true,
        subtree: true,
    });
}) ();