如何过滤附加 SDK 扩展中的 iframe?

发布于 2024-12-03 10:44:09 字数 128 浏览 3 评论 0原文

主要问题是我的扩展正在加载到目标网页上的每个 iframe 中。它还放置了出现在 iframe 内的按钮。我想让他们消失。窗口和文档对象显示为父级的窗口和文档对象。因此,无法检查文档位置,因为它显示的是父级的位置而不是 iframe 的位置。

The main problem is that my extension is loading into every iframes on a target webpage. It puts buttons that appear inside the iframes as well. I want them to disappear. The window and document objects are shown as the parent's window and document objects. So it's impossible to check the document location for example because it shows the parent's location instead of the iframe's location.

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

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

发布评论

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

评论(3

陈独秀 2024-12-10 10:44:10

您可以编写一个使用 @noframes 元数据标头键的用户脚本,并将用户脚本包含到您的 Jetpack 中:此插件 SDK 的用户脚本包

编写用户脚本也比编写页面 Mod 容易得多。

You could write a user script which uses the @noframes metadata header key and include the user script in to your Jetpack with this user script package for the addon sdk.

Writing user scripts is much easier than writing Page Mods too.

此生挚爱伱 2024-12-10 10:44:10

编辑:现在(附加 SDK 版本 1.11 已发布)pagemod 支持您正在寻找的内容。 参见文档和新的attachTo选项。

以下信息已过时(如果您针对 Add-on SDK 1.10 或上一篇:

不幸的是,pagemod 将脚本注入到页面的所有框架中,而不是像使用 Chrome 的内容脚本那样在顶部框架中每个页面注入一次。 但是,如果您确实想在每个页面的顶部框架上注入一次脚本

您必须使用选项卡<。使用此解决方案,您的脚本仅在有时才会被注入。下面

我提供了从 pagemod 移植到 tabs 的示例,使所有消息接收系统都可以轻松修改。如果您不仅需要接收消息,还需要使用相同的 port 对象发送消息。当我们位于 youtube.com 域中时,脚本就会被注入:

旧 pagemod 方式:

var self = require("self");
var pagemod = require("page-mod");

pagemod.PageMod(
{
    include: "*.youtube.com",
    contentScriptFile: self.data.url("myContentScript.js"),
    onAttach: function(worker)
    {
        worker.port.on("myMessageId", function(payload)
        {
            console.log("Message received: " + payload);
        });
    }
});

新标签方式:

var self = require("self");
var tabs = require("tabs");

tabs.on("ready", function(tab)
{
    if (tab != undefined && tab.url != undefined && tab.url.split("/")[2] != undefined)
    {
        var domain = "youtube.com";
        var host = tab.url.split("/")[2];
        if (host == domain || host.substr(host.length - domain.length - 1) == "." + domain)
        {
            var worker = tab.attach({ contentScriptFile: self.data.url("myContentScript.js") });
            worker.port.on("myMessageId", function(payload)
            {
                console.log("Message received: " + payload);
            });
        }
    }
});

Edit: now (Add-on SDK version 1.11 released) pagemod supports what you are looking for. See the docs and the new attachTo option.

The following information is outdated (can be used if you build against Add-on SDK 1.10 or previous:

Unfortunately pagemod injects your script in all the frames of the page, and not just once per page on the top frame as what you achieve with Chrome's content scripts. Of course, there are workarounds for stopping the execution of the script (see the other answers).

But if you really want to inject your script only once per page, on the top frame, you have to use tabs. With this solution you script only gets injected when it has to.

Bellow I provide and example for porting from pagemod to tabs, keeping all the message reception system with port working. It can easily be modified if you need to not just receive, but also send messages using that same port object. The script gets injected when we are in the domain youtube.com:

Old pagemod way:

var self = require("self");
var pagemod = require("page-mod");

pagemod.PageMod(
{
    include: "*.youtube.com",
    contentScriptFile: self.data.url("myContentScript.js"),
    onAttach: function(worker)
    {
        worker.port.on("myMessageId", function(payload)
        {
            console.log("Message received: " + payload);
        });
    }
});

New tabs way:

var self = require("self");
var tabs = require("tabs");

tabs.on("ready", function(tab)
{
    if (tab != undefined && tab.url != undefined && tab.url.split("/")[2] != undefined)
    {
        var domain = "youtube.com";
        var host = tab.url.split("/")[2];
        if (host == domain || host.substr(host.length - domain.length - 1) == "." + domain)
        {
            var worker = tab.attach({ contentScriptFile: self.data.url("myContentScript.js") });
            worker.port.on("myMessageId", function(payload)
            {
                console.log("Message received: " + payload);
            });
        }
    }
});
所谓喜欢 2024-12-10 10:44:10

一种解决方法是在内容脚本中添加类似的内容:

   if (window.frameElement === null){
     // I'm in the topmost window
     // Add buttons and things to the page.
   }else{
     // I'm in an iFrame... do nothing!
   }

内容脚本仍将添加到每个页面,但这是对 iFrame 的相对简单且轻量级的检查。

One workaround is to put something like this in your content script:

   if (window.frameElement === null){
     // I'm in the topmost window
     // Add buttons and things to the page.
   }else{
     // I'm in an iFrame... do nothing!
   }

The content script will still be added to every page, but it's a relatively simple and lightweight check for iFrames.

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