Search code examples
javascriptfirefox-addon-sdk

FireFox addon-sdk page-worker regex


According to MDN's page page-worker API, I should be able to use a Reg-ex as an argument to the include section of my function.

var data = require("sdk/self").data;
var page = require("sdk/page-worker");
var re = new RegExp("/.*google.*/");

function doTheThing(){
    page.Page({
        include: re,
        contentURL: data.url("webPage.html"),
        contentScriptWhen: "ready",
        contentScript: 'document.body.style.border = "5px solid red";'
    })
}

What this code does is that when a tab loads in the browser, it should check the url and if it has google as part of it, my ContentScript should run. The problem is that I get the following error..

 Message: RequirementError: The option "include" must be one of the following types: string, array, undefined

Why can I not use a Reg-ex as my argument? The API clearly states that Reg-ex are allowed.


Solution

  • It looks like the docs don't match the initialization code for page-worker, and I think the bug is that it checks only for strings or arrays. I found that if you enclose the regex object in an array, this works:

    var data = require("sdk/self").data;
    var page = require("sdk/page-worker");
    var re = new RegExp("/.*google.*/");
    
    function doTheThing(){
        page.Page({
            include: [re], // only accepts regex objects in an array
            contentURL: data.url("webPage.html"),
            contentScriptWhen: "ready",
            contentScript: 'document.body.style.border = "5px solid red";'
        })
    }
    

    I've logged this bug to track the issue: https://bugzilla.mozilla.org/show_bug.cgi?id=1138545