是否可以注入一个JavaScript代码来覆盖DOM中存在的JavaScript代码? (例如,默认警报功能)

编程入门 行业动态 更新时间:2024-10-24 04:47:51
本文介绍了是否可以注入一个JavaScript代码来覆盖DOM中存在的JavaScript代码? (例如,默认警报功能)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

好的,我想要的是重写一个已经存在于选项卡中的方法,我要使用的是默认的警报函数。 在JS函数内覆盖它会非常容易。只需添加

window.alert = function(){ //做某事}

但问题是,当我尝试使用 chrome.tabs.executeScript(window .alert = function(){};); 它不起作用。我试图通过使用Chrome中的控制台手动执行此操作,我希望重写该功能,我在日志中键入了该重写功能并按下了回车键,然后完成,警报功能被覆盖,但我无法做到这通过Chrome扩展。

当您添加executeScript时,它似乎创建了一个与标签DOM内部不同的Javascript,因为我可以创建名称为一个已经存在于标签DOM内部的函数。

是否有一种方法可以让executeScript在标签DOM内部编写脚本,因此它可以覆盖任何函数是由.js文件生成的页面编写的?

谢谢!

解决方案

函数不作为DOM的一部分存在;相反,它们存在于包含DOM的执行环境中。内容脚本(包括使用 executeScript )和实际网页共享相同的DOM,但具有独立的执行环境。因此,调用 window.alert = function(){} 仅在内容脚本的执行环境中重写 window.alert ,而不是在实际的页面中。

到达实际页面的执行环境的典型方法是注入< script> 标记到DOM中。这可以通过几种方式完成。一种方法是在 web_accessible_resource 中列出脚本c $ c> ,并在文档中插入引用此脚本的< script> 元素。必需的绝对URL可以通过 chrome.extension获取。 getURL 。

var s = document.createElement(script); s.src = chrome.extension.getURL(script_in_extension.js); (document.head || document.documentElement).appendChild(s);

确保将脚本配置为 run_at:document_start ,以便在任何

注意:您的操作很容易被页面撤消:

window.alert = function(){/*...*/}; //覆盖 delete window.alert; //从页面/控制台/ ...运行 window.alert('Test?'); //显示警报框。

如果被覆盖的函数无法删除至关重要,请使用 Object.defineProperty 定义一个不可变的方法。有关更多详细信息,请参阅停止使用Chrome执行功能扩展。

Ok, so what I want is to override a method that already exists inside a tab, what I'm going to use is the default alert function. Override it inside the JS function would be very easy. just add

window.alert = function(){ //Do Something }

but the problem is that when I try to use chrome.tabs.executeScript("window.alert = function() { };"); it doesn't work. I tried to do this manually by using the Console from Chrome in the tab that I wanted to override the function, I typed that override function in the log and pressed enter, and done, the alert function was overridden, but I can't do this via Chrome Extension.

When you add executeScript, it seems like it creates a Javascript apart from the one inside the tab DOM, because I can create functions with the name of a function that already exists inside the tab DOM.

Is there a way to make executeScript to write the script inside of the tab DOM, so it can actually override any function that was written by the .js file the page generated?

Thanks!

解决方案

Functions don't exist as part of the DOM; instead, they exist within an execution environment that includes the DOM. Content scripts (including scripts run with executeScript) and actual web pages share the same DOM, but have separate execution environments. So calling window.alert = function() {} only rewrites window.alert within your content script's execution environment, not in the actual page's one.

The typical way to reach the execution environment of the actual page is to inject a <script> tag into the DOM. This can be done in several ways. One method is to white-list a script in web_accessible_resource, and insert the <script> element referring to this script in the document. The required absolute URL can be obtained via chrome.extension.getURL.

var s = document.createElement("script"); s.src = chrome.extension.getURL("script_in_extension.js"); (document.head||document.documentElement).appendChild(s);

Make sure that the script is configured to "run_at": "document_start", so that the overwrite takes place before any of the page's functions are loaded.

Note: Your action can easily be undone by the page:

window.alert = function(){ /*...*/ }; // Your overwrite delete window.alert; // Run from the page/console/... window.alert('Test?'); // Displays alert box.

If it's critical that the overwritten function cannot be removed, use Object.defineProperty to define an immutable method. For more details, see Stop a function from execute with Chrome extension.

更多推荐

是否可以注入一个JavaScript代码来覆盖DOM中存在的JavaScript代码? (例如,默认警报功能)

本文发布于:2023-11-27 15:58:57,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码   警报   功能   JavaScript   DOM

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!