我需要在我的 pagemod 中排除 http://forum.blockland.us/*sa=*
,
但是总是出现这个错误:
Error: There can be at most one '*' character in a wildcard.
这是我的 main.js:
var pageMod = require("sdk/page-mod");
pageMod.PageMod({
include: "http://forum.blockland.us/index.php?action=profile*",
exclude: "http://forum.blockland.us/*sa=*",
contentScript: 'document.body.innerHTML = ' +
' "<h1>Page matches ruleset</h1>";'
});
这似乎是 *sa=*
导致了错误。我不知道如何解决这个问题。
如果答案是有一个正则表达式或匹配模式,我想知道如何将其包含在我的 main.js 中。谢谢。
最佳答案
page-mod文档表明 include
和 exclude
属性可以是任何字符串(带有有限的通配符)、RegExp ( regular expression ),或这些类型的数组。您可以获取 match-patterns on the MDN page describing them 更详细的说明。因此,为了匹配您想要的内容,您可以在 exclude
属性中使用正则表达式,而不是带有通配符的字符串文字:
var pageMod = require("sdk/page-mod");
pageMod.PageMod({
include: "http://forum.blockland.us/index.php?action=profile*",
exclude: /http:\/\/forum\.blockland\.us\/.*sa=.*/,
contentScript: 'document.body.innerHTML = ' +
' "<h1>Page matches ruleset</h1>";'
});
从上面可以看出,RegExp ,只是 standard built-in object 的另一种类型。它们可以作为文字输入到您的代码中。您还可以使用构造函数:
var myRegularExpression = new RegExp(pattern [, flags]);
例如:
var exceptedPages =/http:\/\/forum\.blockland\.us\/.*sa=.*/;
或
var exceptedPages = new RegExp("http:\\/\\/forum\\.blockland\\.us\\/.*sa=.*");
请注意,当表示将用作 new RegExp()
构造函数的输入的字符串时,如果该字符串在源代码中表示,则需要使用双反斜杠“\”。这是因为当您的代码被解释时,文本转换为 String
文字使用 \
来指示下一个字符是特殊的。因此,需要一个双反斜杠 \\
来指示实际的反斜杠 \
应该位于 String
中。这将导致:
var pageMod = require("sdk/page-mod");
var excludedPages = /http:\/\/forum\.blockland\.us\/.*sa=.*/;
pageMod.PageMod({
include: "http://forum.blockland.us/index.php?action=profile*",
exclude: excludedPages,
contentScript: 'document.body.innerHTML = ' +
' "<h1>Page matches ruleset</h1>";'
});
关于javascript - Firefox Add-on-SDK page-mod 匹配模式/通配符错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26245973/