Greasy Fork is available in English.

拉卡尼休(Rakanishu)暴露内置变量到全局

通过劫持webpack4打包代码方法,暴露游戏内置变量到全局window.game

// ==UserScript==
// @name         拉卡尼休(Rakanishu)暴露内置变量到全局
// @version      1.0
// @description  通过劫持webpack4打包代码方法,暴露游戏内置变量到全局window.game
// @author       DreamNya
// @match        https://g1tyx.github.io/rakanishu/
// @match        https://yx.g8hh.com/rakanishu
// @grant        none
// @run-at       document-start
// @license      MIT
// @namespace https://greasyfork.org/users/809466
// ==/UserScript==
/*
【原理详解】
webpack4打包代码劫持方法探究
https://bbs.tampermonkey.net.cn/thread-2950-1-1.html
*/
let value;
let hooked = false;
Object.defineProperty(window, "webpackChunkrakanishu", {
    get() {
        return value
    },
    set(newValue) {
        value = newValue
        if (!hooked && window.webpackChunkrakanishu.push && window.webpackChunkrakanishu.push != window.Array.prototype.push) {
            window.webpackChunkrakanishu.realPush = window.webpackChunkrakanishu.push
            window.webpackChunkrakanishu.push = function (...args) {
                if (typeof args[0]?.[1]?.[952] == "function") {
                    let fucText = args[0][1][952].toString()

                    //replace去头+slice去尾
                    fucText = fucText.replace("(me,_e,L)=>{", "").slice(0, -1)
                    //暴露闭包对象到全局
                    fucText = fucText.replace("function Pd(t,n,e,i,r){", "function Pd(t,n,e,i,r){if(!window.game && n?.[15]?.[15]?.[8].data){window.game=n[15][15][8].data}")
                    //构造劫持函数
                    args[0][1][952] = new Function("me,_e,L", fucText)

                    //劫持成功后还原劫持
                    this.push = this.realPush
                }
                this.realPush(...args)
            }
            hooked = true
        }
    }
})