我可以使用 Google Chrome 扩展程序阻止alert()吗
我可以创建一个 Google Chrome 扩展来阻止页面执行 alert()
吗?
Can I create a Google chrome extension to prevent the page from doing an alert()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如 @MrGlass 所说,目前,Chrome 扩展程序在单独的环境中运行,限制对实际
window
对象的访问,并提供仅对扩展程序有效的副本。为了解决这个问题,我们可以将脚本元素直接注入到文档中。这样,您就可以访问文档的环境和真实的
window
对象。首先,让我们创建函数(我还添加了“确认”,因为有些确认让我很烦):
现在,我们要做的就是在文本脚本中转换该函数并将其括在括号中(以避免与页面环境中的实际变量可能发生冲突):
最后,我们注入一个脚本元素,并立即将其删除:
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):
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):
And finally, we inject a script element, and immediately remove it:
是的,你可以,alert() 只是一个 JavaScript 方法,你可以通过这样做来覆盖它的功能。
只需记住通过 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.
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
谢谢。这很有帮助。 我需要这样做才能使其正常工作,我可以这样做
但是,我意识到如果我想删除警报并确认,
Thank you. That helped. However, I realized I needed to do this to get it to work
if I wanted to remove alerts and confirms, I can do