Chrome 扩展:如何使用 declarativeNetRequest 绕过内容安全策略

Moo*_*ood 9 javascript google-chrome javascript-injection google-chrome-extension

我正在制作一个扩展,将用户提供的脚本注入当前网站。我已经完成了这部分(在wOxxOm的帮助下)。唯一的问题是在某些网站上它不起作用。它在控制台中抛出此错误:Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self'。我一直在尝试使用 declarativeNetRequest 修复此问题,但它不起作用。

规则1.json

[
    {
        "id": 1,
        "priority": 1,
        "action": {
            "type": "modifyHeaders",
            "responseHeaders": [
                {
                    "header": "content-security-policy",
                    "operation": "remove"
                }
            ]
        },
        "condition": {
            "urlFilter": "*://*/*",
            "resourceTypes": ["main_frame"]
        }
    }
]
Run Code Online (Sandbox Code Playgroud)

清单.json

{
    ...
    "permissions": ["scripting", "activeTab", "declarativeNetRequest"],
    ...
    "declarative_net_request": {
        "rule_resources": [
            {
                "id": "ruleset_1",
                "enabled": true,
                "path": "/rules/rule1.json"
            }
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

JavaScript

let button = document.getElementById("run");
button.addEventListener("click", async () => {
    let input = document.getElementById("script");
    let script = input.value;
    await execInPage(script);
});
async function execInPage(code) {
    const [tab] = await chrome.tabs.query({ currentWindow: true, active: true });
    chrome.scripting.executeScript({
        target: { tabId: tab.id },
        func: (code) => {
            const el = document.createElement("script");
            el.textContent = code;
            document.head.appendChild(el);
        },
        args: [code],
        world: "MAIN",
    });
}
Run Code Online (Sandbox Code Playgroud)

我正在使用清单 v3。该扩展尚未发布。我现在使用的是开发者模式。

小智 -1

它似乎不适用于使用该元素设置 CSP 的网站。有人有其他解决方案吗?