我正在制作一个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>
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
});
我尝试了以下方法:
- 使用
chrome.scripting.executeScript()
- 使用
eval()
- 使用
chrome.scripting.executeScript()
插入带有函数的脚本标签,然后运行该函数
我刚开始接触Chrome扩展程序开发,可能有些地方没想到,或者这根本不可能。
Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self'
。有没有什么办法可以解决这个问题? - MoodContent-Security-Policy
标头。 - wOxxOm