小编Moo*_*ood的帖子

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

我正在制作一个扩展,将用户提供的脚本注入当前网站。我已经完成了这部分(在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"); …
Run Code Online (Sandbox Code Playgroud)

javascript google-chrome javascript-injection google-chrome-extension

9
推荐指数
1
解决办法
1869
查看次数

Chrome 扩展:如何注入用户提供的脚本?

我正在为 chrome 制作一个扩展,用户可以在其中输入脚本,然后按“运行”将其注入到当前选项卡中。我正在使用 MV3(清单 v3)。有什么方法可以做到这一点吗?

我的代码:

HTML:

<div class="scriptrunner">
    <h1>Script Runner</h1>
    <textarea placeholder="Enter script here" id="script"></textarea>
    <button id="run">Run Script</button>
</div>
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;
    // this is where the script would be ran
});
Run Code Online (Sandbox Code Playgroud)

我尝试过以下方法:

  • 使用chrome.scripting.executeScript()
  • 使用eval()
  • 用于chrome.scripting.executeScript()插入带有函数的脚本标签,然后运行该函数

我刚刚开始研究 chrome 扩展,所以也许我错过了一些东西,或者这是不可能的。

javascript google-chrome google-chrome-extension chrome-extension-manifest-v3

6
推荐指数
1
解决办法
3555
查看次数