Browse Source

It's fucking over

pull/46/head
coomdev 2 years ago
parent
commit
30ce61cfab
  1. 2
      main.meta.js
  2. 835
      main.user.js
  3. 21
      src/App.svelte
  4. 31
      src/requests.ts
  5. 5
      src/stores.ts
  6. 55
      src/thirdeye.ts

2
main.meta.js

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

835
main.user.js

File diff suppressed because it is too large

21
src/App.svelte

@ -1,10 +1,10 @@
<script lang="ts">
import { hasContext, onDestroy } from 'svelte'
import Dialog from './Dialog.svelte';
import Dialog from './Dialog.svelte';
import { settings } from './stores'
import Tag from './Tag.svelte'
import type { Booru } from './thirdeye';
import type { Booru } from './thirdeye';
let newbooru: Partial<Omit<Booru, 'quirks'> & {view: string}> = {};
let dial: Dialog;
@ -65,7 +65,7 @@ import type { Booru } from './thirdeye';
</label>
<label>
<input type="checkbox" bind:checked={$settings.dh} />
Turn off hover preview.
Disable hover preview.
</label>
<label>
<input type="checkbox" bind:checked={$settings.eye} />
@ -96,15 +96,22 @@ import type { Booru } from './thirdeye';
<label>
<input type="checkbox" bind:checked={$settings.ep} />
<!-- svelte-ignore a11y-missing-attribute -->
Turn off embedded file preloading<a
Disable embedded file preloading<a
title="You might still want to enable 'preload external files'">?</a
>
</label>
<label>
<input type="checkbox" bind:checked={$settings.te} />
Turn off third-eye.
Disable third-eye.
</label>
{#if !$settings.te}
<label>
<input type="checkbox" bind:checked={$settings.expte} />
<!-- svelte-ignore a11y-missing-attribute -->
Use Experimental Query API<a title="Can be up to 30% faster, reduces strain on boorus, may break">?</a
>
</label>
<h3>Booru sources</h3>
<div class="tagcont">
{#each $settings.rsources as source, i}
@ -193,10 +200,6 @@ import type { Booru } from './thirdeye';
flex-wrap: wrap;
}
select {
font-size: 1.2em;
}
.enabled {
display: block;
}

31
src/requests.ts

@ -1,3 +1,5 @@
import { localLoad, settings } from "./stores";
const xmlhttprequest = typeof GM_xmlhttpRequest != 'undefined' ?
GM_xmlhttpRequest :
(typeof GM != "undefined" ?
@ -76,4 +78,31 @@ export function GM_fetch(...[url, opt, lisn]: [...Parameters<typeof fetch>, Even
};
xmlhttprequest(gmopt);
});
}
}
const makePoolable = <T extends any[], U>(fun: (...args: T) => Promise<U>, getPoolSize: () => number) => {
const pool = [];
let pending = 0;
const poolFree: (() => void)[] = [];
return async (...args: T) => {
while (pending >= getPoolSize())
await new Promise<void>(_ => poolFree.push(_));
pending++;
const prom = fun(...args);
prom.then(() => {
pending--;
poolFree.forEach(_ => _());
poolFree.length = 0;
});
return prom;
};
};
let csettings: Parameters<typeof settings['set']>[0] = localLoad('settingsv2', {} as any);
settings.subscribe(s => {
csettings = s;
});
const poolFetch = makePoolable(GM_fetch, () => csettings.conc);

5
src/stores.ts

@ -22,6 +22,8 @@ export const settings = writable(localLoad('settingsv2', {
prev: false,
sh: false,
ep: false,
expte: false,
conc: 8,
ho: false,
blacklist: ['guro', 'scat', 'ryona', 'gore'],
rsources: [{
@ -66,7 +68,8 @@ export const settings = writable(localLoad('settingsv2', {
domain: "booru.allthefallen.moe",
endpoint: "/posts.json?tags=md5:",
view: 'https://booru.allthefallen.moe/posts/'
}] as (Omit<Booru, 'quirks'> & {view: string, disabled?: boolean})[]
}] as (Omit<Booru, 'quirks'> & {view: string, disabled?: boolean})[],
...localLoad('settingsv2', {}),
}));
export const appState = writable({

55
src/thirdeye.ts

@ -1,6 +1,7 @@
import type { EmbeddedFile, ImageProcessor } from "./main";
import { GM_fetch } from "./requests";
import { localLoad, settings } from "./stores";
import { Buffer } from "buffer";
export type Booru = {
disabled?: boolean;
@ -41,7 +42,9 @@ const gelquirk: (s: string) => tran = prefix => (a =>
tags: (e.tag_string || e.tags || '').split(' ')
} as BooruMatch)) || []);
let experimentalApi = false;
settings.subscribe(s => {
experimentalApi = s.expte;
boorus = s.rsources.map(e => ({
...e,
quirks: gelquirk(e.view)
@ -60,10 +63,61 @@ settings.subscribe(s => {
black = new Set(s.blacklist);
});
const bufferingTime = 2000;
let expired: number | undefined = undefined;
type ApiResult = { [md5 in string]: { [domain in string]: BooruMatch[] } };
let reqQueue: [string, (a: ApiResult) => void][] = [];
let unlockQueue = Promise.resolve();
const queryCache: ApiResult = {};
const processQueries = async () => {
console.log("======== FIRIN =======");
let unlock!: () => void;
unlockQueue = new Promise<void>(_ => unlock = _);
const md5 = reqQueue.map(e => e[0]).filter(e => !(e in queryCache));
expired = undefined;
if (md5.length > 0) {
const res = await fetch("https://shoujo.coom.tech/api", {
method: "POST",
body: JSON.stringify({ md5 }),
headers: {
'content-type': 'application/json'
}
});
const results: ApiResult = await res.json();
Object.entries(results).forEach(e => queryCache[e[0]] = e[1]);
}
reqQueue.forEach(e => e[1]({ [e[0]]: queryCache[e[0]] }));
reqQueue = [];
unlock();
};
const queueForProcessing = async (hex: string, cb: (a: ApiResult) => void) => {
console.log("putting", hex, 'in queue');
await unlockQueue;
console.log("put", hex, 'in queue');
reqQueue.push([hex, cb]);
if (!expired) {
expired = setTimeout(processQueries, bufferingTime);
}
};
const cache: any = {};
const shoujoFind = async (hex: string): Promise<ApiResult> => {
return new Promise(res => {
queueForProcessing(hex, res);
});
};
const findFileFrom = async (b: Booru, hex: string, abort?: EventTarget) => {
try {
if (experimentalApi) {
const res = await shoujoFind(hex);
if (!res)
debugger;
return hex in res ? (res[hex][b.domain] || []) : [];
}
if (b.domain in cache && hex in cache[b.domain])
return cache[b.domain][hex] as BooruMatch[];
const res = await GM_fetch(`https://${b.domain}${b.endpoint}${hex}`);
@ -86,6 +140,7 @@ const extract = async (b: Buffer, fn?: string) => {
if (e.disabled)
continue;
result = await findFileFrom(e, fn!.substring(0, 32));
if (result.length) {
booru = e.name;
break;

Loading…
Cancel
Save