feat: support AI terms

This commit is contained in:
Gabe
2025-10-01 16:18:19 +08:00
parent 261bb7aa6f
commit 3c5ffc045f
8 changed files with 91 additions and 74 deletions

View File

@@ -52,6 +52,7 @@ export const matchRule = async (href, { injectRules, subrulesList }) => {
"rootsSelector",
"ignoreSelector",
"terms",
"aiTerms",
"selectStyle",
"parentStyle",
"injectJs",
@@ -134,6 +135,7 @@ export const checkRules = (rules) => {
rootsSelector,
ignoreSelector,
terms,
aiTerms,
selectStyle,
parentStyle,
injectJs,
@@ -166,6 +168,7 @@ export const checkRules = (rules) => {
rootsSelector: type(rootsSelector) === "string" ? rootsSelector : "",
ignoreSelector: type(ignoreSelector) === "string" ? ignoreSelector : "",
terms: type(terms) === "string" ? terms : "",
aiTerms: type(aiTerms) === "string" ? aiTerms : "",
selectStyle: type(selectStyle) === "string" ? selectStyle : "",
parentStyle: type(parentStyle) === "string" ? parentStyle : "",
injectJs: type(injectJs) === "string" ? injectJs : "",

View File

@@ -271,6 +271,7 @@ export class Translator {
#translationTagName = APP_NAME; // 翻译容器的标签名
#eventName = ""; // 通信事件名称
#docInfo = {}; // 网页信息
#glossary = {}; // AI词典
#textClass = {}; // 译文样式class
#textSheet = ""; // 译文样式字典
#apiSetting = null;
@@ -334,6 +335,7 @@ export class Translator {
);
this.#placeholderRegex = this.#createPlaceholderRegex();
this.#parseTerms(this.#rule.terms);
this.#parseAITerms(this.#rule.aiTerms);
this.#createTextStyles();
this.#boundMouseMoveHandler = this.#handleMouseMove.bind(this);
@@ -512,6 +514,24 @@ export class Translator {
}
}
#parseAITerms(termsString) {
if (!termsString || typeof termsString !== "string") return;
try {
this.#glossary = Object.fromEntries(
termsString
.split(/\n|;/)
.map((line) => {
const [k = "", v = ""] = line.split(",").map((s) => s.trim());
return [k, v];
})
.filter(([k]) => k)
);
} catch (err) {
kissLog("parse aiterms", err);
}
}
// todo: 利用AI总结
#getDocDescription() {
try {
@@ -1157,6 +1177,7 @@ export class Translator {
toLang,
apiSetting: this.#apiSetting,
docInfo: this.#docInfo,
glossary: this.#glossary,
});
}