今天,我遇到了一个情况,我需要扩展到网页中的一些对象。
但是,我几乎没有发现关于如何实际实现它的文档,我尝试使用 用户访问
API(Firefox当前独有的),但是 window> window
对象是从 window api。 /代码>在页面的HTML DOM上。
我知道这是可能的,因为像greasemonkey这样的用户脚本管理器实现了 unsafeWindow
对象,该对象可以在网页的毫无隔离的上下文中直接访问对象,从而使猴子贴上非常容易。
因此,我想知道这实际上是如何实现的。我已经尝试研究了一个用户脚本管理器的源代码,称为 fivalmonkey ,我发现它定义了< code>unsafeWindow in src/indected/web/gm-api-wrapper.js:53
作为对 global> global
对象的引用在源代码中定义。
我知道这不是通过简单地在页面中注入&lt; script&gt;
元素来实现的,因为在我的检查时,页面的DOM中没有看不见。
我对这种酷机制的实现非常好奇,我敢肯定我错过了一些明显的东西,所以我需要您的帮助来消除我的盲目!
Today I came across a situation where I need to make an extension to monkey-patch some objects in a web page.
However I found little to none documentation on how it can actually be achieved, I've tried using the userScripts
API (currently exclusive to Firefox) but the window
object is contextually isolated from the window
on the HTML DOM of the page.
I know that this is possible because user script managers like GreaseMonkey implement an unsafeWindow
object which gives the script direct access to objects in the unisolated context of the web page, thus making monkey-patching very easy.
So I would like to know how this is actually achieved. I've attempted to study the source code of an user script manager called ViolentMonkey, I found that it defines unsafeWindow
in src/injected/web/gm-api-wrapper.js:53
as a reference to the global
object but I could not locate where this in turn is defined in the source code.
I know that this is not implemented by simply injecting a <script>
element in the page because none is visible in the page's DOM upon my inspection.
I'm very curious about the implementation of this cool mechanism, I'm pretty sure I missed something obvious so I need your help to remove my blind-folds!
发布评论
评论(1)
您需要做的是基本上加载DOM并修改内容注入内容脚本。
例如,要修改以下HTML:
您可以注入此代码:
您可以在Chrome扩展程序上注入动态或静态声明的脚本。
这是示例清单。使用静态声明的json:
此外,您可以检查[Chrome Extension的文档]以获取更多信息。
What you need to do is basically load the DOM and modify the content injecting Content Scripts.
For example to modify the following HTML:
You can inject this code:
You can inject the script with dynamic or static declarations on a Chrome extension.
Here is a sample manifest.json using static declarations:
In addition, you can check [Chrome extension's docs] for more information.