MCPcopy Create free account
hub / github.com/Useful-Scripts-Extension/useful-script / hook

Function hook

scripts/libs/ajax-hook/index.js:1–16  ·  view source on GitHub ↗
(configs = [])

Source from the content-addressed store, hash-verified

1function hook(configs = []) {
2 const unsubFn = [];
3 for (const { fn, arr } of configs) {
4 if (typeof fn !== "function" || !Array.isArray(arr)) continue;
5 const id = randId();
6 arr.push({ fn, id });
7 unsubFn.push(() => {
8 const index = arr.findIndex((e) => e.id === id);
9 if (index !== -1) arr.splice(index, 1);
10 });
11 }
12
13 return () => {
14 unsubFn.forEach((fn) => fn?.());
15 };
16}
17
18function randId() {
19 return Math.random().toString(36).slice(2);

Callers 3

hookFetchFunction · 0.70
hookXHRFunction · 0.70
hookWSFunction · 0.70

Calls 2

randIdFunction · 0.85
fnFunction · 0.50

Tested by

no test coverage detected