fix: inject js/css
This commit is contained in:
@@ -228,11 +228,11 @@ export const I18N = {
|
|||||||
en: `After setting, it will produce mutual translation effect with the target language, but it relies on remote language recognition.`,
|
en: `After setting, it will produce mutual translation effect with the target language, but it relies on remote language recognition.`,
|
||||||
},
|
},
|
||||||
text_style: {
|
text_style: {
|
||||||
zh: `文字样式`,
|
zh: `译文样式`,
|
||||||
en: `Text Style`,
|
en: `Text Style`,
|
||||||
},
|
},
|
||||||
text_style_alt: {
|
text_style_alt: {
|
||||||
zh: `文字样式`,
|
zh: `译文样式`,
|
||||||
en: `Text Style`,
|
en: `Text Style`,
|
||||||
},
|
},
|
||||||
bg_color: {
|
bg_color: {
|
||||||
@@ -419,6 +419,10 @@ export const I18N = {
|
|||||||
zh: `注入JS`,
|
zh: `注入JS`,
|
||||||
en: `Inject JS`,
|
en: `Inject JS`,
|
||||||
},
|
},
|
||||||
|
inject_js_helper: {
|
||||||
|
zh: `翻译时注入运行,且随着页面变化,可能会多次注入运行。`,
|
||||||
|
en: `It is injected and run during translation, and may be injected and run multiple times as the page changes.`,
|
||||||
|
},
|
||||||
inject_css: {
|
inject_css: {
|
||||||
zh: `注入CSS`,
|
zh: `注入CSS`,
|
||||||
en: `Inject CSS`,
|
en: `Inject CSS`,
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ export const injectInlineJs = (code) => {
|
|||||||
el.setAttribute("data-source", "KISS-Calendar injectInlineJs");
|
el.setAttribute("data-source", "KISS-Calendar injectInlineJs");
|
||||||
el.setAttribute("type", "text/javascript");
|
el.setAttribute("type", "text/javascript");
|
||||||
el.textContent = code;
|
el.textContent = code;
|
||||||
document.body.appendChild(el);
|
document.body?.appendChild(el);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Function to inject external JavaScript file
|
// Function to inject external JavaScript file
|
||||||
@@ -13,7 +13,7 @@ export const injectExternalJs = (src) => {
|
|||||||
el.setAttribute("data-source", "KISS-Calendar injectExternalJs");
|
el.setAttribute("data-source", "KISS-Calendar injectExternalJs");
|
||||||
el.setAttribute("type", "text/javascript");
|
el.setAttribute("type", "text/javascript");
|
||||||
el.setAttribute("src", src);
|
el.setAttribute("src", src);
|
||||||
document.body.appendChild(el);
|
document.body?.appendChild(el);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Function to inject internal CSS code
|
// Function to inject internal CSS code
|
||||||
@@ -21,7 +21,7 @@ export const injectInternalCss = (styles) => {
|
|||||||
const el = document.createElement("style");
|
const el = document.createElement("style");
|
||||||
el.setAttribute("data-source", "KISS-Calendar injectInternalCss");
|
el.setAttribute("data-source", "KISS-Calendar injectInternalCss");
|
||||||
el.textContent = styles;
|
el.textContent = styles;
|
||||||
document.head.appendChild(el);
|
document.head?.appendChild(el);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Function to inject external CSS file
|
// Function to inject external CSS file
|
||||||
@@ -31,5 +31,5 @@ export const injectExternalCss = (href) => {
|
|||||||
el.setAttribute("rel", "stylesheet");
|
el.setAttribute("rel", "stylesheet");
|
||||||
el.setAttribute("type", "text/css");
|
el.setAttribute("type", "text/css");
|
||||||
el.setAttribute("href", href);
|
el.setAttribute("href", href);
|
||||||
document.head.appendChild(el);
|
document.head?.appendChild(el);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -20,6 +20,8 @@ import { debounce, genEventName } from "./utils";
|
|||||||
import { runFixer } from "./webfix";
|
import { runFixer } from "./webfix";
|
||||||
import { apiTranslate } from "../apis";
|
import { apiTranslate } from "../apis";
|
||||||
import { sendBgMsg } from "./msg";
|
import { sendBgMsg } from "./msg";
|
||||||
|
import { isExt } from "./client";
|
||||||
|
import { injectInlineJs, injectInternalCss } from "./injector";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 翻译类
|
* 翻译类
|
||||||
@@ -276,8 +278,13 @@ export class Translator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 注入用户JS/CSS
|
// 注入用户JS/CSS
|
||||||
injectJs && sendBgMsg(MSG_INJECT_JS, injectJs);
|
if (isExt) {
|
||||||
injectCss && sendBgMsg(MSG_INJECT_CSS, injectCss);
|
injectJs && sendBgMsg(MSG_INJECT_JS, injectJs);
|
||||||
|
injectCss && sendBgMsg(MSG_INJECT_CSS, injectCss);
|
||||||
|
} else {
|
||||||
|
injectJs && injectInlineJs(injectJs);
|
||||||
|
injectCss && injectInternalCss(injectCss);
|
||||||
|
}
|
||||||
|
|
||||||
// 搜索节点
|
// 搜索节点
|
||||||
this._queryNodes();
|
this._queryNodes();
|
||||||
@@ -406,9 +413,7 @@ export class Translator {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// 移除用户JS/CSS
|
// 移除用户JS/CSS
|
||||||
document
|
this._removeInjector();
|
||||||
.querySelectorAll(`[data-source^="KISS-Calendar"]`)
|
|
||||||
?.forEach((el) => el.remove());
|
|
||||||
|
|
||||||
// 清空节点集合
|
// 清空节点集合
|
||||||
this._rootNodes.clear();
|
this._rootNodes.clear();
|
||||||
@@ -418,8 +423,15 @@ export class Translator {
|
|||||||
clearFetchPool();
|
clearFetchPool();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
_removeInjector = () => {
|
||||||
|
document
|
||||||
|
.querySelectorAll(`[data-source^="KISS-Calendar"]`)
|
||||||
|
?.forEach((el) => el.remove());
|
||||||
|
};
|
||||||
|
|
||||||
_reTranslate = debounce(() => {
|
_reTranslate = debounce(() => {
|
||||||
if (this._rule.transOpen === "true") {
|
if (this._rule.transOpen === "true") {
|
||||||
|
this._removeInjector();
|
||||||
this._register();
|
this._register();
|
||||||
}
|
}
|
||||||
}, 500);
|
}, 500);
|
||||||
|
|||||||
@@ -353,6 +353,7 @@ function RuleFields({ rule, rules, setShow, setKeyword }) {
|
|||||||
<TextField
|
<TextField
|
||||||
size="small"
|
size="small"
|
||||||
label={i18n("inject_js")}
|
label={i18n("inject_js")}
|
||||||
|
helperText={i18n("inject_js_helper")}
|
||||||
name="injectJs"
|
name="injectJs"
|
||||||
value={injectJs}
|
value={injectJs}
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
|
|||||||
Reference in New Issue
Block a user