文件
mynode/packages/app-desktop/gui/utils/loadScript.ts
chen 301194963b
一些检查失败了
automerge / automerge (push) Has been cancelled
react-native-android-build-apk / AssembleRelease (push) Has been cancelled
Build macOS M1 / Main (push) Has been cancelled
Joplin Continuous Integration / Main (macos-15-intel) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-11-vs2026-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-2025) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04-arm) (push) Has been cancelled
Joplin UI tests / Main (ubuntu-22.04) (push) Has been cancelled
Joplin UI tests / Main (windows-2025) (push) Has been cancelled
Joplin UI tests / Mobile (push) Has been cancelled
Close stale issues / ProcessStaleIssues (push) Has been cancelled
feat: Initial clean release of CHM
2026-11-04 11:29:39 +08:00

55 行
1.5 KiB
TypeScript

import Logger from '@joplin/utils/Logger';
const logger = Logger.create('loadScript');
export interface Script {
id: string;
src: string;
attrs?: Record<string, string>;
}
export const loadScript = async (script: Script, document: Document) => {
return new Promise((resolve) => {
// eslint-disable-next-line no-console
console.info('Loading script:', script);
let element = document.getElementById(script.id) as HTMLLinkElement | HTMLScriptElement | null;
if (element) {
const src = 'href' in element ? element.href : element.src;
if (src === script.src) {
logger.info(`Trying to load a script that has already been loaded: ${JSON.stringify(script)} - skipping it`);
resolve(null);
} else {
logger.info(`Source of script has changed - reloading it: ${JSON.stringify(script)}`);
element.parentNode.removeChild(element);
element = null;
}
}
if (script.src.indexOf('.css') >= 0) {
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = script.src;
element = link;
} else {
const scriptElement = document.createElement('script');
scriptElement.src = script.src;
if (script.attrs) {
for (const attr in script.attrs) {
(scriptElement as unknown as Record<string, string>)[attr] = script.attrs[attr];
}
}
element = scriptElement;
}
element.id = script.id;
element.onload = () => {
resolve(null);
};
document.getElementsByTagName('head')[0].appendChild(element);
});
};