Skip to content

Commit

Permalink
fix(all/api): plugin api is broken (FlysoftBeta#77)
Browse files Browse the repository at this point in the history
* fix(renderer/api/waitForElement): `selectors.splice` breaks the loop

* fix(all/loader): script entry is loaded multiple times

* fix(renderer/api): add missing `ntCall` api
  • Loading branch information
FlysoftBeta authored Jul 6, 2023
1 parent 42e8a47 commit 49ae34c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@ const s = path.sep;

const loadedPlugins: LoadedPlugins = {};

const stylesheets: [Plugin, string][] = [],
let stylesheets: [Plugin, string][] = [],
scripts: [Plugin, string][] = [];

export function applyScripts(api: any) {
scripts.forEach(([plugin, script]) => {
scripts = scripts.filter(([plugin, script]) => {
try {
require(script)(api);
return false;
} catch (reason) {
console.error(
`[!Loader] 运行此插件脚本时出现意外错误:${script},请联系插件作者 (${plugin.manifest.author}) 解决`
);
console.error(reason);
}
return true;
});
}

Expand Down
2 changes: 2 additions & 0 deletions src/renderer/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as fs from "fs-extra";
import { InterruptIPC, InterruptIPCOptions, addInterruptIpc } from "../../ipc";
import { nt } from "./nt";
import { startWatchingElement, waitForElement } from "./waitForElement";
import { ntCall } from "./nt/call";

export function getAPI(windowLoadPromise: Promise<void>) {
windowLoadPromise.then(() => startWatchingElement());
Expand All @@ -12,6 +13,7 @@ export function getAPI(windowLoadPromise: Promise<void>) {
addInterruptIpc(func, options),
},
nt: nt,
ntCall: ntCall,
modules: {
fs: fs,
},
Expand Down
12 changes: 5 additions & 7 deletions src/renderer/api/waitForElement.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const waitForElementSelectors: [string, (element: HTMLElement) => void][] = [];
let waitForElementSelectors: [string, (element: HTMLElement) => void][] = [];

export function startWatchingElement() {
new MutationObserver(() => refreshStatus()).observe(document.documentElement, {
Expand All @@ -8,12 +8,10 @@ export function startWatchingElement() {
}

export function refreshStatus() {
waitForElementSelectors.forEach((item, idx) => {
const ele = document.querySelector<HTMLElement>(item[0]);
if (ele) {
item[1](ele);
waitForElementSelectors.splice(idx);
}
waitForElementSelectors = waitForElementSelectors.filter(([selector, callback]) => {
const element = document.querySelector<HTMLElement>(selector);
element && callback(element);
return !element;
});
}

Expand Down

0 comments on commit 49ae34c

Please sign in to comment.