From e496d16a3a556f6a7d821cd572e6f4759a35339c Mon Sep 17 00:00:00 2001 From: coomdev Date: Mon, 8 Aug 2022 13:05:25 +0200 Subject: [PATCH] Fix third eye loading --- src/background.ts | 2 + src/platform.ts | 51 ++++++++++++++++++------ src/processor.worker.ts | 87 +++++++++++++++++++++++------------------ src/stores.ts | 33 ++++------------ src/thirdeye.ts | 2 +- 5 files changed, 96 insertions(+), 79 deletions(-) diff --git a/src/background.ts b/src/background.ts index 0e6a7e2..6f059cb 100644 --- a/src/background.ts +++ b/src/background.ts @@ -199,9 +199,11 @@ const onMessage = (c: MessagePort, cb: any) => // eslint-disable-next-line no-constant-condition while (true) { const c = await new Promise(waitConnect); + console.log('New connection'); const pendingFetches = new Map(); onMessage(c, async (obj: any) => { + console.log(obj); const { id, name, args, sid, fid, url } = obj as any; if (name == "keepAlive") { console.log('im alive, tho?'); diff --git a/src/platform.ts b/src/platform.ts index a17cf6b..4a94e64 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -2,11 +2,6 @@ import { GM_fetch, GM_head, headerStringToObject } from './requests'; export const lqueue = {} as any; -const localLoad = (key: string, def: T) => - ('__pee__' + key) in localStorage - ? JSON.parse(localStorage.getItem('__pee__' + key)!) as T - : def; - const localSet = (key: string, value: any) => localStorage.setItem('__pee__' + key, JSON.stringify(value)); @@ -46,11 +41,20 @@ if (execution_mode != 'userscript' && !isBackground && execution_mode != 'worker }); } +let msgBuff: [any, Transferable[] | undefined][] = []; + export const setupPort = (port: MessagePort) => { port1 = port; port1.onmessage = (ev) => { lqueue[ev.data.id](ev.data); }; + + if (execution_mode == "worker") { + for (const msg of msgBuff) { + port.postMessage(msg[0], { transfer: msg[1] }); + } + msgBuff = []; + } }; // will be later overwritten if it's not launched from the userscript @@ -60,11 +64,7 @@ if (execution_mode == "worker") { lqueue[ev.data.id](ev.data); }, postMessage(msg, tr?: Transferable[]) { - (postMessage as any)({ - type: 'ipc', - tr, - msg - }, tr); + msgBuff.push([msg, tr]); } } as MessagePort; } @@ -97,7 +97,9 @@ export const sendCmd = (cmd: any, tr?: Transferable[], overwrite = false, tod }; const bridge = V>(name: string, f: T) => { - if (execution_mode != 'userscript' && !isBackground && execution_mode != 'worker') + if (execution_mode == 'userscript') + return f; + if (isBackground) return f; // It has to be the background script return (...args: U) => { @@ -162,8 +164,31 @@ export class Platform { return obj.tabs.create({ active: opts.active, url: src, index: i }); } - static getValue(name: string, def: T) { - return localLoad(name, def); + static async getValue(key: string, def: T) { + const isinls = ('__pee__' + key) in localStorage; + let ret: T; + if (isinls) { + let it = localStorage.getItem('__pee__' + key); + if (it === "undefined") + it = null; + ret = { ...def, ...JSON.parse(it || '{}') } as T; + } else + ret = def; + + if (execution_mode != "userscript") { + if (isinls) { + delete localStorage[('__pee__' + key)]; + await chrome.storage.local.set({ + [key]: JSON.stringify(ret) + }); + } else { + const d = await chrome.storage.local.get([key]); + if (typeof d[key] == "string") + return { ...def, ...(await JSON.parse('' + d[key] || '{}')) } as T; + } + } + + return ret; } static setValue(name: string, val: any) { diff --git a/src/processor.worker.ts b/src/processor.worker.ts index 3f881be..9902572 100644 --- a/src/processor.worker.ts +++ b/src/processor.worker.ts @@ -2,12 +2,15 @@ /// import * as platform from './platform'; -import pngv3 from "./pngv3"; +import { initial_settings } from './stores'; +//import { initial_settings } from './stores'; +//import { initial_settings } from './stores'; +//import pngv3 from "./pngv3"; //import webm from "./webm"; //import gif from "./gif"; -import jpg from "./jpg"; +//import jpg from "./jpg"; import thirdeye from "./thirdeye"; -import pomf from "./pomf"; +//import pomf from "./pomf"; console.log("Worker started"); @@ -99,7 +102,7 @@ export interface ImageProcessor { } const processors: ImageProcessor[] = - [thirdeye, pomf, pngv3, jpg]; //, webm, gif + [thirdeye];//, pomf, pngv3, jpg]; //, webm, gif const processImage = async (srcs: AsyncGenerator, fn: string, hex: string, prevurl: string) => { const ret = await Promise.all(processors.filter(e => e.match(fn)).map(async proc => { @@ -155,44 +158,50 @@ const processImage = async (srcs: AsyncGenerator, fn: string return ret.filter(e => e).map(e => e!); }; -onmessage = async (msg: MessageEvent) => { - const des = deserializeMessage(msg.data); - switch (des.type) { - case 'ipc': { - platform.setupPort(des.port); - break; - } - case 'cmd': { - switch (des.fun) { - case 'processImage': { - //console.log('Received process image command', des); - const res = await processImage(des.args[0], des.args[1], des.args[2], des.args[3]); - //console.log('Finished process image command', des); - const tr: Transferable[] = []; - for (const ef of res) { - for (const e of ef[0]) { - if (Buffer.isBuffer(e.thumbnail) || e.thumbnail instanceof Uint8Array) - tr.push(e.thumbnail.buffer); - if (Buffer.isBuffer(e.data) || e.data instanceof Uint8Array) - tr.push(e.data.buffer); +const snooze = (n:number) => new Promise(_ => setTimeout(_, n)); + +(async () => { + onmessage = async (msg: MessageEvent) => { + console.log(msg.data); + const des = deserializeMessage(msg.data); + switch (des.type) { + case 'ipc': { + platform.setupPort(des.port); + break; + } + case 'cmd': { + switch (des.fun) { + case 'processImage': { + await initial_settings; + //console.log('Received process image command', des); + const res = await processImage(des.args[0], des.args[1], des.args[2], des.args[3]); + //console.log('Finished process image command', des); + const tr: Transferable[] = []; + for (const ef of res) { + for (const e of ef[0]) { + if (Buffer.isBuffer(e.thumbnail) || e.thumbnail instanceof Uint8Array) + tr.push(e.thumbnail.buffer); + if (Buffer.isBuffer(e.data) || e.data instanceof Uint8Array) + tr.push(e.data.buffer); + } } + //console.log('Sent reply', res, des); + postMessage({ + type: 'reply', + id: des.id, + res + }, [...new Set(tr)]); } - //console.log('Sent reply', res, des); - postMessage({ - type: 'reply', - id: des.id, - res - }, [...new Set(tr)]); } + break; } - break; - } - case 'ag': { - const cb = pendinggens[des.id].shift(); - if (cb) { - cb(des.res); + case 'ag': { + const cb = pendinggens[des.id].shift(); + if (cb) { + cb(des.res); + } + break; } - break; } - } -}; + }; +})(); \ No newline at end of file diff --git a/src/stores.ts b/src/stores.ts index 25346b0..543c10b 100644 --- a/src/stores.ts +++ b/src/stores.ts @@ -1,40 +1,21 @@ -import { writable } from "svelte/store"; import type { HydrusClient } from "./hydrus"; import type { Booru } from "./thirdeye"; +import { writable } from "svelte/store"; +import { Platform } from "./platform"; + // Todo: use GM get/setValue instead? export const localLoad = async (key: string, def: T) => { + const ret = await Platform.getValue(key, def); if (execution_mode == "worker") { - return def; + return (ret as any).res; } - const isinls = ('__pee__' + key) in localStorage; - let ret: T; - if (isinls) { - let it = localStorage.getItem('__pee__' + key); - if (it === "undefined") - it = null; - ret = { ...def, ...JSON.parse(it || '{}') } as T; - } else - ret = def; - - if (execution_mode != "userscript") { - if (isinls) { - delete localStorage[('__pee__' + key)]; - await chrome.storage.local.set({ - [key]: JSON.stringify(ret) - }); - } else { - const d = await chrome.storage.local.get([key]); - if (typeof d[key] == "string") - return { ...def, ...(await JSON.parse('' + d[key] || '{}')) } as T; - } - } - + return ret; }; const localSet = (key: string, value: any) => { - if (execution_mode == "worker") { + if (execution_mode == "worker") { return; } if (execution_mode != "userscript") diff --git a/src/thirdeye.ts b/src/thirdeye.ts index f4d0578..69cb6b8 100644 --- a/src/thirdeye.ts +++ b/src/thirdeye.ts @@ -197,7 +197,7 @@ const hammingDist = (a: string, b: string) => { const has_embed = async (b: Buffer, fn?: string, prevlink?: string) => { // It's not worth to bother skipping images with filenames that match their md5 because // 4chan reencodes jpegs, which is well over half the files posted - + //debugger; // ok fine you autists if (Buffer.from(fn!, 'hex').equals(b)) return false;