首页 > 解决方案 > 如何杀死 Chrome 开发者工具中的警告框?

问题描述

示例js:

如何在 chrome 开发人员工具中杀死警报框?

在 content_scripts -> content.js

window.onload = function () {
   window.alert = function () {}
}

但没有效果,帮帮我。

标签: javascript

解决方案


清单.json

  "content_scripts": [
    {
      "matches": [
        "http://elearn.ecust.edu.cn:86/*"
      ],
      "js": [
        "inject.js"
      ],
      "run_at": "document_start",
      "all_frames": true
    }
  ],

注入.js

window.addEventListener("DOMContentLoaded", function () {
    console.log("----加载inject.js---DOMContentLoaded")
    window.alert = function () { }
    overrideSelectNativeJS_Functions()
})

window.addEventListener("load", function () {
    console.log("----加载inject.js---load")
    window.alert = function () { }
    overrideSelectNativeJS_Functions()
})
window.addEventListener("beforeunload", function () {
    console.log("----加载inject.js---beforeunload")
    window.alert = function () { }
    overrideSelectNativeJS_Functions()
})
window.addEventListener("unload", function () {
    console.log("----加载inject.js---unload")
    window.alert = function () { }
    overrideSelectNativeJS_Functions()
})


addJS_Node(null, null, overrideSelectNativeJS_Functions);

function overrideSelectNativeJS_Functions() {
    window.alert = function alert(message) {
        console.log(message);
    }
}

function addJS_Node(text, s_URL, funcToRun) {
    var D = document;
    var scriptNode = D.createElement('script');
    scriptNode.type = "text/javascript";
    if (text) scriptNode.textContent = text;
    if (s_URL) scriptNode.src = s_URL;
    if (funcToRun) scriptNode.textContent = '(' + funcToRun.toString() + ')()';

    var targ = D.getElementsByTagName('head')[0] || D.body || D.documentElement;
    targ.appendChild(scriptNode);
}

推荐阅读