Files
kiss-translator/src/background.js

266 lines
6.4 KiB
JavaScript
Raw Normal View History

2025-02-18 11:45:57 +08:00
/* global messenger */
2023-08-05 18:15:01 +08:00
import browser from "webextension-polyfill";
2023-07-20 13:45:41 +08:00
import {
MSG_FETCH,
MSG_GET_HTTPCACHE,
2023-08-21 14:03:39 +08:00
MSG_TRANS_TOGGLE,
2023-09-23 19:16:51 +08:00
MSG_OPEN_OPTIONS,
MSG_SAVE_RULE,
2023-08-21 16:06:21 +08:00
MSG_TRANS_TOGGLE_STYLE,
2023-11-22 11:02:48 +08:00
MSG_OPEN_TRANBOX,
2023-12-11 11:25:02 +08:00
MSG_CONTEXT_MENUS,
2024-01-22 13:11:02 +08:00
MSG_COMMAND_SHORTCUTS,
2024-03-14 18:06:28 +08:00
MSG_INJECT_JS,
MSG_INJECT_CSS,
2024-03-26 12:00:09 +08:00
MSG_UPDATE_CSP,
DEFAULT_CSPLIST,
2023-08-21 14:03:39 +08:00
CMD_TOGGLE_TRANSLATE,
2023-08-21 16:06:21 +08:00
CMD_TOGGLE_STYLE,
2023-09-10 13:56:11 +08:00
CMD_OPEN_OPTIONS,
2023-11-22 11:02:48 +08:00
CMD_OPEN_TRANBOX,
2023-07-20 13:45:41 +08:00
} from "./config";
2023-08-30 18:05:37 +08:00
import { getSettingWithDefault, tryInitDefaultData } from "./libs/storage";
2023-08-31 13:38:06 +08:00
import { trySyncSettingAndRules } from "./libs/sync";
import { fetchHandle, getHttpCache } from "./libs/fetch";
2023-08-21 14:03:39 +08:00
import { sendTabMsg } from "./libs/msg";
2023-08-30 18:05:37 +08:00
import { trySyncAllSubRules } from "./libs/subRules";
import { tryClearCaches } from "./libs";
2023-09-23 19:16:51 +08:00
import { saveRule } from "./libs/rules";
2024-03-14 18:06:28 +08:00
import { getCurTabId } from "./libs/msg";
import { injectInlineJs, injectInternalCss } from "./libs/injector";
2024-03-26 12:00:09 +08:00
import { kissLog } from "./libs/log";
2023-07-20 13:45:41 +08:00
2023-09-20 16:02:17 +08:00
globalThis.ContextType = "BACKGROUND";
2024-03-26 12:00:09 +08:00
const REMOVE_HEADERS = [
`content-security-policy`,
`content-security-policy-report-only`,
`x-webkit-csp`,
`x-content-security-policy`,
];
2023-07-20 13:45:41 +08:00
/**
2023-12-11 11:25:02 +08:00
* 添加右键菜单
2023-07-20 13:45:41 +08:00
*/
2025-02-18 12:44:06 +08:00
async function addContextMenus(contextMenuType = 1) {
2024-02-05 10:51:42 +08:00
// 添加前先删除,避免重复ID的错误
try {
2025-02-18 11:42:47 +08:00
await browser.menus.removeAll();
2024-02-05 10:51:42 +08:00
} catch (err) {
//
}
2024-02-05 11:28:34 +08:00
switch (contextMenuType) {
case 1:
2025-02-18 11:42:47 +08:00
browser.menus.create({
2024-02-05 11:28:34 +08:00
id: CMD_TOGGLE_TRANSLATE,
2024-02-21 17:01:11 +08:00
title: browser.i18n.getMessage("app_name"),
2024-02-05 11:28:34 +08:00
contexts: ["page", "selection"],
});
break;
case 2:
2025-02-18 11:42:47 +08:00
browser.menus.create({
2024-02-05 11:28:34 +08:00
id: CMD_TOGGLE_TRANSLATE,
title: browser.i18n.getMessage("toggle_translate"),
contexts: ["page", "selection"],
});
2025-02-18 11:42:47 +08:00
browser.menus.create({
2024-02-05 11:28:34 +08:00
id: CMD_TOGGLE_STYLE,
title: browser.i18n.getMessage("toggle_style"),
contexts: ["page", "selection"],
});
2025-02-18 11:42:47 +08:00
browser.menus.create({
2024-02-05 11:28:34 +08:00
id: CMD_OPEN_TRANBOX,
title: browser.i18n.getMessage("open_tranbox"),
contexts: ["page", "selection"],
});
2025-02-18 11:42:47 +08:00
browser.menus.create({
2024-02-05 11:28:34 +08:00
id: "options_separator",
type: "separator",
contexts: ["page", "selection"],
});
2025-02-18 11:42:47 +08:00
browser.menus.create({
2024-02-05 11:28:34 +08:00
id: CMD_OPEN_OPTIONS,
title: browser.i18n.getMessage("open_options"),
contexts: ["page", "selection"],
});
break;
default:
}
2023-12-11 11:25:02 +08:00
}
2024-03-26 12:00:09 +08:00
/**
* 更新CSP策略
2024-03-26 12:05:35 +08:00
* @param {*} csplist
2024-03-26 12:00:09 +08:00
*/
async function updateCspRules(csplist = DEFAULT_CSPLIST.join(",\n")) {
try {
const newRules = csplist
.split(/\n|,/)
.map((url) => url.trim())
.filter(Boolean)
.map((url, idx) => ({
id: idx + 1,
action: {
type: "modifyHeaders",
responseHeaders: REMOVE_HEADERS.map((header) => ({
operation: "remove",
header,
})),
},
condition: {
urlFilter: url,
resourceTypes: ["main_frame", "sub_frame"],
},
}));
const oldRules = await browser.declarativeNetRequest.getDynamicRules();
const oldRuleIds = oldRules.map((rule) => rule.id);
await browser.declarativeNetRequest.updateDynamicRules({
removeRuleIds: oldRuleIds,
addRules: newRules,
});
} catch (err) {
kissLog(err, "update csp rules");
}
}
2025-02-18 11:42:47 +08:00
/**
* 注册邮件显示脚本
*/
async function registerMsgDisplayScript() {
await messenger.messageDisplayScripts.register({
js: [{file: "/content.js"}]
});
}
2023-12-11 11:25:02 +08:00
/**
* 插件安装
*/
browser.runtime.onInstalled.addListener(() => {
tryInitDefaultData();
2023-12-11 17:26:49 +08:00
2025-02-18 11:42:47 +08:00
//在thunderbird中注册脚本
if (process.env.REACT_APP_CLIENT === "thunderbird") {
registerMsgDisplayScript();
}
2023-12-11 17:26:49 +08:00
// 右键菜单
2025-02-18 12:44:06 +08:00
addContextMenus();
2024-03-26 12:42:39 +08:00
// 禁用CSP
updateCspRules();
2023-07-20 13:45:41 +08:00
});
/**
* 浏览器启动
*/
2025-02-18 12:44:06 +08:00
browser.runtime.onStartup.addListener(async () => {
2023-07-31 03:10:09 +08:00
// 同步数据
2023-09-20 17:47:23 +08:00
await trySyncSettingAndRules();
2023-07-31 03:10:09 +08:00
2024-03-26 12:42:39 +08:00
const { clearCache, contextMenuType, subrulesList, csplist } =
2024-02-05 11:28:34 +08:00
await getSettingWithDefault();
2023-12-11 11:25:02 +08:00
2023-07-31 03:10:09 +08:00
// 清除缓存
2023-12-11 11:25:02 +08:00
if (clearCache) {
2023-08-30 18:05:37 +08:00
tryClearCaches();
2023-07-20 13:45:41 +08:00
}
2025-02-18 11:42:47 +08:00
//在thunderbird中注册脚本
if (process.env.REACT_APP_CLIENT === "thunderbird") {
registerMsgDisplayScript();
}
2023-12-11 11:25:02 +08:00
// 右键菜单
2024-02-05 11:28:34 +08:00
// firefox重启后菜单会消失,故重复添加
2025-02-18 12:44:06 +08:00
addContextMenus(contextMenuType);
2023-12-11 11:25:02 +08:00
2024-03-26 12:05:35 +08:00
// 禁用CSP
2024-03-26 12:42:39 +08:00
updateCspRules(csplist);
2024-03-26 12:05:35 +08:00
// 同步订阅规则
2023-12-11 11:25:02 +08:00
trySyncAllSubRules({ subrulesList });
2023-07-20 13:45:41 +08:00
});
/**
* 监听消息
*/
browser.runtime.onMessage.addListener(async ({ action, args }) => {
switch (action) {
case MSG_FETCH:
return await fetchHandle(args);
case MSG_GET_HTTPCACHE:
const { input, init } = args;
return await getHttpCache(input, init);
case MSG_OPEN_OPTIONS:
return await browser.runtime.openOptionsPage();
case MSG_SAVE_RULE:
return await saveRule(args);
case MSG_INJECT_JS:
return await browser.scripting.executeScript({
target: { tabId: await getCurTabId(), allFrames: true },
func: injectInlineJs,
args: [args],
world: "MAIN",
});
case MSG_INJECT_CSS:
return await browser.scripting.executeScript({
target: { tabId: await getCurTabId(), allFrames: true },
func: injectInternalCss,
args: [args],
world: "MAIN",
});
2024-03-26 12:00:09 +08:00
case MSG_UPDATE_CSP:
return await updateCspRules(args);
case MSG_CONTEXT_MENUS:
2025-02-18 12:44:06 +08:00
return await addContextMenus(args);
case MSG_COMMAND_SHORTCUTS:
return await browser.commands.getAll();
default:
throw new Error(`message action is unavailable: ${action}`);
2023-07-20 13:45:41 +08:00
}
});
2023-08-21 14:03:39 +08:00
/**
* 监听快捷键
*/
browser.commands.onCommand.addListener((command) => {
// console.log(`Command: ${command}`);
switch (command) {
case CMD_TOGGLE_TRANSLATE:
sendTabMsg(MSG_TRANS_TOGGLE);
break;
case CMD_OPEN_TRANBOX:
sendTabMsg(MSG_OPEN_TRANBOX);
break;
2023-08-21 16:06:21 +08:00
case CMD_TOGGLE_STYLE:
sendTabMsg(MSG_TRANS_TOGGLE_STYLE);
break;
2023-09-10 13:56:11 +08:00
case CMD_OPEN_OPTIONS:
browser.runtime.openOptionsPage();
break;
2023-08-21 14:03:39 +08:00
default:
}
});
2023-11-21 11:20:05 +08:00
2023-12-11 11:25:02 +08:00
/**
* 监听右键菜单
*/
2025-02-18 12:44:06 +08:00
browser.contextMenus.onClicked.addListener(({ menuItemId }) => {
2023-11-21 11:20:05 +08:00
switch (menuItemId) {
case CMD_TOGGLE_TRANSLATE:
sendTabMsg(MSG_TRANS_TOGGLE);
break;
case CMD_TOGGLE_STYLE:
sendTabMsg(MSG_TRANS_TOGGLE_STYLE);
break;
2023-11-22 11:02:48 +08:00
case CMD_OPEN_TRANBOX:
sendTabMsg(MSG_OPEN_TRANBOX);
break;
2023-11-21 11:20:05 +08:00
case CMD_OPEN_OPTIONS:
browser.runtime.openOptionsPage();
break;
default:
}
});