1

我正在尝试在火狐浏览器中构建附加组件。我使用了一个页面模块,因此在登陆特定页面(Youtube)时会触发一个脚本,该脚本可以将数据传递给主脚本。但它无法与数据评论通信,因为它在页面加载后加载(使用 ajax 或其他东西)。请帮我!我的例子:

main.js

pageMod.PageMod({
  include: "*.youtube.com",
  contentScriptFile: data.url("jquery-1.8.3.js"),
  contentScriptFile: data.url("element-getter.js"),
  contentScriptWhen: "end"          
});

元素getter.js

var divs = document.getElementsByTagName("div");
for (var i = 0; i < divs.length; ++i) {
    divs[i].setAttribute("style", "border: solid red 1px;");
}

你可以看到里面的评论内容

<div id = watch-discussion></div>

非常感谢。

4

2 回答 2

0

使用 setTimer 并检查计时器处理程序中的注释 DOM 节点。完成评论处理后停止计时器。

另一种方法是从页面监听 XMLHTTPRequests 并从这里获取评论

于 2013-09-22T12:09:40.340 回答
0

由于内容是在初始页面加载后加载的,因此您应该使用MutatinObserver api。这将允许您在修改内容时触发回调watch-discussion

以下是我的一个扩展的片段:

// create an observer instance
var observer = new page.defaultView.MutationObserver(function(mutations, obs) {
  stopTube._handleMutation(mutations, obs);
});

// configuration of the observer:
var config = { childList: true};

// pass in the target node, as well as the observer options
// Here instead of page.body, you should be using page.getElementById('watch-discussion'), otherwise you will have too many notifications. 
observer.observe(page.body, config);         

page.addEventListener('unload', function(evt) {
  // remember to stop observing when you are done
  observer.disconnect();

});
于 2013-09-22T13:23:01.893 回答