fix: hooks & injectjs
This commit is contained in:
@@ -13,6 +13,18 @@ export const injectInlineJs = (code, id = "kiss-translator-inline-js") => {
|
||||
(document.head || document.documentElement).appendChild(el);
|
||||
};
|
||||
|
||||
export const injectInlineJsBg = (code, id = "kiss-translator-inline-js") => {
|
||||
if (document.getElementById(id)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const el = document.createElement("script");
|
||||
el.type = "text/javascript";
|
||||
el.id = id;
|
||||
el.textContent = code;
|
||||
(document.head || document.documentElement).appendChild(el);
|
||||
};
|
||||
|
||||
// Function to inject external JavaScript file
|
||||
export const injectExternalJs = (src, id = "kiss-translator-external-js") => {
|
||||
if (document.getElementById(id)) {
|
||||
|
||||
@@ -193,7 +193,7 @@ export class InputTranslator {
|
||||
try {
|
||||
addLoading(node, loadingId);
|
||||
|
||||
const [trText, isSame] = await apiTranslate({
|
||||
const { trText, isSame } = await apiTranslate({
|
||||
text,
|
||||
fromLang,
|
||||
toLang,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import Sval from "sval";
|
||||
|
||||
const interpreter = new Sval({
|
||||
export const interpreter = new Sval({
|
||||
// ECMA Version of the code
|
||||
// 3 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15
|
||||
// or 2015 | 2016 | 2017 | 2018 | 2019 | 2020 | 2021 | 2022 | 2023 | 2024
|
||||
@@ -12,5 +12,3 @@ const interpreter = new Sval({
|
||||
// Whether the code runs in a sandbox
|
||||
sandBox: true,
|
||||
});
|
||||
|
||||
export default interpreter;
|
||||
|
||||
@@ -60,7 +60,7 @@ export const matchRule = async (href, { injectRules, subrulesList }) => {
|
||||
"parentStyle",
|
||||
"grandStyle",
|
||||
"injectJs",
|
||||
"injectCss",
|
||||
// "injectCss",
|
||||
// "fixerSelector",
|
||||
"transStartHook",
|
||||
"transEndHook",
|
||||
@@ -154,7 +154,7 @@ export const checkRules = (rules) => {
|
||||
parentStyle,
|
||||
grandStyle,
|
||||
injectJs,
|
||||
injectCss,
|
||||
// injectCss,
|
||||
apiSlug,
|
||||
fromLang,
|
||||
toLang,
|
||||
@@ -193,7 +193,7 @@ export const checkRules = (rules) => {
|
||||
parentStyle: type(parentStyle) === "string" ? parentStyle : "",
|
||||
grandStyle: type(grandStyle) === "string" ? grandStyle : "",
|
||||
injectJs: type(injectJs) === "string" ? injectJs : "",
|
||||
injectCss: type(injectCss) === "string" ? injectCss : "",
|
||||
// injectCss: type(injectCss) === "string" ? injectCss : "",
|
||||
bgColor: type(bgColor) === "string" ? bgColor : "",
|
||||
textDiyStyle: type(textDiyStyle) === "string" ? textDiyStyle : "",
|
||||
apiSlug:
|
||||
|
||||
@@ -2,8 +2,6 @@ import {
|
||||
APP_UPNAME,
|
||||
APP_LCNAME,
|
||||
APP_CONSTS,
|
||||
MSG_INJECT_JS,
|
||||
MSG_INJECT_CSS,
|
||||
OPT_STYLE_FUZZY,
|
||||
GLOBLA_RULE,
|
||||
DEFAULT_SETTING,
|
||||
@@ -16,13 +14,10 @@ import {
|
||||
OPT_SPLIT_PARAGRAPH_DISABLE,
|
||||
OPT_SPLIT_PARAGRAPH_TEXTLENGTH,
|
||||
} from "../config";
|
||||
import interpreter from "./interpreter";
|
||||
import { interpreter } from "./interpreter";
|
||||
import { clearFetchPool } from "./pool";
|
||||
import { debounce, scheduleIdle, genEventName, truncateWords } from "./utils";
|
||||
import { apiTranslate } from "../apis";
|
||||
import { sendBgMsg } from "./msg";
|
||||
import { isExt } from "./client";
|
||||
import { injectInlineJs, injectInternalCss } from "./injector";
|
||||
import { kissLog } from "./log";
|
||||
import { clearAllBatchQueue } from "./batchQueue";
|
||||
import { genTextClass } from "./style";
|
||||
@@ -1145,7 +1140,6 @@ export class Translator {
|
||||
const {
|
||||
transTag,
|
||||
textStyle,
|
||||
transStartHook,
|
||||
transEndHook,
|
||||
transOnly,
|
||||
termsStyle,
|
||||
@@ -1164,20 +1158,6 @@ export class Translator {
|
||||
const parentNode = hostNode.parentElement;
|
||||
const hideOrigin = transOnly === "true";
|
||||
|
||||
// 翻译开始钩子函数
|
||||
if (transStartHook?.trim()) {
|
||||
try {
|
||||
interpreter.run(`exports.transStartHook = ${transStartHook}`);
|
||||
interpreter.exports.transStartHook({
|
||||
hostNode,
|
||||
parentNode,
|
||||
nodes,
|
||||
});
|
||||
} catch (err) {
|
||||
kissLog("transStartHook", err);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const [processedString, placeholderMap] = this.#serializeForTranslation(
|
||||
nodes,
|
||||
@@ -1202,10 +1182,8 @@ export class Translator {
|
||||
nodes[nodes.length - 1].after(wrapper);
|
||||
|
||||
const currentRunId = this.#runId;
|
||||
const [translatedText, isSameLang] = await this.#translateFetch(
|
||||
processedString,
|
||||
deLang
|
||||
);
|
||||
const { trText: translatedText, isSame: isSameLang } =
|
||||
await this.#translateFetch(processedString, deLang);
|
||||
if (this.#runId !== currentRunId) {
|
||||
throw new Error("Request terminated");
|
||||
}
|
||||
@@ -1391,16 +1369,39 @@ export class Translator {
|
||||
|
||||
// 发起翻译请求
|
||||
#translateFetch(text, deLang = "") {
|
||||
const { fromLang, toLang } = this.#rule;
|
||||
const { toLang, transStartHook } = this.#rule;
|
||||
const fromLang = deLang || this.#rule.fromLang;
|
||||
const apiSetting = { ...this.#apiSetting };
|
||||
const docInfo = { ...this.#docInfo };
|
||||
const glossary = { ...this.#glossary };
|
||||
const apisMap = this.#apisMap;
|
||||
|
||||
return apiTranslate({
|
||||
const args = {
|
||||
text,
|
||||
fromLang: deLang || fromLang,
|
||||
fromLang,
|
||||
toLang,
|
||||
apiSetting: this.#apiSetting,
|
||||
docInfo: this.#docInfo,
|
||||
glossary: this.#glossary,
|
||||
});
|
||||
apiSetting,
|
||||
docInfo,
|
||||
glossary,
|
||||
};
|
||||
|
||||
// 翻译开始钩子函数
|
||||
if (transStartHook?.trim()) {
|
||||
try {
|
||||
interpreter.run(`exports.transStartHook = ${transStartHook}`);
|
||||
const hookResult = interpreter.exports.transStartHook({
|
||||
...args,
|
||||
apisMap,
|
||||
});
|
||||
if (hookResult) {
|
||||
Object.assign(args, ...hookResult);
|
||||
}
|
||||
} catch (err) {
|
||||
kissLog("transStartHook", err);
|
||||
}
|
||||
}
|
||||
|
||||
return apiTranslate(args);
|
||||
}
|
||||
|
||||
// 查找指定节点下所有译文节点
|
||||
@@ -1596,14 +1597,35 @@ export class Translator {
|
||||
this.#isJsInjected = true;
|
||||
|
||||
try {
|
||||
const { injectJs, injectCss } = this.#rule;
|
||||
if (isExt) {
|
||||
injectJs && sendBgMsg(MSG_INJECT_JS, injectJs);
|
||||
injectCss && sendBgMsg(MSG_INJECT_CSS, injectCss);
|
||||
} else {
|
||||
injectJs &&
|
||||
injectInlineJs(injectJs, "kiss-translator-userinit-injector");
|
||||
injectCss && injectInternalCss(injectCss);
|
||||
// const { injectJs, injectCss } = this.#rule;
|
||||
// if (isExt) {
|
||||
// injectJs && sendBgMsg(MSG_INJECT_JS, injectJs);
|
||||
// injectCss && sendBgMsg(MSG_INJECT_CSS, injectCss);
|
||||
// } else {
|
||||
// injectJs &&
|
||||
// injectInlineJs(injectJs, "kiss-translator-userinit-injector");
|
||||
// injectCss && injectInternalCss(injectCss);
|
||||
// }
|
||||
|
||||
const { injectJs, toLang } = this.#rule;
|
||||
if (injectJs?.trim()) {
|
||||
const apiSetting = { ...this.#apiSetting };
|
||||
const docInfo = { ...this.#docInfo };
|
||||
const glossary = { ...this.#glossary };
|
||||
const apisMap = this.#apisMap;
|
||||
const apiDectect = tryDetectLang;
|
||||
interpreter.import({
|
||||
KT: {
|
||||
apiTranslate,
|
||||
apiDectect,
|
||||
apiSetting,
|
||||
apisMap,
|
||||
toLang,
|
||||
docInfo,
|
||||
glossary,
|
||||
},
|
||||
});
|
||||
interpreter.run(injectJs);
|
||||
}
|
||||
} catch (err) {
|
||||
kissLog("inject js", err);
|
||||
@@ -1654,8 +1676,8 @@ export class Translator {
|
||||
|
||||
try {
|
||||
const deLang = await tryDetectLang(title);
|
||||
const [translatedTitle] = await this.#translateFetch(title, deLang);
|
||||
document.title = translatedTitle || title;
|
||||
const { trText } = await this.#translateFetch(title, deLang);
|
||||
document.title = trText || title;
|
||||
} catch (err) {
|
||||
kissLog("tanslate title", err);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user