我可以使用 Google Chrome 扩展程序阻止alert()吗

发布于 2024-09-03 14:49:53 字数 62 浏览 10 评论 0原文

我可以创建一个 Google Chrome 扩展来阻止页面执行 alert() 吗?

Can I create a Google chrome extension to prevent the page from doing an alert() ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温柔少女心 2024-09-10 14:49:53

正如 @MrGlass 所说,目前,Chrome 扩展程序在单独的环境中运行,限制对实际 window 对象的访问,并提供仅对扩展程序有效的副本。

为了解决这个问题,我们可以将脚本元素直接注入到文档中。这样,您就可以访问文档的环境和真实的window对象。

首先,让我们创建函数(我还添加了“确认”,因为有些确认让我很烦):

var disablerFunction = function () {

    window.alert = function alert(msg) { console.log('Hidden Alert ' + msg); };
    window.confirm = function confirm(msg) { 
        console.log("Hidden Confirm " + msg); 
        return true; /*simulates user clicking yes*/ 
    };

};

现在,我们要做的就是在文本脚本中转换该函数并将其括在括号中(以避免与页面环境中的实际变量可能发生冲突):

var disablerCode = "(" + disablerFunction.toString() + ")();";

最后,我们注入一个脚本元素,并立即将其删除:

var disablerScriptElement = document.createElement('script');
disablerScriptElement.textContent = disablerCode;

document.documentElement.appendChild(disablerScriptElement);
disablerScriptElement.parentNode.removeChild(disablerScriptElement);

As @MrGlass said, currently, Chrome Extensions run in a separate environment, limiting access to the actual window object and providing a duplicate that is only valid for the extension.

To solve this, we can inject a script element directly into the document. This way, you access the document's environment and the real window object.

First, lets create the function (I added the "confirm" as well, because some confirms were annoying me so much):

var disablerFunction = function () {

    window.alert = function alert(msg) { console.log('Hidden Alert ' + msg); };
    window.confirm = function confirm(msg) { 
        console.log("Hidden Confirm " + msg); 
        return true; /*simulates user clicking yes*/ 
    };

};

Now, what we're going to do is to transform that function in a text script and enclose it in parentheses (to avoid possible conflicts with actual vars in the page environment):

var disablerCode = "(" + disablerFunction.toString() + ")();";

And finally, we inject a script element, and immediately remove it:

var disablerScriptElement = document.createElement('script');
disablerScriptElement.textContent = disablerCode;

document.documentElement.appendChild(disablerScriptElement);
disablerScriptElement.parentNode.removeChild(disablerScriptElement);
╭ゆ眷念 2024-09-10 14:49:53

是的,你可以,alert() 只是一个 JavaScript 方法,你可以通过这样做来覆盖它的功能。

window.alert = function alert(msg) {
  console.log('Hidden Alert ' + msg);
};

只需记住通过 run_at 清单内容脚本修饰符在清单内的 document_start 处运行该内容脚本即可。

我相信有一个扩展可以做到这一点。开发者将其命名为 Nice Alert。
https://chrome.google.com/extensions/detail/ehnbelnegmgdnjaghgomaakjcmpcakhk

Yes you can, alert() is just a JavaScript method, you can override its functionality by doing.

window.alert = function alert(msg) {
  console.log('Hidden Alert ' + msg);
};

Just remember to run that content script at document_start within the manifest via run_at manifest content script modifier.

I believe there is an extension that just does that. The developer names it Nice Alert.
https://chrome.google.com/extensions/detail/ehnbelnegmgdnjaghgomaakjcmpcakhk

甜心 2024-09-10 14:49:53

谢谢。这很有帮助。 我需要这样做才能使其正常工作,我可以这样做

location.href="javascript: window.alert = function(x) {console.log(x)};"

但是,我意识到如果我想删除警报并确认,

location.href="javascript: window.alert = function(x) {console.log(x)}; window.confirm = function(){return true;};";

Thank you. That helped. However, I realized I needed to do this to get it to work

location.href="javascript: window.alert = function(x) {console.log(x)};"

if I wanted to remove alerts and confirms, I can do

location.href="javascript: window.alert = function(x) {console.log(x)}; window.confirm = function(){return true;};";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文