2

我刚开始使用 FireFox Builder 来构建一个简单的插件。我意识到我无法直接访问窗口对象。

我想要做的是获取窗口对象并用一些类和函数污染它,以便我可以从页面本身调用它们。

以下是当前代码:

// This is an active module of the ritcoder Add-on
require("widget").Widget({
    id: "widgetID1",
    label: "My Mozilla Widget",
    contentURL: "http://www.mozilla.org/favicon.ico",
    onClick: function(evt){
        var tabs = require("tabs");
        var activeTab = tabs.activeTab;

        var notifications = require("notifications");
        notifications.notify({
          title: "Jabberwocky",
          text: "'Twas brillig, and the slithy toves",
          data: "did gyre and gimble in the wabe",
          onClick: function (data) {
            console.log(data);
            // console.log(this.data) would produce the same result.
          }
        });

        activeTab.window.a=20; //this fails
        context.alert('yesx');
    }
});

我该怎么做呢?将一些代码注入活动页面,以便可以调用它。

问候,

4

1 回答 1

2

您需要使用tab.attach()在选项卡的上下文中运行内容脚本,然后使用unsafeWindow添加页面脚本可以看到的属性。(您还应该阅读内容脚本的介绍。)

插件 SDK 不提供从插件代码到页面的直接访问(没有内容脚本),因为它试图向前兼容使网页在独立于浏览器和插件的进程中运行的计划。 on的过程。

于 2011-12-11T02:31:28.980 回答