处理 gm add element csp问题

This commit is contained in:
2025-04-14 16:24:04 +08:00
parent 1965137191
commit 3b2e72127f
4 changed files with 43 additions and 15 deletions

View File

@ -60,9 +60,7 @@ export default class ContentRuntime {
parentNode = (this.msg as CustomEventMessage).getAndDelRelatedTarget(parentNodeId);
}
const el = <Element>document.createElement(tagName);
Object.keys(attr).forEach((key) => {
el.setAttribute(key, attr[key]);
});
let textContent = "";
if (attr) {
if (attr.textContent) {
@ -72,6 +70,9 @@ export default class ContentRuntime {
} else {
attr = {};
}
Object.keys(attr).forEach((key) => {
el.setAttribute(key, attr[key]);
});
if (textContent) {
el.innerHTML = textContent;
}

View File

@ -247,6 +247,10 @@ export class RuntimeService {
registerInjectScript() {
chrome.userScripts.getScripts({ ids: ["scriptcat-inject"] }).then((res) => {
if (res.length == 0) {
chrome.userScripts.configureWorld({
csp: "script-src 'self' 'unsafe-inline' 'unsafe-eval' *",
messaging: true,
});
fetch("inject.js")
.then((res) => res.text())
.then(async (injectJs) => {
@ -261,18 +265,17 @@ export class RuntimeService {
world: "MAIN",
runAt: "document_start",
},
// 注册content
{
id: "scriptcat-content",
js: [{ file: "src/content.js" }],
matches: ["<all_urls>"],
allFrames: true,
runAt: "document_start",
world: "USER_SCRIPT",
},
]);
});
chrome.scripting.registerContentScripts([
{
id: "scriptcat-content",
js: ["src/content.js"],
matches: ["<all_urls>"],
allFrames: true,
runAt: "document_start",
world: "ISOLATED",
},
]);
}
});
}
@ -426,7 +429,6 @@ export class RuntimeService {
if (script.metadata["run-at"]) {
registerScript.runAt = getRunAt(script.metadata["run-at"]);
}
console.log("registerScript", script.name, registerScript, scriptMatchInfo);
if (await Cache.getInstance().get("registryScript:" + script.uuid)) {
await chrome.userScripts.update([registerScript]);
} else {