Search code examples
facebookfirefoxfirefox-addon-sdk

firefox pagemod include sometimes not working?


The piece of code for a firefox pagemod here doesn't work on every facebook page:

var pageMod = require("sdk/page-mod");
pageMod.PageMod({
  include: /.*facebook.*/ ,
  contentScript: "window.alert('Page matches ruleset');",
  contentScriptWhen: 'end'
});

I s there any misinterpretation I have about the include part? I have also tried "*fecebook.com" and still sometimes I don't get the alert command executed

Example: for example for both the above solutions when i manually use the address bar to go to fecebook.com it works but when I use fecebook's home button which again goes to facebook.com it doesn't work


Solution

  • It doesn't hurt to be as precise as you can be with the regular expression you use:

    var pageMod = require("sdk/page-mod");
    pageMod.PageMod({
      include: /^http[s]*\:\/\/.*facebook.com\/.*/,
      contentScript: "window.alert('Page matches ruleset');",
      contentScriptWhen: 'end'
    });
    

    The other factor I noticed when testing this on Facebook.com is that a lot of the time a change in the page url does not necessarily mean the page is being completely reloaded. The contentscript code in your example will only file when the page is initially loaded. If you want to react to changes in the url regardless of page loads, you might need to monitor the html5 history api in your content script and then react to those changes as well.