Browse Source

Fix third eye loading

pull/46/head
coomdev 2 years ago
parent
commit
e496d16a3a
  1. 2
      src/background.ts
  2. 51
      src/platform.ts
  3. 87
      src/processor.worker.ts
  4. 33
      src/stores.ts
  5. 2
      src/thirdeye.ts

2
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<MessagePort>(waitConnect);
console.log('New connection');
const pendingFetches = new Map<MessagePort, { [id in number]: { fetchFully: boolean } }>();
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?');

51
src/platform.ts

@ -2,11 +2,6 @@ import { GM_fetch, GM_head, headerStringToObject } from './requests';
export const lqueue = {} as any;
const localLoad = <T>(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 = <V>(cmd: any, tr?: Transferable[], overwrite = false, tod
};
const bridge = <U extends any[], V, T extends (...args: U) => 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<T>(name: string, def: T) {
return localLoad(name, def);
static async getValue<T>(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) {

87
src/processor.worker.ts

@ -2,12 +2,15 @@
/// <reference lib="webworker" />
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<string, void, void>, 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<string, void, void>, fn: string
return ret.filter(e => e).map(e => e!);
};
onmessage = async (msg: MessageEvent<any>) => {
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<any>) => {
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;
}
}
};
};
})();

33
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 <T>(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")

2
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;

Loading…
Cancel
Save