Firefox插件SDK页面mod匹配模式/通配符错误

Firefox Add-on-SDK page-mod Matchpattern/Wildcard Error

本文关键字:模式 通配符 错误 插件 SDK 页面 mod Firefox      更新时间:2023-09-26

我需要在我的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中。谢谢。

页面mod文档指出includeexclude属性可以是字符串(带有限通配符)、RegExp(正则表达式)或这些类型的数组中的任何一个。您可以在描述匹配模式的MDN页面上获得更详细的匹配模式描述。因此,为了匹配您想要的内容,您可以在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只是另一种类型的标准内置对象。它们可以作为文本在代码中输入。您也可以使用构造函数:
var myRegularExpression = new RegExp(pattern [, flags]);

例如:

var excludedPages = /http:'/'/forum'.blockland'.us'/.*sa=.*/;

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