Firefox Addon Pagemod 未针对 youtube 执行

Firefox Addon Pagemod not executed for youtube

我正在构建一个与 youtube 网站交互的小插件。 为了在页面内注入自定义脚本,我使用方便的 page-mod 像这样:

var pageMod = require("sdk/page-mod");

pageMod.PageMod({
  include: "*",
  contentScript: "window.alert('injected');",
  contentScriptWhen : 'start',
  attachTo: ["existing", "top"],
  onAttach: function(worker) {
    console.log(worker.tab.url);
  }
});

当我浏览页面时,每次加载新页面时都会显示 'injected' 消息。但是当谈到youtube时,我没有结果。

我访问过的 url 是有序的:

我注意到当从视频切换到视频时 url 发生了变化,但网页似乎没有重新加载...

当我手动重新加载 YouTube 视频(cmd+rf5)时,我能够在 YouTube 视频上收到 injected 消息。

当我搜索时,我发现 this article on page-mod's attachTo 这可能是一个解决方案,但是带有 attachTo: ["existing", "top"] 行的事件,结果是相同的...

你有什么想法吗?

谢谢

YouTube 使用 JS 更改其网站的内容。我不确定当 youtube 更改位置时是否会触发任何事件。我知道的唯一事件是 popstate,它会在用户使用后退或前进按钮导航时触发(参见 https://developer.mozilla.org/en-US/docs/Web/Events/popstate)。

在查看另一个 firefox 扩展库时,我找到了问题的解决方案:

background.js

pageMod.PageMod({
  include : youtubeRegex,
  contentScriptFile : 'permanent.js',
  onAttach : function(worker){
    worker.port.on('update', function(){
      console.log('update from contentscript');
      worker.port.emit('include'); // call the script update
    });
  },
  contentScriptWhen : 'start',
});

permanent.js

// 1st line of the file
self.port.emit("update");

// stuff called once
// ...

// stuff called on each udpate
self.port.on('include', function(){
  window.alert('injected');
});

这样即使在 youtube 网站上我也能得到 injected

希望对您有所帮助:)