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

时间:2021-06-14 19:38:17

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.

主要问题是我的扩展程序正在加载到目标网页上的每个iframe中。它还会将按钮显示在iframe中。我希望他们消失。窗口和文档对象显示为父窗口和文档对象。因此,例如检查文档位置是不可能的,因为它显示了父级的位置而不是iframe的位置。

3 个解决方案

#1


3  

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.

您可以编写一个使用@noframes元数据标头键的用户脚本,并使用此用户脚本包将用户脚本包含在您的Jetpack中,以用于addon sdk。

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

编写用户脚本比编写Page Mods要容易得多。

#2


2  

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

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

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

以下信息已过时(如果您针对Add-on SDK 1.10或之前版本构建,则可以使用此信息:

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).

不幸的是,pagemod会在页面的所有框架中注入您的脚本,而不是在顶部框架上每个页面注入一次,就像您使用Chrome的内容脚本一样。当然,有一些停止执行脚本的解决方法(参见其他答案)。

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:

Bellow我提供了从pagemod移植到选项卡的示例,并保留了所有消息接收系统的端口工作。如果您不仅需要接收,还可以使用相同的端口对象发送消息,则可以轻松修改它。当我们在youtube.com域中时,脚本会被注入:

Old pagemod way:

旧的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);
        });
    }
});

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);
            });
        }
    }
});

#3


1  

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.

内容脚本仍将添加到每个页面,但它是iFrames的一个相对简单和轻量级的检查。

#1


3  

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.

您可以编写一个使用@noframes元数据标头键的用户脚本,并使用此用户脚本包将用户脚本包含在您的Jetpack中,以用于addon sdk。

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

编写用户脚本比编写Page Mods要容易得多。

#2


2  

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

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

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

以下信息已过时(如果您针对Add-on SDK 1.10或之前版本构建,则可以使用此信息:

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).

不幸的是,pagemod会在页面的所有框架中注入您的脚本,而不是在顶部框架上每个页面注入一次,就像您使用Chrome的内容脚本一样。当然,有一些停止执行脚本的解决方法(参见其他答案)。

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:

Bellow我提供了从pagemod移植到选项卡的示例,并保留了所有消息接收系统的端口工作。如果您不仅需要接收,还可以使用相同的端口对象发送消息,则可以轻松修改它。当我们在youtube.com域中时,脚本会被注入:

Old pagemod way:

旧的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);
        });
    }
});

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);
            });
        }
    }
});

#3


1  

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.

内容脚本仍将添加到每个页面,但它是iFrames的一个相对简单和轻量级的检查。