Browse Source

Add some error handling

pull/46/head
coomdev 2 years ago
parent
commit
779b449516
  1. 1
      build-chrome.js
  2. BIN
      chrome/_metadata/generated_indexed_rulesets/_ruleset1
  3. 4
      chrome/manifest.json
  4. 2
      firefox_update.json
  5. 2
      main.meta.js
  6. 51
      main.user.js
  7. 140
      src/background.ts
  8. 20
      src/main.ts
  9. 24
      src/pngv3.ts
  10. 5
      src/thirdeye.ts

1
build-chrome.js

@ -22,6 +22,7 @@ const domains = [
"https://*.coom.tech/*", "https://*.coom.tech/*",
"https://*.4chan.org/*", "https://*.4chan.org/*",
"https://*.4channel.org/*", "https://*.4channel.org/*",
"https://*.4plebs.org/*",
"https://*.pomf.cat/*", "https://*.pomf.cat/*",
"https://pomf.cat/*", "https://pomf.cat/*",
"https://desuarchive.org/*", "https://desuarchive.org/*",

BIN
chrome/_metadata/generated_indexed_rulesets/_ruleset1

Binary file not shown.

4
chrome/manifest.json

@ -2,7 +2,7 @@
"manifest_version": 3, "manifest_version": 3,
"name": "PngExtraEmbedder", "name": "PngExtraEmbedder",
"description": "Discover embedded files on 4chan and archives!", "description": "Discover embedded files on 4chan and archives!",
"version": "0.241", "version": "0.245",
"icons": { "icons": {
"64": "1449696017588.png" "64": "1449696017588.png"
}, },
@ -13,6 +13,7 @@
"https://*.coom.tech/*", "https://*.coom.tech/*",
"https://*.4chan.org/*", "https://*.4chan.org/*",
"https://*.4channel.org/*", "https://*.4channel.org/*",
"https://*.4plebs.org/*",
"https://*.pomf.cat/*", "https://*.pomf.cat/*",
"https://pomf.cat/*", "https://pomf.cat/*",
"https://desuarchive.org/*", "https://desuarchive.org/*",
@ -66,6 +67,7 @@
"https://*.coom.tech/*", "https://*.coom.tech/*",
"https://*.4chan.org/*", "https://*.4chan.org/*",
"https://*.4channel.org/*", "https://*.4channel.org/*",
"https://*.4plebs.org/*",
"https://*.pomf.cat/*", "https://*.pomf.cat/*",
"https://pomf.cat/*", "https://pomf.cat/*",
"https://desuarchive.org/*", "https://desuarchive.org/*",

2
firefox_update.json

@ -1 +1 @@
{"addons":{"{34ac4994-07f2-44d2-8599-682516a6c6a6}":{"updates":[{"version":"0.242","update_link":"https://git.coom.tech/fuckjannies/lolipiss/raw/branch/%E4%B8%AD%E5%87%BA%E3%81%97/pngextraembedder-0.241.xpi"}]}}} {"addons":{"{34ac4994-07f2-44d2-8599-682516a6c6a6}":{"updates":[{"version":"0.245","update_link":"https://git.coom.tech/fuckjannies/lolipiss/raw/branch/%E4%B8%AD%E5%87%BA%E3%81%97/pngextraembedder-0.241.xpi"}]}}}

2
main.meta.js

@ -1,7 +1,7 @@
// ==UserScript== // ==UserScript==
// @name PNGExtraEmbed // @name PNGExtraEmbed
// @namespace https://coom.tech/ // @namespace https://coom.tech/
// @version 0.242 // @version 0.245
// @description uhh // @description uhh
// @author You // @author You
// @match https://boards.4channel.org/* // @match https://boards.4channel.org/*

51
main.user.js

@ -1,7 +1,7 @@
// ==UserScript== // ==UserScript==
// @name PNGExtraEmbed // @name PNGExtraEmbed
// @namespace https://coom.tech/ // @namespace https://coom.tech/
// @version 0.242 // @version 0.245
// @description uhh // @description uhh
// @author You // @author You
// @match https://boards.4channel.org/* // @match https://boards.4channel.org/*
@ -89,7 +89,7 @@ const _DOMParser = DOMParser;
var define_BUILD_VERSION_default; var define_BUILD_VERSION_default;
var init_define_BUILD_VERSION = __esm({ var init_define_BUILD_VERSION = __esm({
"<define:BUILD_VERSION>"() { "<define:BUILD_VERSION>"() {
define_BUILD_VERSION_default = [0, 242]; define_BUILD_VERSION_default = [0, 245];
} }
}); });
@ -16189,6 +16189,7 @@ const _DOMParser = DOMParser;
csettings2 = b; csettings2 = b;
}); });
var CUM3 = import_buffer3.Buffer.from("doo\0m"); var CUM3 = import_buffer3.Buffer.from("doo\0m");
var CUM4 = import_buffer3.Buffer.from("voo\0m");
var BufferReadStream = (b) => { var BufferReadStream = (b) => {
const ret = new ReadableStream({ const ret = new ReadableStream({
pull(cont) { pull(cont) {
@ -16198,6 +16199,15 @@ const _DOMParser = DOMParser;
}); });
return ret; return ret;
}; };
var password = import_buffer3.Buffer.from("NOA");
var xor = (a, p) => {
let n = 0;
for (let i = 0; i < a.byteLength; ++i) {
a[i] ^= p[n];
n++;
n %= p.byteLength;
}
};
var extract = async (png) => { var extract = async (png) => {
const reader = BufferReadStream(png).getReader(); const reader = BufferReadStream(png).getReader();
const sneed = new PNGDecoder(reader); const sneed = new PNGDecoder(reader);
@ -16212,6 +16222,12 @@ const _DOMParser = DOMParser;
const k = await decodeCoom3Payload(buff.slice(4 + CUM3.length)); const k = await decodeCoom3Payload(buff.slice(4 + CUM3.length));
ret.push(...k.filter((e) => e).map((e) => e)); ret.push(...k.filter((e) => e).map((e) => e));
} }
if (buff.slice(4, 4 + CUM4.length).equals(CUM4)) {
const passed = buff.slice(4 + CUM4.length);
xor(passed, password);
const k = await decodeCoom3Payload(passed);
ret.push(...k.filter((e) => e).map((e) => e));
}
break; break;
case "IDAT": case "IDAT":
case "IEND": case "IEND":
@ -16250,7 +16266,9 @@ const _DOMParser = DOMParser;
if (magic2 && name != "IDAT") if (magic2 && name != "IDAT")
break; break;
if (!magic2 && name == "IDAT") { if (!magic2 && name == "IDAT") {
await encoder.insertchunk(["tEXt", async () => buildChunk("tEXt", import_buffer3.Buffer.concat([CUM3, injb])), () => Promise.resolve(0), 0]); const passed = import_buffer3.Buffer.from(injb);
xor(passed, password);
await encoder.insertchunk(["tEXt", async () => buildChunk("tEXt", import_buffer3.Buffer.concat([CUM4, passed])), () => Promise.resolve(0), 0]);
magic2 = true; magic2 = true;
} }
await encoder.insertchunk([name, chunk, crc, offset]); await encoder.insertchunk([name, chunk, crc, offset]);
@ -16278,6 +16296,8 @@ const _DOMParser = DOMParser;
buff = await chunk(); buff = await chunk();
if (buff.slice(4, 4 + CUM3.length).equals(CUM3)) if (buff.slice(4, 4 + CUM3.length).equals(CUM3))
return true; return true;
if (buff.slice(4, 4 + CUM4.length).equals(CUM4))
return true;
break; break;
case "IDAT": case "IDAT":
case "IEND": case "IEND":
@ -16304,7 +16324,7 @@ const _DOMParser = DOMParser;
init_esbuild_inject(); init_esbuild_inject();
var import_buffer4 = __toESM(require_buffer(), 1); var import_buffer4 = __toESM(require_buffer(), 1);
var ebml = __toESM(require_lib2(), 1); var ebml = __toESM(require_lib2(), 1);
var password = import_buffer4.Buffer.from("NOA"); var password2 = import_buffer4.Buffer.from("NOA");
var findEnclosingTag = (ch, name) => { var findEnclosingTag = (ch, name) => {
const first = ch.findIndex((e) => e.type == "m" && e.name == name); const first = ch.findIndex((e) => e.type == "m" && e.name == name);
if (first < 0) if (first < 0)
@ -16708,8 +16728,9 @@ const _DOMParser = DOMParser;
cache[b.domain][hex] = tran; cache[b.domain][hex] = tran;
return tran; return tran;
} catch (e) { } catch (e) {
console.error("The following error might be expected"); const error = e;
console.error(e); if (!error.message.includes("Unexpected end of JSON input"))
console.error(e);
return []; return [];
} }
}; };
@ -24012,12 +24033,17 @@ const _DOMParser = DOMParser;
} }
} }
}; };
if (shouldUseCache()) { try {
res2 = await getEmbedsFromCache(qp.getCurrentBoard(), +qp.getCurrentThread(), post.id); if (shouldUseCache()) {
} res2 = await getEmbedsFromCache(qp.getCurrentBoard(), +qp.getCurrentThread(), post.id);
if (!res2) { }
res2 = await processImage(origlink, qp.getFilename(post), qp.getMD5(post), thumbLink); if (!res2) {
res2 = res2?.filter((e) => e); res2 = await processImage(origlink, qp.getFilename(post), qp.getMD5(post), thumbLink);
res2 = res2?.filter((e) => e);
}
} catch (e) {
console.error(e);
return;
} }
if (!res2 || res2.length == 0) if (!res2 || res2.length == 0)
return; return;
@ -24285,7 +24311,6 @@ const _DOMParser = DOMParser;
})); }));
}; };
if (location.host.startsWith("boards.4chan")) { if (location.host.startsWith("boards.4chan")) {
setTimeout(() => startup(false), 2e3);
document.addEventListener("4chanParsingDone", () => startup(false), { once: true }); document.addEventListener("4chanParsingDone", () => startup(false), { once: true });
} }
document.addEventListener("4chanXInitFinished", () => startup(true), { once: true }); document.addEventListener("4chanXInitFinished", () => startup(true), { once: true });

140
src/background.ts

@ -95,74 +95,86 @@ const bgCorsFetch = async (c: MessagePort, id: number, input: string, init?: Req
input = 'https:' + input; input = 'https:' + input;
if (init?.body && execution_mode == "chrome_api") if (init?.body && execution_mode == "chrome_api")
init.body = await deserialize(init.body); init.body = await deserialize(init.body);
const k = await fetch(input, init); try {
let headersStr = ''; const k = await fetch(input, init);
const headerObj = {} as any;
k.headers.forEach((v, k) => { let headersStr = '';
headerObj[k] = v; const headerObj = {} as any;
headersStr += `${k}: ${v}\n`; k.headers.forEach((v, k) => {
}); headerObj[k] = v;
c.postMessage({ headersStr += `${k}: ${v}\n`;
id, setRes: true, });
ok: k.ok, c.postMessage({
headers: headerObj, id, setRes: true,
responseHeaders: headersStr, ok: k.ok,
redirected: k.redirected, headers: headerObj,
type: k.type, responseHeaders: headersStr,
url: k.url, redirected: k.redirected,
status: k.status, type: k.type,
bodyUsed: k.bodyUsed, url: k.url,
statusText: k.statusText, status: k.status,
}); bodyUsed: k.bodyUsed,
statusText: k.statusText,
});
pendingFetches.set(c, { pendingFetches.set(c, {
[id]: { [id]: {
fetchFully: false fetchFully: false
}, },
...(pendingFetches.get(c) || {}) ...(pendingFetches.get(c) || {})
}); });
let buff: Buffer[] = []; let buff: Buffer[] = [];
const ctotal = +headerObj['content-length'] || 0; // content total const ctotal = +headerObj['content-length'] || 0; // content total
let ltotal = 0; // loaded total let ltotal = 0; // loaded total
// sequence number, required to reorder messages client-side // sequence number, required to reorder messages client-side
// if they get processed out of order // if they get processed out of order
let s = 0; let s = 0;
const e = { const e = {
write(chunk: Uint8Array) { write(chunk: Uint8Array) {
ltotal += chunk.byteLength; ltotal += chunk.byteLength;
c.postMessage({ id, progress: [ltotal, ctotal] }); c.postMessage({ id, progress: [ltotal, ctotal] });
if (!pendingFetches.get(c)![id].fetchFully) { if (!pendingFetches.get(c)![id].fetchFully) {
//const url = new Blob([chunk]); //const url = new Blob([chunk]);
c.postMessage({ id, s: s++, pushData: { data: chunk } }, [chunk.buffer]); c.postMessage({ id, s: s++, pushData: { data: chunk } }, [chunk.buffer]);
} else { } else {
buff.push(Buffer.from(chunk)); buff.push(Buffer.from(chunk));
} }
}, },
close() { close() {
if (buff.length > 0) { if (buff.length > 0) {
const chunk = Buffer.concat(buff); const chunk = Buffer.concat(buff);
c.postMessage({ id, s: s++, pushData: { data: chunk } }, [chunk.buffer]); c.postMessage({ id, s: s++, pushData: { data: chunk } }, [chunk.buffer]);
buff = []; buff = [];
}
const obj = pendingFetches.get(c)!;
delete obj[id];
if (Object.keys(obj).length == 0)
pendingFetches.delete(c);
c.postMessage({ id, s: s++, pushData: {} });
} }
const obj = pendingFetches.get(c)!; };
delete obj[id]; const reader = k.body?.getReader();
if (Object.keys(obj).length == 0) let res: ReadableStreamDefaultReadResult<Uint8Array>;
pendingFetches.delete(c); for (; ;) {
c.postMessage({ id, s: s++, pushData: {} }); res = await reader!.read();
if (res.done) break;
e.write(res.value);
} }
}; e.close();
const reader = k.body?.getReader(); reader?.releaseLock();
let res: ReadableStreamDefaultReadResult<Uint8Array>; } catch(e) {
for (; ;) { const err = e as Error;
res = await reader!.read(); c.postMessage({
if (res.done) break; id,
e.write(res.value); setRes: true,
ok: false,
url: input,
status: err.message
});
} }
e.close();
reader?.releaseLock();
}; };
const meself = new URL(obj.runtime.getURL('')).origin; const meself = new URL(obj.runtime.getURL('')).origin;
@ -170,7 +182,7 @@ const meself = new URL(obj.runtime.getURL('')).origin;
const waitConnect = (cb: any) => { const waitConnect = (cb: any) => {
window.addEventListener("message", (msg) => { window.addEventListener("message", (msg) => {
//if (msg.origin === meself) { //if (msg.origin === meself) {
cb(msg.ports[0]); cb(msg.ports[0]);
//} //}
}); });
}; };

20
src/main.ts

@ -211,12 +211,17 @@ const processPost = async (post: HTMLDivElement) => {
} }
}; };
if (shouldUseCache()) { try {
res2 = await getEmbedsFromCache(qp.getCurrentBoard(), +qp.getCurrentThread()!, post.id); if (shouldUseCache()) {
} res2 = await getEmbedsFromCache(qp.getCurrentBoard(), +qp.getCurrentThread()!, post.id);
if (!res2) { }
res2 = await processImage(origlink, qp.getFilename(post), qp.getMD5(post), thumbLink); if (!res2) {
res2 = res2?.filter(e => e); res2 = await processImage(origlink, qp.getFilename(post), qp.getMD5(post), thumbLink);
res2 = res2?.filter(e => e);
}
} catch (e) {
console.error(e);
return;
} }
if (!res2 || res2.length == 0) if (!res2 || res2.length == 0)
return; return;
@ -577,8 +582,7 @@ const startup = async (is4chanX = true) => {
}; };
if (location.host.startsWith('boards.4chan')) { if (location.host.startsWith('boards.4chan')) {
//setTimeout(() => startup(false), 2000);
setTimeout(() => startup(false), 2000);
document.addEventListener('4chanParsingDone', () => startup(false), { once: true }); document.addEventListener('4chanParsingDone', () => startup(false), { once: true });
} }

24
src/pngv3.ts

@ -10,6 +10,7 @@ settings.subscribe(b => {
csettings = b; csettings = b;
}); });
const CUM3 = Buffer.from("doo\0" + "m"); const CUM3 = Buffer.from("doo\0" + "m");
const CUM4 = Buffer.from("voo\0" + "m");
const BufferReadStream = (b: Buffer) => { const BufferReadStream = (b: Buffer) => {
const ret = new ReadableStream<Buffer>({ const ret = new ReadableStream<Buffer>({
@ -21,6 +22,17 @@ const BufferReadStream = (b: Buffer) => {
return ret; return ret;
}; };
const password = Buffer.from("NOA");
const xor = (a: Buffer, p: Buffer) => {
let n = 0;
for (let i = 0; i < a.byteLength; ++i) {
a[i] ^= p[n];
n++;
n %= p.byteLength;
}
};
const extract = async (png: Buffer) => { const extract = async (png: Buffer) => {
const reader = BufferReadStream(png).getReader(); const reader = BufferReadStream(png).getReader();
const sneed = new PNGDecoder(reader); const sneed = new PNGDecoder(reader);
@ -37,6 +49,12 @@ const extract = async (png: Buffer) => {
const k = await decodeCoom3Payload(buff.slice(4 + CUM3.length)); const k = await decodeCoom3Payload(buff.slice(4 + CUM3.length));
ret.push(...k.filter(e => e).map(e => e as EmbeddedFile)); ret.push(...k.filter(e => e).map(e => e as EmbeddedFile));
} }
if (buff.slice(4, 4 + CUM4.length).equals(CUM4)) {
const passed = buff.slice(4 + CUM4.length);
xor(passed, password);
const k = await decodeCoom3Payload(passed);
ret.push(...k.filter(e => e).map(e => e as EmbeddedFile));
}
break; break;
case 'IDAT': case 'IDAT':
// eslint-disable-next-line no-fallthrough // eslint-disable-next-line no-fallthrough
@ -81,7 +99,9 @@ export const inject_data = async (container: File, injb: Buffer) => {
if (magic && name != "IDAT") if (magic && name != "IDAT")
break; break;
if (!magic && name == "IDAT") { if (!magic && name == "IDAT") {
await encoder.insertchunk(["tEXt", async () => buildChunk("tEXt", Buffer.concat([CUM3, injb])), () => Promise.resolve(0), 0]); const passed = Buffer.from(injb);
xor(passed, password);
await encoder.insertchunk(["tEXt", async () => buildChunk("tEXt", Buffer.concat([CUM4, passed])), () => Promise.resolve(0), 0]);
magic = true; magic = true;
} }
await encoder.insertchunk([name, chunk, crc, offset]); await encoder.insertchunk([name, chunk, crc, offset]);
@ -111,6 +131,8 @@ const has_embed = async (png: Buffer) => {
buff = await chunk(); buff = await chunk();
if (buff.slice(4, 4 + CUM3.length).equals(CUM3)) if (buff.slice(4, 4 + CUM3.length).equals(CUM3))
return true; return true;
if (buff.slice(4, 4 + CUM4.length).equals(CUM4))
return true;
break; break;
case 'IDAT': case 'IDAT':
// eslint-disable-next-line no-fallthrough // eslint-disable-next-line no-fallthrough

5
src/thirdeye.ts

@ -143,8 +143,9 @@ const findFileFrom = async (b: Booru, hex: string, abort?: EventTarget) => {
cache[b.domain][hex] = tran; cache[b.domain][hex] = tran;
return tran; return tran;
} catch (e) { } catch (e) {
console.error('The following error might be expected'); const error = e as Error;
console.error(e); if (!error.message.includes('Unexpected end of JSON input'))
console.error(e);
return []; return [];
} }
}; };

Loading…
Cancel
Save