Files
kiss-translator/src/common.js

177 lines
4.1 KiB
JavaScript
Raw Normal View History

2025-10-21 02:07:33 +08:00
import { OPT_HIGHLIGHT_WORDS_DISABLE } from "./config";
import {
getFabWithDefault,
getSettingWithDefault,
getWordsWithDefault,
} from "./libs/storage";
2025-10-21 02:07:33 +08:00
import { isIframe } from "./libs/iframe";
import { genEventName } from "./libs/utils";
2023-11-24 17:07:29 +08:00
import { handlePing, injectScript } from "./libs/gm";
import { matchRule } from "./libs/rules";
import { trySyncAllSubRules } from "./libs/subRules";
import { isInBlacklist } from "./libs/blacklist";
2025-10-07 16:35:00 +08:00
import { runSubtitle } from "./subtitle/subtitle";
import { logger } from "./libs/log";
import { injectInlineJs } from "./libs/injector";
2025-10-21 02:07:33 +08:00
import TranslatorManager from "./libs/translatorManager";
2023-10-23 13:35:57 +08:00
2023-11-24 17:07:29 +08:00
/**
* 油猴脚本设置页面
*/
function runSettingPage() {
if (GM?.info?.script?.grant?.includes("unsafeWindow")) {
unsafeWindow.GM = GM;
unsafeWindow.APP_INFO = {
name: process.env.REACT_APP_NAME,
version: process.env.REACT_APP_VERSION,
};
} else {
const ping = genEventName();
window.addEventListener(ping, handlePing);
// window.eval(`(${injectScript})("${ping}")`); // eslint-disable-line
injectInlineJs(
`(${injectScript})("${ping}")`,
"kiss-translator-options-injector"
);
2023-11-24 17:07:29 +08:00
}
}
2023-10-23 13:35:57 +08:00
2023-11-24 17:07:29 +08:00
/**
* 显示错误信息到页面顶部
* @param {*} message
*/
function showErr(message) {
2025-10-01 11:18:41 +08:00
const bannerId = "KISS-Translator-Message";
const existingBanner = document.getElementById(bannerId);
if (existingBanner) {
existingBanner.remove();
}
const banner = document.createElement("div");
banner.id = bannerId;
Object.assign(banner.style, {
position: "fixed",
top: "0",
left: "0",
width: "100%",
backgroundColor: "#f44336",
color: "white",
textAlign: "center",
padding: "8px 16px",
zIndex: "1001",
boxSizing: "border-box",
fontSize: "16px",
boxShadow: "0 2px 5px rgba(0,0,0,0.2)",
});
const closeButton = document.createElement("span");
2025-10-14 23:36:28 +08:00
closeButton.textContent = "×";
2025-10-01 11:18:41 +08:00
Object.assign(closeButton.style, {
position: "absolute",
top: "50%",
right: "20px",
transform: "translateY(-50%)",
cursor: "pointer",
fontSize: "22px",
fontWeight: "bold",
});
const messageText = document.createTextNode(`KISS-Translator: ${message}`);
banner.appendChild(messageText);
banner.appendChild(closeButton);
document.body.appendChild(banner);
const removeBanner = () => {
banner.style.transition = "opacity 0.5s ease";
banner.style.opacity = "0";
setTimeout(() => {
if (banner && banner.parentNode) {
banner.parentNode.removeChild(banner);
}
}, 500);
};
closeButton.onclick = removeBanner;
setTimeout(removeBanner, 10000);
2023-10-23 13:35:57 +08:00
}
2023-11-11 16:59:38 +08:00
2025-10-21 02:07:33 +08:00
async function getFavWords(rule) {
if (
rule.highlightWords &&
rule.highlightWords !== OPT_HIGHLIGHT_WORDS_DISABLE
) {
try {
return Object.keys(await getWordsWithDefault());
} catch (err) {
logger.info("get fav words", err);
}
2023-11-11 16:59:38 +08:00
}
2025-10-21 02:07:33 +08:00
return [];
2023-11-11 16:59:38 +08:00
}
2023-11-24 17:07:29 +08:00
/**
* 入口函数
*/
export async function run(isUserscript = false) {
try {
if (document?.documentElement?.tagName?.toUpperCase() !== "HTML") {
return;
}
// 读取设置信息
const setting = await getSettingWithDefault();
// 日志
logger.setLevel(setting.logLevel);
2023-11-24 17:07:29 +08:00
const href = document.location.href;
// 设置页面
if (
isUserscript &&
(href.includes(process.env.REACT_APP_OPTIONSPAGE_DEV) ||
2024-04-12 14:33:29 +08:00
href.includes(process.env.REACT_APP_OPTIONSPAGE))
2023-11-24 17:07:29 +08:00
) {
runSettingPage();
return;
}
// 黑名单
if (isInBlacklist(href, setting)) {
return;
}
// 翻译网页
const rule = await matchRule(href, setting);
2025-10-21 02:07:33 +08:00
const favWords = await getFavWords(rule);
const fabConfig = await getFabWithDefault();
const translatorManager = new TranslatorManager({
setting,
2025-10-21 02:07:33 +08:00
rule,
fabConfig,
favWords,
2025-10-21 02:07:33 +08:00
isIframe,
isUserscript,
});
2025-10-21 02:07:33 +08:00
translatorManager.start();
2023-11-24 17:07:29 +08:00
2024-02-22 17:26:22 +08:00
if (isIframe) {
return;
}
2025-10-07 16:35:00 +08:00
// 字幕翻译
2025-10-14 22:41:18 +08:00
runSubtitle({ href, setting, rule, isUserscript });
2025-10-07 16:35:00 +08:00
2025-10-21 02:07:33 +08:00
if (isUserscript) {
trySyncAllSubRules(setting);
}
2023-11-24 17:07:29 +08:00
} catch (err) {
console.error("[KISS-Translator]", err);
showErr(err.message);
}
}