fix: run webfix before translate

This commit is contained in:
Gabe Yuan
2023-12-27 15:44:02 +08:00
parent d2d18a2384
commit 748f2002ab
3 changed files with 34 additions and 16 deletions

View File

@@ -20,7 +20,7 @@ import { touchTapListener } from "./libs/touch";
import { debounce, genEventName } from "./libs/utils";
import { handlePing, injectScript } from "./libs/gm";
import { browser } from "./libs/browser";
import { runWebfix } from "./libs/webfix";
import { matchFixer } from "./libs/webfix";
import { matchRule } from "./libs/rules";
import { trySyncAllSubRules } from "./libs/subRules";
import { isInBlacklist } from "./libs/blacklist";
@@ -252,11 +252,11 @@ export async function run(isUserscript = false) {
}
// 不规范网页修复
await runWebfix(setting);
const fixerSetting = await matchFixer(href, setting);
// 翻译网页
const rule = await matchRule(href, setting);
const translator = new Translator(rule, setting);
const translator = new Translator(rule, setting, fixerSetting);
// 监听消息
windowListener(rule);

View File

@@ -13,6 +13,7 @@ import {
import Content from "../views/Content";
import { updateFetchPool, clearFetchPool } from "./fetch";
import { debounce, genEventName } from "./utils";
import { runFixer } from "./webfix";
/**
* 翻译类
@@ -20,6 +21,7 @@ import { debounce, genEventName } from "./utils";
export class Translator {
_rule = {};
_setting = {};
_fixerSetting = null;
_rootNodes = new Set();
_tranNodes = new Map();
_skipNodeNames = [
@@ -91,13 +93,14 @@ export class Translator {
};
};
constructor(rule, setting) {
constructor(rule, setting, fixerSetting) {
const { fetchInterval, fetchLimit } = setting;
updateFetchPool(fetchInterval, fetchLimit);
this._overrideAttachShadow();
this._setting = setting;
this._rule = rule;
this._fixerSetting = fixerSetting;
if (rule.transOpen === "true") {
this._register();
@@ -235,6 +238,11 @@ export class Translator {
return;
}
// webfix
if (this._fixerSetting) {
runFixer(this._fixerSetting);
}
// 搜索节点
this._queryNodes();

View File

@@ -218,28 +218,38 @@ export const loadOrFetchWebfix = async (url) => {
};
/**
* 匹配站点
* 执行fixer
* @param {*} param0
*/
export async function runWebfix({ injectWebfix }) {
export async function runFixer({ selector, fixer, rootSelector }) {
try {
if (!injectWebfix) {
return;
run(selector, fixerMap[fixer], rootSelector);
} catch (err) {
console.error(`[kiss-webfix run]: ${err.message}`);
}
}
const href = document.location.href;
/**
* 匹配fixer配置
*/
export async function matchFixer(href, { injectWebfix }) {
if (!injectWebfix) {
return null;
}
try {
const userSites = await getWebfixRulesWithDefault();
const subSites = await loadOrFetchWebfix(process.env.REACT_APP_WEBFIXURL);
const sites = [...userSites, ...subSites];
for (var i = 0; i < sites.length; i++) {
var site = sites[i];
if (isMatch(href, site.pattern)) {
if (fixerMap[site.fixer]) {
run(site.selector, fixerMap[site.fixer], site.rootSelector);
}
break;
if (isMatch(href, site.pattern) && fixerMap[site.fixer]) {
return site;
}
}
} catch (err) {
console.error(`[kiss-webfix]: ${err.message}`);
console.error(`[kiss-webfix match]: ${err.message}`);
}
return null;
}