2015-03-02 72 views
0

根據MDN的頁面page-worker API,我應該能夠使用Reg-ex作爲我函數的include部分的參數。FireFox addon-sdk page-worker正則表達式

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

這段代碼的作用是,當一個標籤加載在瀏覽器中,它應該檢查URL,如果它具有谷歌作爲它的一部分,我應該ContentScript運行。問題是,我碰到下面的錯誤..

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

我爲什麼不能使用REG-EX作爲我的論點?該API明確指出Reg-ex是允許的。

回答

1

它看起來像文檔不匹配page-worker的初始化代碼,我認爲這個錯誤是它只檢查字符串或數組。我發現,如果你在一個數組封裝正則表達式對象,這個工程:

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

我登錄了這個bug跟蹤問題:https://bugzilla.mozilla.org/show_bug.cgi?id=1138545

+0

優秀。非常感謝,這解決了它。 – Shadowvail 2015-03-03 14:28:36