Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Chrome扩展:将用户输入字符串插入多个DeclarativeNet请求regexSubstitution规则_Javascript_Json_Google Chrome_Google Chrome Extension_Google Chrome App - Fatal编程技术网

Javascript Chrome扩展:将用户输入字符串插入多个DeclarativeNet请求regexSubstitution规则

Javascript Chrome扩展:将用户输入字符串插入多个DeclarativeNet请求regexSubstitution规则,javascript,json,google-chrome,google-chrome-extension,google-chrome-app,Javascript,Json,Google Chrome,Google Chrome Extension,Google Chrome App,我有一个扩展,用字符串WTTDOTM替换URL中的UTM参数。它通过拥有多个规则集来工作,每个规则集都有一个单独的regexSubstitution规则。我这样做是因为,如果我有多个规则试图在同一个规则集中执行正则表达式替换,那么似乎只有一个规则执行(这在某种程度上是相关的) 这里有一条规则我想知道是否有一种方法可以收集用户输入并获得输入的json引用,以便将规则更改为“\\1[用户输入]\\2”,而不是“\\1WTTDOTM\\2” utmsource.json [ { "i

我有一个扩展,用字符串
WTTDOTM
替换URL中的UTM参数。它通过拥有多个规则集来工作,每个规则集都有一个单独的regexSubstitution规则。我这样做是因为,如果我有多个规则试图在同一个规则集中执行正则表达式替换,那么似乎只有一个规则执行(这在某种程度上是相关的)

这里有一条规则我想知道是否有一种方法可以收集用户输入并获得输入的json引用,以便将规则更改为“\\1[用户输入]\\2”,而不是“\\1WTTDOTM\\2”

utmsource.json

[
{
    "id": 1,
    "priority": 1,
    "action": {
      "type": "redirect",
      "redirect": { "regexSubstitution": "\\1WTTDOTM\\2" }
    },
    "condition": {
      "regexFilter": "(.*?utm_source=).+?($|\\&.*)",
      "resourceTypes": ["main_frame"]
    }
  }
]
[
{
    "id": 1,
    "priority": 1,
    "action": {
      "type": "redirect",
      "redirect": { "regexSubstitution": "\\1Fuck%20Off%20My%20Data\\2" }
    },
    "condition": {
      "regexFilter": "(.*?utm_source=).+?($|\\&.*)",
      "resourceTypes": ["main_frame"]
    }
  }
]
manifest.json(简化的w/o额外规则,它们都遵循上述格式,只是utm_medium/campaign/term/content替换了utm_source:

{
    "manifest_version": 3,
    "name": "CustomUTM",
    "version": "1.0",
    "description": "Custom UTM Extension",
    "declarative_net_request": {
    "rule_resources": [
      {
        "id": "ruleset_1",
        "enabled": true,
        "path": "utmsource.json"
      }
    ]
  },
  "host_permissions": ["*://*/*"],
  "permissions": ["declarativeNetRequest", "declarativeNetRequestFeedback"],
    "icons": {
          "128": "icon128.png" }
}
我知道我可以使用updateDynamicRules创建动态规则,但似乎所有动态规则都存在于同一个规则集中,这与前面的多个regexSubstitution冲突有关,因为除非有办法生成多个不同的动态规则集,否则实际上只会执行一个替换

任何帮助都将不胜感激

编辑----

下面是一个包含多个regexSubstitution规则的json规则集示例:

对于示例输入
https://about.fb.com/regulations/?utm_source=programmatic&utm_medium=paid-非平台和utm_术语=填充术语和utm_内容=填充内容

实际输出为
https://about.fb.com/regulations/?utm_source=programmatic&utm_medium=paid-offplatform&utm\u term=fillerterm&utm\u content=WTTDOTM

而所需的输出是
https://about.fb.com/regulations/?utm_source=WTTDOTM&utm_medium=paid-WTTDOTM&utm_术语=WTTDOTM&utm_内容=WTTDOTM

它总是只完成具有最大优先级的规则#。这就是为什么我关心updateDynamicRules,除非我可以使用一些奇怪的正则表达式将这4条规则压缩为1进行替换

concurrendruleset.json

[
{
    "id": 1,
    "priority": 1,
    "action": {
      "type": "redirect",
      "redirect": { "regexSubstitution": "\\1WTTDOTM\\2" }
    },
    "condition": {
      "regexFilter": "(.*?utm_source=).+?($|\\&.*)",
      "resourceTypes": ["main_frame"]
    }
  },
{
    "id": 2,
    "priority": 2,
    "action": {
      "type": "redirect",
      "redirect": { "regexSubstitution": "\\1WTTDOTM\\2" }
    },
    "condition": {
      "regexFilter": "(.*?utm_medium=).+?($|\\&.*)",
      "resourceTypes": ["main_frame"]
    }
  },
  {
    "id": 3,
    "priority": 3,
    "action": {
      "type": "redirect",
      "redirect": { "regexSubstitution": "\\1WTTDOTM\\2" }
    },
    "condition": {
      "regexFilter": "(.*?utm_term=).+?($|\\&.*)",
      "resourceTypes": ["main_frame"]
    }
  },
  {
    "id": 4,
    "priority": 4,
    "action": {
      "type": "redirect",
      "redirect": { "regexSubstitution": "\\1WTTDOTM\\2" }
    },
    "condition": {
      "regexFilter": "(.*?utm_content=).+?($|\\&.*)",
      "resourceTypes": ["main_frame"]
    }
  }
]
然而,如果我这样做多个规则集,我会得到所需的输出:

utmsource.json

[
{
    "id": 1,
    "priority": 1,
    "action": {
      "type": "redirect",
      "redirect": { "regexSubstitution": "\\1WTTDOTM\\2" }
    },
    "condition": {
      "regexFilter": "(.*?utm_source=).+?($|\\&.*)",
      "resourceTypes": ["main_frame"]
    }
  }
]
[
{
    "id": 1,
    "priority": 1,
    "action": {
      "type": "redirect",
      "redirect": { "regexSubstitution": "\\1Fuck%20Off%20My%20Data\\2" }
    },
    "condition": {
      "regexFilter": "(.*?utm_source=).+?($|\\&.*)",
      "resourceTypes": ["main_frame"]
    }
  }
]
utmmedium.json

[
  {
    "id": 1,
    "priority": 1,
    "action": {
      "type": "redirect",
      "redirect": { "regexSubstitution": "\\1Fuck%20Off%20My%20Data\\2" }
    },
    "condition": {
      "regexFilter": "(.*?utm_medium=).+?($|\\&.*)",
      "resourceTypes": ["main_frame"]
    }
  }
]
依此类推。此清单如下所示:

manifest.json

{
    "manifest_version": 3,
    "name": "CustomUTM",
    "version": "1.3",
    "description": "Custom UTM Extension",
    "declarative_net_request": {
    "rule_resources": [
      {
        "id": "ruleset_1",
        "enabled": true,
        "path": "utmsource.json"
      },
      {
        "id": "ruleset_2",
        "enabled": true,
        "path": "utmmedium.json"
      },
      {
        "id": "ruleset_3",
        "enabled": true,
        "path": "utmcontent.json"
      },
      {
        "id": "ruleset_4",
        "enabled": true,
        "path": "utmcampaign.json"
      },
      {
        "id": "ruleset_5",
        "enabled": true,
        "path": "utmterm.json"
      }
    ]
  },
  "host_permissions": ["*://*/*"],
  "permissions": ["declarativeNetRequest", "declarativeNetRequestFeedback"],
    "icons": {
          "128": "icon128.png" }
}

对查询部分使用
transform
操作,而不是
regexSubstitution
,以及匹配所有参数的条件

“条件”:{
“regexFilter”:“[?&]utm|(源|中|项|内容)=[^&]+”,
“资源类型”:[“主框架”]
},
“行动”:{
“类型”:“重定向”,
“重定向”:{
“转变”:{
“查询转换”:{
“addOrReplaceParams”:[
{“key”:“utm_source”,“value”:“foo”},
{“key”:“utm_medium”,“value”:“foo”},
{“key”:“utm_term”,“value”:“foo”},
{“键”:“utm_内容”,“值”:“foo”}
]
}
}
}
}

“在同一规则集中,似乎只有一个规则执行”-技术上不应该有这样的限制,所以我猜这是因为您的regexFilter匹配相同的URL,但我希望至少看到两个这样的规则集。无论如何,唯一的解决方案是updateDynamicRules。感谢@wOxxOm,我在一个示例中添加了一个压缩规则集,它不起作用,并且相同的规则在多个规则集上出现我想要么我需要弄清楚如何创建一个正则表达式规则来同时完成所有这些规则集,要么我需要弄清楚如何创建彼此不冲突的动态规则。