webfix
This commit is contained in:
@@ -5,6 +5,7 @@ import {
|
||||
STOKEY_SYNC,
|
||||
STOKEY_MSAUTH,
|
||||
STOKEY_RULESCACHE_PREFIX,
|
||||
STOKEY_WEBFIXCACHE_PREFIX,
|
||||
DEFAULT_SETTING,
|
||||
DEFAULT_RULES,
|
||||
DEFAULT_SYNC,
|
||||
@@ -104,6 +105,14 @@ export const delSubRules = (url) => del(STOKEY_RULESCACHE_PREFIX + url);
|
||||
export const setSubRules = (url, val) =>
|
||||
setObj(STOKEY_RULESCACHE_PREFIX + url, val);
|
||||
|
||||
/**
|
||||
* 修复站点
|
||||
*/
|
||||
export const getWebfix = (url) => getObj(STOKEY_WEBFIXCACHE_PREFIX + url);
|
||||
export const getWebfixWithDefault = async () => (await getWebfix()) || [];
|
||||
export const setWebfix = (url, val) =>
|
||||
setObj(STOKEY_WEBFIXCACHE_PREFIX + url, val);
|
||||
|
||||
/**
|
||||
* fab位置
|
||||
*/
|
||||
|
||||
@@ -6,7 +6,7 @@ import {
|
||||
getSubRules,
|
||||
updateSetting,
|
||||
} from "./storage";
|
||||
import { apiFetchRules } from "../apis";
|
||||
import { apiFetch } from "../apis";
|
||||
import { checkRules } from "./rules";
|
||||
import { isAllchar } from "./utils";
|
||||
|
||||
@@ -16,7 +16,7 @@ import { isAllchar } from "./utils";
|
||||
* @returns
|
||||
*/
|
||||
export const syncSubRules = async (url, isBg = false) => {
|
||||
const res = await apiFetchRules(url, isBg);
|
||||
const res = await apiFetch(url, isBg);
|
||||
const rules = checkRules(res).filter(
|
||||
({ pattern }) => !isAllchar(pattern, GLOBAL_KEY)
|
||||
);
|
||||
|
||||
@@ -1,4 +1,11 @@
|
||||
import { isMatch } from "./utils";
|
||||
import { getWebfix, setWebfix } from "./storage";
|
||||
import { apiFetch } from "../apis";
|
||||
|
||||
/**
|
||||
* 修复程序类型
|
||||
*/
|
||||
const WEBFIX_BR = "br";
|
||||
|
||||
/**
|
||||
* 需要修复的站点列表
|
||||
@@ -7,18 +14,18 @@ import { isMatch } from "./utils";
|
||||
* - rootSlector 需要监听的选择器,可留空
|
||||
* - fixer 修复函数,可针对不同网址,选用不同修复函数
|
||||
*/
|
||||
export const sites = [
|
||||
const DEFAULT_SITES = [
|
||||
{
|
||||
pattern: "www.phoronix.com",
|
||||
selector: ".content",
|
||||
rootSlector: "",
|
||||
fixer: brFixer,
|
||||
fixer: WEBFIX_BR,
|
||||
},
|
||||
{
|
||||
pattern: "t.me/s/*",
|
||||
selector: ".tgme_widget_message_text",
|
||||
rootSlector: ".tgme_channel_history",
|
||||
fixer: brFixer,
|
||||
fixer: WEBFIX_BR,
|
||||
},
|
||||
];
|
||||
|
||||
@@ -80,6 +87,13 @@ function brFixer(node) {
|
||||
node.innerHTML = html;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修复程序映射
|
||||
*/
|
||||
const fixerMap = {
|
||||
[WEBFIX_BR]: brFixer,
|
||||
};
|
||||
|
||||
/**
|
||||
* 查找、监听节点,并执行修复函数
|
||||
* @param {*} selector
|
||||
@@ -108,19 +122,51 @@ function run(selector, fixer, rootSlector) {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步远程数据
|
||||
* @param {*} url
|
||||
* @returns
|
||||
*/
|
||||
export const syncWebfix = async (url) => {
|
||||
const sites = await apiFetch(url);
|
||||
await setWebfix(url, sites);
|
||||
return sites;
|
||||
};
|
||||
|
||||
/**
|
||||
* 从缓存或远程加载修复站点
|
||||
* @param {*} url
|
||||
* @returns
|
||||
*/
|
||||
export const loadOrFetchWebfix = async (url) => {
|
||||
try {
|
||||
let sites = await getWebfix(url);
|
||||
if (sites?.length) {
|
||||
return sites;
|
||||
}
|
||||
return syncWebfix(url);
|
||||
} catch (err) {
|
||||
console.log("[load webfix]", err.message);
|
||||
return DEFAULT_SITES;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 匹配站点
|
||||
*/
|
||||
export function webfix(href, { injectWebfix }) {
|
||||
export async function webfix(href, { injectWebfix }) {
|
||||
try {
|
||||
if (!injectWebfix) {
|
||||
return;
|
||||
}
|
||||
|
||||
const sites = await loadOrFetchWebfix(process.env.REACT_APP_WEBFIXURL);
|
||||
for (var i = 0; i < sites.length; i++) {
|
||||
var site = sites[i];
|
||||
if (isMatch(href, site.pattern)) {
|
||||
run(site.selector, site.fixer, site.rootSlector);
|
||||
if (fixerMap[site.fixer]) {
|
||||
run(site.selector, fixerMap[site.fixer], site.rootSlector);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user