【问题标题】:Firefox Addon Pagemod not executed for youtube没有为 youtube 执行 Firefox Addon Pagemod
【发布时间】:2015-04-06 23:51:47
【问题描述】:

我正在构建一个与 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);
  }
});

当我浏览页面时,每次加载新页面时都会显示“已注入”消息。但是当谈到 youtube 时,我没有得到任何结果。

我访问的网址是有序的:

  • https://www.google.com -> injected
  • https://www.google.com/?q=youtube -> injected
  • https://www.youtube.com -> injected
  • https://www.youtube.com/watch?v=lnUeovQ68Tw -> 没有
  • https://www.youtube.com/results?search_query=dark+side+of+the+moon -> 没什么
  • 现在,如果我留在 Youtube 上,我永远不会收到 injected 消息...

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

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

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

你有什么想法吗?

谢谢

【问题讨论】:

    标签: javascript firefox firefox-addon-sdk


    【解决方案1】:

    在查看另一个 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

    希望它会有所帮助:)

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 很有趣,但同时我能够找到一种方法,如果您有兴趣,请查看我对问题的回答,无论如何谢谢:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多