feat: inject user js/css

This commit is contained in:
Gabe Yuan
2024-03-14 18:06:28 +08:00
parent 9d4c596b4b
commit 2eabb7d5ac
10 changed files with 213 additions and 7 deletions

View File

@@ -10,6 +10,8 @@ import {
MSG_OPEN_TRANBOX,
MSG_CONTEXT_MENUS,
MSG_COMMAND_SHORTCUTS,
MSG_INJECT_JS,
MSG_INJECT_CSS,
CMD_TOGGLE_TRANSLATE,
CMD_TOGGLE_STYLE,
CMD_OPEN_OPTIONS,
@@ -22,6 +24,8 @@ import { sendTabMsg } from "./libs/msg";
import { trySyncAllSubRules } from "./libs/subRules";
import { tryClearCaches } from "./libs";
import { saveRule } from "./libs/rules";
import { getCurTabId } from "./libs/msg";
import { injectInlineJs, injectInternalCss } from "./libs/injector";
globalThis.ContextType = "BACKGROUND";
@@ -139,6 +143,40 @@ browser.runtime.onMessage.addListener(
case MSG_SAVE_RULE:
saveRule(args);
break;
case MSG_INJECT_JS:
getCurTabId()
.then((tabId) =>
browser.scripting.executeScript({
target: { tabId: tabId, allFrames: true },
func: injectInlineJs,
args: [args],
world: "MAIN",
})
)
.then(() => {
// skip
})
.catch((error) => {
sendResponse({ error: error.message });
});
break;
case MSG_INJECT_CSS:
getCurTabId()
.then((tabId) =>
browser.scripting.executeScript({
target: { tabId: tabId, allFrames: true },
func: injectInternalCss,
args: [args],
world: "MAIN",
})
)
.then(() => {
// skip
})
.catch((error) => {
sendResponse({ error: error.message });
});
break;
case MSG_CONTEXT_MENUS:
const { contextMenuType } = args;
addContextMenus(contextMenuType);