Hook Vue3 app

通过劫持Proxy方法,逆向还原Vue3 app元素到DOM

14.08.2022 itibariyledir. En son verisyonu görün.

Bu script direkt olarak kurulamaz. Başka scriptler için bir kütüphanedir ve meta yönergeleri içerir // @require https://update.greasyfork.org/scripts/449444/1081399/Hook%20Vue3%20app.js

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==UserScript==
// @name         Hook Vue3 app
// @version      1.0.2
// @description  通过劫持Proxy方法,逆向还原Vue3 app元素到DOM
// @author       DreamNya
// @license      MIT
// @namespace https://greasyfork.org/users/809466
// ==/UserScript==

const $window = window.unsafeWindow || document.defaultView || window
const realLog = $window.console.log; //反劫持console.log
const realProxy = $window.Proxy; //劫持Proxy

var vueApp = {} //存储所有app
var vueHooked = {} //存储已还原app

$window.Proxy = function () {
    let app = arguments[0]._
    if (app && app.uid >= 0) { //判断app
        let el = app.vnode.el
        if (el) {
            if (!el.__vue__) {
                el.__vue__ = app //存在el则还原__vue__
                if (el.__vue__) {
                    recordVue(vueHooked, app)
                }
            }
        } else {
            //realLog(app,el)
            watchEl(app.vnode) //不存在el则进行观察
        }
        recordVue(vueApp, app)
    }
    return new realProxy(...arguments)
}

function watchEl(vnode) { //观察el 变动时还原到DOM
    let value = vnode.el
    let hooked = false
    Object.defineProperty(vnode, "el", {
        get() {
            return value
        },
        set(newValue) {
            value = newValue
            if (!hooked && this.el) {
                this.el.__vue__ = this.component
                if (this.el.__vue__) {
                    hooked = true
                    recordVue(vueHooked, this.component)
                    //realLog(this.component,"已还原")
                }
            }
        }
    })
}

function recordVue(obj, app) { //以uid为key存储app
    if (!obj[app.uid]) { //uid不存在则直接存储
        obj[app.uid] = app
    } else if (Array.isArray(obj[app.uid])) {
        obj[app.uid].push(app)
    } else {
        obj[app.uid] = [obj[app.uid], app] //多个root相同uid时以数组形式存储 可能存在优化空间
    }
}