用户脚本扩展名如何实现不安全的窗口?

发布于 2025-02-11 10:17:35 字数 1106 浏览 1 评论 0 原文

今天,我遇到了一个情况,我需要扩展到网页中的一些对象。

但是,我几乎没有发现关于如何实际实现它的文档,我尝试使用 用户访问 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!

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

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

发布评论

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

评论(1

爱殇璃 2025-02-18 10:17:35

您需要做的是基本上加载DOM并修改内容注入内容脚本。

例如,要修改以下HTML:

<html>
     <button id="mybutton">click me</button>
     <script>
        var greeting = "hello, ";
        var button = document.getElementById("mybutton");
        button.person_name = "Bob";
        button.addEventListener(
            "click", () => alert(greeting + button.person_name + "."), false);
      </script>
</html>

您可以注入此代码:

var greeting = "hola, ";
var button = document.getElementById("mybutton");
button.person_name = "Roberto";
button.addEventListener(
    "click", () => alert(greeting + button.person_name + "."), false);

您可以在Chrome扩展程序上注入动态或静态声明的脚本。

这是示例清单。使用静态声明的json:

{
 "name": "My extension",
 ...
 "content_scripts": [
   {
     "matches": ["https://*.nytimes.com/*"],
     "css": ["my-styles.css"],
     "js": ["content-script.js"]
   }
 ],
 ...
}

此外,您可以检查[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:

<html>
     <button id="mybutton">click me</button>
     <script>
        var greeting = "hello, ";
        var button = document.getElementById("mybutton");
        button.person_name = "Bob";
        button.addEventListener(
            "click", () => alert(greeting + button.person_name + "."), false);
      </script>
</html>

You can inject this code:

var greeting = "hola, ";
var button = document.getElementById("mybutton");
button.person_name = "Roberto";
button.addEventListener(
    "click", () => alert(greeting + button.person_name + "."), false);

You can inject the script with dynamic or static declarations on a Chrome extension.

Here is a sample manifest.json using static declarations:

{
 "name": "My extension",
 ...
 "content_scripts": [
   {
     "matches": ["https://*.nytimes.com/*"],
     "css": ["my-styles.css"],
     "js": ["content-script.js"]
   }
 ],
 ...
}

In addition, you can check [Chrome extension's docs] for more information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文