开发者问题收集

Chrome 扩展程序-declarativeNetRequest 删除 requestHeader“origin”不起作用

2021-11-12
2146

我尝试使用 declarativeNetRequest 删除 requestHeader“origin”。它不起作用,因为源仍在通过 SharePoint rest api 调用发送。我们如何确保规则是否被触发?我们如何解决问题?

这是我的 manifest.json 和 rules.json

{
  "short_name": "SPO Helper",
  "name": "SPO Helper",
  "icons": {
    "16": "favicon.ico",
    "48": "logo192.png",
    "128": "logo512.png"
  },
  "manifest_version": 3,
  "version": "0.0.1",
  "background": {
    "service_worker": "./static/js/background.js"
  },
  "content_scripts": [
    {
      "matches": [
        "<all_urls>"
      ],
      "js": [
        "content.js"
      ],
      "all_frames": false,
      "run_at": "document_end"
    }
  ],
  "action": {    
    "default_title": "SPO Helper"
  },
  "declarative_net_request": {
    "rule_resources": [
      {
        "id": "ruleset_1",
        "enabled": true,
        "path": "./rules.json"
      }
    ]
  },
  "permissions": [
    "tabs",
    "activeTab",
    "cookies",
    "scripting",
    "declarativeNetRequest",
    "declarativeNetRequestFeedback"
  ],
  "host_permissions": [
    "https://*.sharepoint.com/"
  ],
  "content_security_policy": {
    "extension_pages": "script-src 'self'; object-src 'self'"
  }
}

rules.json

[
    {
        "id": 1,
        "priority": 1,
        "action": {
            "type": "modifyHeaders",
            "requestHeaders": [
                {
                    "header": "origin",
                    "operation": "remove"
                }
            ]
        },
        "condition" : {
            "domains": ["cbgbfoeehbjllcimibeojmpgeoncgjcl"],
            "resourceTypes" : ["main_frame", "sub_frame"]
          }
    }
]
1个回答

使用 onRuleMatchedDebug ,您可以检查您的规则是否被触发。

您必须在 manifest.json 中添加 declarativeNetRequestFeedback 权限,并将其添加到您的服务工作线程中:

chrome.declarativeNetRequest.onRuleMatchedDebug.addListener(function (o) {
  console.log('rule matched:', o);
});
Patricio Jofre
2022-02-26