fix: inject js/css
This commit is contained in:
@@ -4,7 +4,7 @@ export const injectInlineJs = (code) => {
|
||||
el.setAttribute("data-source", "KISS-Calendar injectInlineJs");
|
||||
el.setAttribute("type", "text/javascript");
|
||||
el.textContent = code;
|
||||
document.body.appendChild(el);
|
||||
document.body?.appendChild(el);
|
||||
};
|
||||
|
||||
// Function to inject external JavaScript file
|
||||
@@ -13,7 +13,7 @@ export const injectExternalJs = (src) => {
|
||||
el.setAttribute("data-source", "KISS-Calendar injectExternalJs");
|
||||
el.setAttribute("type", "text/javascript");
|
||||
el.setAttribute("src", src);
|
||||
document.body.appendChild(el);
|
||||
document.body?.appendChild(el);
|
||||
};
|
||||
|
||||
// Function to inject internal CSS code
|
||||
@@ -21,7 +21,7 @@ export const injectInternalCss = (styles) => {
|
||||
const el = document.createElement("style");
|
||||
el.setAttribute("data-source", "KISS-Calendar injectInternalCss");
|
||||
el.textContent = styles;
|
||||
document.head.appendChild(el);
|
||||
document.head?.appendChild(el);
|
||||
};
|
||||
|
||||
// Function to inject external CSS file
|
||||
@@ -31,5 +31,5 @@ export const injectExternalCss = (href) => {
|
||||
el.setAttribute("rel", "stylesheet");
|
||||
el.setAttribute("type", "text/css");
|
||||
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 { apiTranslate } from "../apis";
|
||||
import { sendBgMsg } from "./msg";
|
||||
import { isExt } from "./client";
|
||||
import { injectInlineJs, injectInternalCss } from "./injector";
|
||||
|
||||
/**
|
||||
* 翻译类
|
||||
@@ -276,8 +278,13 @@ export class Translator {
|
||||
}
|
||||
|
||||
// 注入用户JS/CSS
|
||||
injectJs && sendBgMsg(MSG_INJECT_JS, injectJs);
|
||||
injectCss && sendBgMsg(MSG_INJECT_CSS, injectCss);
|
||||
if (isExt) {
|
||||
injectJs && sendBgMsg(MSG_INJECT_JS, injectJs);
|
||||
injectCss && sendBgMsg(MSG_INJECT_CSS, injectCss);
|
||||
} else {
|
||||
injectJs && injectInlineJs(injectJs);
|
||||
injectCss && injectInternalCss(injectCss);
|
||||
}
|
||||
|
||||
// 搜索节点
|
||||
this._queryNodes();
|
||||
@@ -406,9 +413,7 @@ export class Translator {
|
||||
});
|
||||
|
||||
// 移除用户JS/CSS
|
||||
document
|
||||
.querySelectorAll(`[data-source^="KISS-Calendar"]`)
|
||||
?.forEach((el) => el.remove());
|
||||
this._removeInjector();
|
||||
|
||||
// 清空节点集合
|
||||
this._rootNodes.clear();
|
||||
@@ -418,8 +423,15 @@ export class Translator {
|
||||
clearFetchPool();
|
||||
};
|
||||
|
||||
_removeInjector = () => {
|
||||
document
|
||||
.querySelectorAll(`[data-source^="KISS-Calendar"]`)
|
||||
?.forEach((el) => el.remove());
|
||||
};
|
||||
|
||||
_reTranslate = debounce(() => {
|
||||
if (this._rule.transOpen === "true") {
|
||||
this._removeInjector();
|
||||
this._register();
|
||||
}
|
||||
}, 500);
|
||||
|
||||
Reference in New Issue
Block a user