可以监听js以及其内容

26 阅读25分钟

明天就可以直接检查过往的js有没有包含特定的字符串变量名了。

// ==UserScript== // @name hook fetch // @namespace tampermonkey.net/ // @version 0.1 // @description try to take over the world! // @author wenzd // @run-at document-start // @match www.zhihu.com/* // @icon www.google.com/s2/favicons… // @grant GM_xmlhttpRequest // ==/UserScript==

(function() { 'use strict';

// Your code here...

const suffix = 'react.production.min.js';
const originalAppendChild = Element.prototype.appendChild;
Element.prototype.appendChild = function (node) {

    if (node instanceof HTMLScriptElement) {
        //debugger;
        if (node.tagName === 'SCRIPT' && node.src) {
            //debugger;
            if (node.src.includes(suffix)) {
                //debugger;
                console.log('拦截到目标JS文件:', node.src);
                const newScript = document.createElement('script');
                newScript.textContent = `
                // 这里放置你修改后的JS代码
                // 或者通过fetch获取原始代码并修改
                console.log('这是修改后的JS文件');

                // 原始代码的修改版本...
            `;
                // 将新脚本添加到文档中
                document.head.appendChild(newScript);
                // 阻止原始脚本加载
                return newScript;
            }
        }
    }
    // 调用原始方法
    return originalAppendChild.call(this, node);
};


GM_xmlhttpRequest({
    method: 'GET',
    url: 'https://xtx-gen-oss.oss-cn-chengdu.aliyuncs.com/main_site/assets/js/jquery.1.12.4.min.js',
    onload: function (response) {
        debugger;
        console.log(response.responseText);
    },
    onerror: function (error) {
        debugger;
    }
});})();