I'm wenku8 admin!

通过简单的几行代码把用户伪装成wenku8管管

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/428403/1610689/I%27m%20wenku8%20admin%21.js

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

(function __MAIN__() {
    const targetPage1 = 'https://www.wenku8.net/userinfo.php?id=917768';
    const targetPage2 = 'https://www.wenku8.cc/userinfo.php?id=917768';
    const scriptId = 'pretend_admin';
    const imgId = 'pretend_admin_img';
    const scriptLink = 'https://greasyfork.org/scripts/428403/code/script.js';
    //const scriptLink = 'https://pastebin.com/raw/EcHyuzuh';
    const scriptSource = __MAIN__.toString();
    main();

    function main() {
        $('#' + imgId)?.remove();

        switch(location.href) {
            case 'about:blank': {
                console.log('Wenku8 admin: Injecting to opener...');
                // Opened page: Inject to opener
                const oDoc = window.opener.document;
                const script = $(oDoc, scriptId);
                loadJS(script.src, null, null, oDoc);
                break;
            }
            case targetPage1:
            case targetPage2: {
                // Just this page
                console.log('Wenku8 admin: Editing page...');
                getDocument('/userinfo.php?id=2', function (oDoc) {
                    const content = $(document, '#content');
                    const oContent = $(oDoc, '#content');
                    content.innerHTML = oContent.innerHTML;
                });
                break;
            }
            default: {
                // Opener page: Make all opener links magical
                if (window.magical) { return false; }
                console.log('Wenku8 admin: Doing magic...');
                window.magical = true;
                const as = Array.from(document.querySelectorAll('a[href*="/userinfo.php?id=917768"]'))
                    .filter(a => a.pathname === '/userinfo.php' && new URLSearchParams(a.search).get('id') === '917768');
                as.forEach(a => {
                    a.addEventListener('click', function (e) {
                        destroyEvent(e);
                        const newtab = window.open(a.href);
                        const oWin = newtab.window;
                        oWin.addEventListener('load', function(e) {
                            const dom = oWin.document;
                            loadJS(scriptLink, null, err => {
                                const script = dom.createElement('script');
                                script.innerHTML = `(${scriptSource}) ();`;
                                dom.head.appendChild(script);
                            }, dom);
                        });
                    });
                });
            }
        }
        return true;
    }

    // Load javascript from given url
    function loadJS(url, onload, onerror, oDoc = document) {
        onload = onload ?? function() {};
        onload = onload ?? function() {};
        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.addEventListener('load', onload)
        script.addEventListener('error', onerror);
        script.src = url;
        oDoc.head.append(script);
    }

    // Download and parse a url page into a html document(dom).
    // when xhr onload: callback.apply([dom, args])
    function getDocument(url, callback, args = []) {
        const xhr = new XMLHttpRequest();
        xhr.open('GET', url, true);
        xhr.responseType = 'blob';
        xhr.onload = function (response) {
            const htmlblob = xhr.response;
            const reader = new FileReader();
            reader.onload = function (e) {
                const htmlText = reader.result;
                const dom = new DOMParser().parseFromString(htmlText, 'text/html');
                args = [dom].concat(args);
                callback.apply(null, args);
                //callback(dom, htmlText);
            }
            reader.readAsText(htmlblob, 'GBK');
        }
        xhr.send();
    }

    // Just stopPropagation and preventDefault
    function destroyEvent(e) {
        if (!e) {return false;};
        if (!e instanceof Event) {return false;};
        e.stopPropagation();
        e.preventDefault();
    }

    function $() {
        if (arguments.length === 1) {
            return document.querySelector(...arguments);
        } else {
            return arguments[0].querySelector(arguments[1]);
        }
    }
}) ();