nobe4
nobe4

Reputation: 2842

Firefox Addon Pagemod not executed for youtube

I am building a small add-on that interacts with the youtube website. For injecting a custom script inside the page I use the convenient page-mod like so :

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);
  }
});

When I navigate through pages, the 'injected' message shows up each time a new page is loaded. But when it comes to youtube I got no result.

My visited urls are in order :

I noticed that when changing from video to video the url change but the webpage doesn't seem to reload...

I was able to get the injected message on a youtube video when I manually reload it (cmd+r or f5).

As I searched I found this article on page-mod's attachTo which could have been a solution, but event with the attachTo: ["existing", "top"] line, the result was the same...

Do you have any idea ?

Thanks

Upvotes: 2

Views: 86

Answers (2)

nobe4
nobe4

Reputation: 2842

While looking in another firefox extension lib I found a solution to my question :

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');
});

This way I get a injected even in the youtube website.

Hope it will help :)

Upvotes: 3

humanoid
humanoid

Reputation: 764

YouTube changes the content of their site using JS. I am not sure if there are any events fired when youtube changes the location. The only event I know of is popstate, which fires, whenever the user navigates using the back or forward buttons (see https://developer.mozilla.org/en-US/docs/Web/Events/popstate).

Upvotes: 1

Related Questions