Files
kiss-translator/src/libs/translator.js

266 lines
6.4 KiB
JavaScript
Raw Normal View History

2023-08-05 15:32:51 +08:00
import { createRoot } from "react-dom/client";
import {
APP_LCNAME,
TRANS_MIN_LENGTH,
TRANS_MAX_LENGTH,
EVENT_KISS,
MSG_TRANS_CURRULE,
2023-08-21 16:06:21 +08:00
OPT_STYLE_DASHLINE,
OPT_STYLE_FUZZY,
2023-08-25 17:07:53 +08:00
SHADOW_KEY,
} from "../config";
2023-08-05 15:32:51 +08:00
import Content from "../views/Content";
2023-08-11 16:48:09 +08:00
import { fetchUpdate, fetchClear } from "./fetch";
2023-08-24 14:57:54 +08:00
import { debounce } from "./utils";
2023-08-23 17:53:46 +08:00
2023-08-05 15:32:51 +08:00
/**
* 翻译类
*/
export class Translator {
_rule = {};
2023-08-22 21:45:23 +08:00
_minLength = 0;
_maxLength = 0;
2023-08-24 16:21:01 +08:00
_skipNodeNames = [
APP_LCNAME,
"style",
"svg",
"img",
"audio",
"video",
"textarea",
"input",
"button",
"select",
"option",
"head",
"script",
2023-08-25 17:07:53 +08:00
"iframe",
2023-08-24 16:21:01 +08:00
];
2023-08-25 17:07:53 +08:00
_rootNodes = new Set();
_tranNodes = new Set();
2023-08-05 15:32:51 +08:00
2023-08-23 17:53:46 +08:00
// 显示
2023-08-05 15:32:51 +08:00
_interseObserver = new IntersectionObserver(
(intersections) => {
intersections.forEach((intersection) => {
if (intersection.isIntersecting) {
this._render(intersection.target);
this._interseObserver.unobserve(intersection.target);
}
});
},
{
threshold: 0.1,
}
);
2023-08-23 17:53:46 +08:00
// 变化
2023-08-05 15:32:51 +08:00
_mutaObserver = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
2023-08-24 14:57:54 +08:00
if (
2023-08-24 16:21:01 +08:00
!this._skipNodeNames.includes(mutation.target.localName) &&
2023-08-24 14:57:54 +08:00
mutation.addedNodes.length > 0
) {
2023-08-25 17:07:53 +08:00
const nodes = Array.from(mutation.addedNodes).filter((node) => {
2023-08-24 16:21:01 +08:00
if (
this._skipNodeNames.includes(node.localName) ||
node.id === APP_LCNAME
) {
return false;
2023-08-24 14:57:54 +08:00
}
2023-08-24 16:21:01 +08:00
return true;
2023-08-24 14:57:54 +08:00
});
2023-08-25 17:07:53 +08:00
if (nodes.length > 0) {
// const rootNode = mutation.target.getRootNode();
// todo
2023-08-24 14:57:54 +08:00
this._reTranslate();
2023-08-05 15:32:51 +08:00
}
2023-08-24 14:57:54 +08:00
}
2023-08-05 15:32:51 +08:00
});
});
2023-08-24 16:21:01 +08:00
// 插入 shadowroot
2023-08-24 14:57:54 +08:00
_overrideAttachShadow = () => {
const _this = this;
const _attachShadow = HTMLElement.prototype.attachShadow;
HTMLElement.prototype.attachShadow = function () {
_this._reTranslate();
return _attachShadow.apply(this, arguments);
};
};
2023-08-22 21:45:23 +08:00
constructor(rule, { fetchInterval, fetchLimit, minLength, maxLength }) {
2023-08-10 13:41:55 +08:00
fetchUpdate(fetchInterval, fetchLimit);
2023-08-24 14:57:54 +08:00
this._overrideAttachShadow();
2023-08-22 21:45:23 +08:00
this._minLength = minLength ?? TRANS_MIN_LENGTH;
this._maxLength = maxLength ?? TRANS_MAX_LENGTH;
this.rule = rule;
2023-08-08 13:29:15 +08:00
if (rule.transOpen === "true") {
2023-08-05 15:32:51 +08:00
this._register();
}
}
get rule() {
// console.log("get rule", this._rule);
2023-08-05 15:32:51 +08:00
return this._rule;
}
set rule(rule) {
// console.log("set rule", rule);
this._rule = rule;
// 广播消息
window.dispatchEvent(
new CustomEvent(EVENT_KISS, {
detail: {
action: MSG_TRANS_CURRULE,
args: rule,
},
})
);
}
2023-08-05 15:32:51 +08:00
updateRule = (obj) => {
this.rule = { ...this.rule, ...obj };
2023-08-05 15:32:51 +08:00
};
toggle = () => {
if (this.rule.transOpen === "true") {
this.rule = { ...this.rule, transOpen: "false" };
2023-08-05 15:32:51 +08:00
this._unRegister();
} else {
this.rule = { ...this.rule, transOpen: "true" };
2023-08-05 15:32:51 +08:00
this._register();
}
};
2023-08-21 16:06:21 +08:00
toggleStyle = () => {
const textStyle =
this.rule.textStyle === OPT_STYLE_FUZZY
? OPT_STYLE_DASHLINE
: OPT_STYLE_FUZZY;
this.rule = { ...this.rule, textStyle };
};
2023-08-25 22:48:11 +08:00
_querySelectorAll = (selector, node) => {
try {
return node.querySelectorAll(selector);
} catch (err) {
console.log(`[querySelectorAll err]: ${selector}`);
}
return [];
2023-08-25 17:07:53 +08:00
};
2023-08-24 14:57:54 +08:00
_queryNodes = (rootNode = document) => {
2023-08-25 17:07:53 +08:00
// const childRoots = Array.from(rootNode.querySelectorAll("*"))
// .map((item) => item.shadowRoot)
// .filter(Boolean);
// const childNodes = childRoots.map((item) => this._queryNodes(item));
// const nodes = Array.from(rootNode.querySelectorAll(this.rule.selector));
// return nodes.concat(childNodes).flat();
this._rootNodes.add(rootNode);
this._rule.selector
.split(";")
.map((item) => item.trim())
.filter(Boolean)
.forEach((selector) => {
if (selector.includes(SHADOW_KEY)) {
const [outSelector, inSelector] = selector
.split(SHADOW_KEY)
.map((item) => item.trim());
if (outSelector && inSelector) {
2023-08-25 22:48:11 +08:00
const outNodes = this._querySelectorAll(outSelector, rootNode);
2023-08-25 17:07:53 +08:00
outNodes.forEach((outNode) => {
if (outNode.shadowRoot) {
this._rootNodes.add(outNode.shadowRoot);
2023-08-25 22:48:11 +08:00
this._querySelectorAll(inSelector, outNode.shadowRoot).forEach(
2023-08-25 17:07:53 +08:00
(item) => {
this._tranNodes.add(item);
}
);
}
});
}
} else {
2023-08-25 22:48:11 +08:00
this._querySelectorAll(selector, rootNode).forEach((item) => {
2023-08-25 17:07:53 +08:00
this._tranNodes.add(item);
});
}
});
2023-08-24 14:57:54 +08:00
};
2023-08-05 15:32:51 +08:00
_register = () => {
2023-08-25 17:07:53 +08:00
// 搜索节点
this._queryNodes();
this._rootNodes.forEach((node) => {
// 监听节点变化;
this._mutaObserver.observe(node, {
childList: true,
subtree: true,
// characterData: true,
});
2023-08-05 15:32:51 +08:00
});
2023-08-25 17:07:53 +08:00
this._tranNodes.forEach((node) => {
// 监听节点显示
this._interseObserver.observe(node);
2023-08-05 15:32:51 +08:00
});
};
_unRegister = () => {
// 解除节点变化监听
this._mutaObserver.disconnect();
2023-08-25 22:48:11 +08:00
// 解除节点显示监听
this._interseObserver.disconnect();
2023-08-05 15:32:51 +08:00
2023-08-25 22:48:11 +08:00
// 移除已插入元素
this._tranNodes.forEach((node) => {
2023-08-25 17:07:53 +08:00
node.querySelector(APP_LCNAME)?.remove();
2023-08-23 17:53:46 +08:00
});
2023-08-11 16:48:09 +08:00
2023-08-25 17:07:53 +08:00
// 清空节点集合
this._rootNodes.clear();
this._tranNodes.clear();
2023-08-11 16:48:09 +08:00
// 清空任务池
fetchClear();
2023-08-05 15:32:51 +08:00
};
2023-08-24 14:57:54 +08:00
_reTranslate = debounce(() => {
2023-08-25 17:07:53 +08:00
if (this._rule.transOpen === "true") {
this._register();
2023-08-24 14:57:54 +08:00
}
}, 500);
2023-08-05 15:32:51 +08:00
_render = (el) => {
// 已翻译
2023-08-05 15:32:51 +08:00
if (el.querySelector(APP_LCNAME)) {
return;
}
// 太长或太短
2023-08-05 15:32:51 +08:00
const q = el.innerText.trim();
2023-08-22 21:45:23 +08:00
if (!q || q.length < this._minLength || q.length > this._maxLength) {
2023-08-05 15:32:51 +08:00
return;
}
2023-08-25 22:48:47 +08:00
// console.log("---> ", q);
2023-08-05 15:32:51 +08:00
const span = document.createElement(APP_LCNAME);
2023-08-10 17:03:37 +08:00
span.style.visibility = "visible";
2023-08-05 15:32:51 +08:00
el.appendChild(span);
2023-08-11 16:48:09 +08:00
el.style.cssText +=
"-webkit-line-clamp: unset; max-height: none; height: auto;";
2023-08-24 14:57:54 +08:00
if (el.parentElement) {
el.parentElement.style.cssText +=
"-webkit-line-clamp: unset; max-height: none; height: auto;";
}
2023-08-05 15:32:51 +08:00
const root = createRoot(span);
2023-08-19 13:48:03 +08:00
root.render(<Content q={q} translator={this} />);
2023-08-05 15:32:51 +08:00
};
}