Firefox扩展:面板内部更改的事件监听器

时间:2013-03-07 03:44:16

标签: javascript firefox firefox-addon firefox-addon-sdk

我想要一些帮助:

  • Firefox扩展程序打开一个面板,从外部网站加载内容
  • 如何收听面板内容中的更改?它是动态的并且不断更新
  • 当更改发生时,在浏览器中触发通知(类似“有一些新闻!”)

这是一些代码(对不起,但我非常喜欢):

var Widget = require("widget").Widget;

var tabs = require('tabs');

exports.main = function() {

      data = require("self").data

        var Panel = require("panel").Panel({
              width:1000,
              height:630,
              contentURL: "http://mysite.com/news.html",
        });

        require("widget").Widget({
              id: "News",
              label: "News",
              contentURL: "http://mysite.com/images/favicon.ico",
              panel: Panel

            });

        };

1 个答案:

答案 0 :(得分:1)

您想要将一些contentScript注入页面(http://mysite.com/news.html)并通过该脚本监视更改。当您看到所需的更改时,只需向面板发送一条消息,通知该更改的附加组件。

我将在文档中添加示例,

// this could be listening to any event happening in the page
var myScript = "window.addEventListener('click', function(event) {" +
               "  var t = event.target;" +
               "  if (t.nodeName == 'A')" +
                    //  this self.port.emit is what sends the 'click-link' signal
                    //  to your add-on code
               "    self.port.emit('click-link', t.toString());" +
               "}, false);"

var panel = require("sdk/panel").Panel({
  contentURL: "http://mysite.com/news.html",
  // !!! use contentScriptFile, this is just for small inline stuff
  contentScript: myScript
});

// this is how your add-on listens for the 'click-link' signal, but it could be any signal name you want
panel.port.on("click-link", function(url) {
  // here is where you fire the "There are some news!" event in the browser
  console.log(url);
});