Cache webpack searches, reuse objs and fix Firefox loading

This commit is contained in:
Jade Macho 2024-10-30 22:18:54 +01:00
parent dd71df83a1
commit d8b91bb16b
Signed by: 0x0ade
GPG Key ID: E1960710FE4FBEEF
2 changed files with 68 additions and 17 deletions

View File

@ -24,6 +24,18 @@ export default JadefinIntegrity("Jadefin", import.meta.url, () => window["Jadefi
/** @type {any} */
_webpackModuleFuncs;
/** @type {{[id: number]: {0: number[], 1: {[id: number]: function}}}} */
_webpackIdToMeta = {};
/** @type {{[id: number]: string[]}} */
_webpackIdToJSs = {};
/** @type {{[id: number]: string}} */
_webpackIdToJS = {};
/** @type {{[id: number]: string}} */
_webpackIdToName = {};
_webpackBlacklistClient = JSON.parse(localStorage.getItem("jadefin-blacklist") || "[]");
/** @type {typeof this._webpackBlacklistClient} */
webpackBlacklistClient = new Proxy(this._webpackBlacklistClient, {
@ -159,9 +171,7 @@ export default JadefinIntegrity("Jadefin", import.meta.url, () => window["Jadefi
/** @type {(id: number | string) => any} */
this.webpackLoad = id => {
const sid = `${id}`;
const cached = this._webpackCache[sid];
const cached = this._webpackCache[id];
if (cached) {
return cached;
}
@ -169,9 +179,9 @@ export default JadefinIntegrity("Jadefin", import.meta.url, () => window["Jadefi
const name = this.webpackIdToName(id);
if (this.webpackBlacklist !== this.webpackBlacklistClient) {
const cachedRaw = this._webpackRawCache?.[sid];
const cachedRaw = this._webpackRawCache?.[id];
if (cachedRaw && cachedRaw.loaded) {
return this._webpackCache[sid] = cachedRaw.exports;
return this._webpackCache[id] = cachedRaw.exports;
}
// Some modules are known to break things when loaded too early.
@ -193,7 +203,7 @@ export default JadefinIntegrity("Jadefin", import.meta.url, () => window["Jadefi
}
try {
return this._webpackCache[sid] = this.webpackRawLoad?.(id);
return this._webpackCache[id] = this.webpackRawLoad?.(id);
} catch (e) {
this.log.e(`Failed to load webpack module ${id} (${name})`);
this.log.dir(e);
@ -345,30 +355,61 @@ export default JadefinIntegrity("Jadefin", import.meta.url, () => window["Jadefi
return unsafe;
}
/**
* @param {any} id
* @return {typeof this._webpackIdToMeta[0]}
*/
webpackIdToMeta(id) {
const meta = this._webpackIdToMeta[id];
if (meta) {
return meta;
}
this.log.i("Rebuilding webpackIdToMeta");
this._webpackIdToMeta = {};
for (const meta of window["webpackChunk"]) {
for (const pid of meta[0]) {
this._webpackIdToMeta[pid] = meta;
}
for (const cid of Object.keys(meta[1])) {
this._webpackIdToMeta[cid] = meta;
}
}
return this._webpackIdToMeta[id];
}
/**
* @param {any} id
* @return {string[]}
*/
webpackIdToJSs(id) {
if (!this.webpackChunkIdToJS) {
return [ `${id}.unknown.chunk.js` ];
throw new Error("Calling webpackIdToJSs too early");
}
const sid = `${id}`;
const meta = window["webpackChunk"].find(c => c[1][sid]);
if (!meta) {
return [ this.webpackChunkIdToJS(parseInt(sid)) ];
const cached = this._webpackIdToJSs[id];
if (cached) {
return cached;
}
const jss = [];
const meta = this.webpackIdToMeta(id);
if (!meta) {
jss.push(this.webpackChunkIdToJS(parseInt(id)));
return jss;
}
for (const pid of meta[0]) {
jss.push(this.webpackChunkIdToJS(pid));
this._webpackIdToJSs[pid] = jss;
}
for (const cid of Object.keys(meta[1])) {
jss.push(this.webpackChunkIdToJS(cid));
this._webpackIdToJSs[cid] = jss;
}
return jss;
@ -379,9 +420,13 @@ export default JadefinIntegrity("Jadefin", import.meta.url, () => window["Jadefi
* @return {string}
*/
webpackIdToJS(id) {
const jss = this.webpackIdToJSs(id);
const cached = this._webpackIdToJS[id];
if (cached) {
return cached;
}
return jss.find(c => !/^\d/.test(c)) || jss.find(c => c.startsWith(`${id}.`)) || `${id}.unknown.chunk.js`;
const jss = this.webpackIdToJSs(id);
return this._webpackIdToJS[id] = jss.find(c => !/^\d/.test(c)) || jss.find(c => c.startsWith(`${id}.`)) || `${id}.unknown.chunk.js`;
};
/**
@ -389,18 +434,23 @@ export default JadefinIntegrity("Jadefin", import.meta.url, () => window["Jadefi
* @return {string}
*/
webpackIdToName(id) {
const cached = this._webpackIdToName[id];
if (cached) {
return cached;
}
const name = this.webpackIdToJS(id);
if (name.endsWith(".bundle.js")) {
return name.substring(0, name.length - ".bundle.js".length);
return this._webpackIdToName[id] = name.substring(0, name.length - ".bundle.js".length);
}
if (name.endsWith(".chunk.js")) {
const split = name.lastIndexOf(".", name.length - ".chunk.js".length - 1);
return name.substring(0, split);
return this._webpackIdToName[id] = name.substring(0, split);
}
return name;
return this._webpackIdToName[id] = name;
};
/**

View File

@ -64,6 +64,7 @@ export default JadefinIntegrity("FilterworksAnime4KSrc", import.meta.url, () =>
async init(name, url) {
await super.init(name, url);
// https://github.com/monyone/Anime4K.js/
await new Promise((resolve, reject) => {
const loader = document.createElement("script");
document.head.appendChild(loader);