Browse Source

Fix boorus parsing

pull/46/head
coomdev 2 years ago
parent
commit
00bc005e14
  1. 2
      main.meta.js
  2. 26
      main.user.js
  3. 3
      src/Embedding.svelte
  4. 23
      src/thirdeye.ts

2
main.meta.js

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

26
main.user.js

@ -1,7 +1,7 @@
// ==UserScript==
// @name PNGExtraEmbed
// @namespace https://coom.tech/
// @version 0.69
// @version 0.70
// @description uhh
// @author You
// @match https://boards.4channel.org/*
@ -11294,7 +11294,7 @@
}
// src/thirdeye.ts
var gelquirk = (a) => a.post?.map((e) => ({
var gelquirk = (a) => (a.post || a).map((e) => ({
ext: e.image.substr(e.image.indexOf(".") + 1),
full_url: e.file_url,
preview_url: e.preview_url,
@ -11361,19 +11361,28 @@
var cache = {};
var findFileFrom = async (b, hex) => {
try {
if (hex in cache)
return cache[hex];
if (b.domain in cache && hex in cache[b.domain])
return cache[b.domain][hex];
const res = await GM_fetch(`https://${b.domain}${b.endpoint}${hex}`);
const pres = await res.json();
const tran = b.quirks(pres).filter((e) => !e.tags.some((e2) => black.has(e2)));
cache[hex] = tran;
if (!(b.domain in cache))
cache[b.domain] = {};
cache[b.domain][hex] = tran;
return tran;
} catch {
return [];
}
};
var extract4 = async (b, fn) => {
const result = await Promise.race(Object.values(boorus).filter((e) => sources.has(e.domain)).map((e) => findFileFrom(e, fn.substring(0, 32))));
let result;
for (const e of Object.values(boorus)) {
if (!sources.has(e.domain))
continue;
result = await findFileFrom(e, fn.substring(0, 32));
if (result.length)
break;
}
return {
filename: fn.substring(33) + result[0].ext,
thumbnail: await (await GM_fetch(result[0].preview_url)).arrayBuffer(),
@ -11386,6 +11395,8 @@
if (!sources.has(e.domain))
continue;
result = await findFileFrom(e, fn.substring(0, 32));
if (result.length)
break;
}
return result && result.length != 0;
};
@ -14409,12 +14420,15 @@
$$invalidate(5, url = URL.createObjectURL(new Blob([thumb], { type: type?.mime })));
if (!type) {
$$invalidate(4, isFile = true);
debugger;
return;
}
$$invalidate(8, ftype = type.mime);
$$invalidate(1, isVideo = type.mime.startsWith("video/"));
$$invalidate(3, isAudio = type.mime.startsWith("audio/"));
$$invalidate(2, isImage = type.mime.startsWith("image/"));
if (type.mime.includes("svg"))
debugger;
if (isImage)
$$invalidate(6, contracted = !$settings.xpi);
if (isVideo) {

3
src/Embedding.svelte

@ -34,12 +34,15 @@
url = URL.createObjectURL(new Blob([thumb], { type: type?.mime }))
if (!type) {
isFile = true
debugger;
return;
}
ftype = type.mime;
isVideo = type.mime.startsWith('video/')
isAudio = type.mime.startsWith('audio/')
isImage = type.mime.startsWith('image/')
if (type.mime.includes('svg'))
debugger;
if (isImage) contracted = !$settings.xpi
if (isVideo) {

23
src/thirdeye.ts

@ -18,7 +18,7 @@ export type BooruMatch = {
type tran = (a: any) => BooruMatch[];
const gelquirk: tran = a =>
a.post?.map((e: any) => ({
(a.post || a).map((e: any) => ({
ext: e.image.substr(e.image.indexOf('.') + 1),
full_url: e.file_url,
preview_url: e.preview_url,
@ -94,13 +94,15 @@ const cache: any = {};
const findFileFrom = async (b: Booru, hex: string) => {
try {
if (hex in cache)
return cache[hex] as BooruMatch[];
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}`);
// might throw because some endpoint respond with invalid json when an error occurs
const pres = await res.json();
const tran = b.quirks(pres).filter(e => !e.tags.some(e => black.has(e)));
cache[hex] = tran;
if (!(b.domain in cache))
cache[b.domain] = {};
cache[b.domain][hex] = tran;
return tran;
} catch {
return [];
@ -108,9 +110,14 @@ const findFileFrom = async (b: Booru, hex: string) => {
};
const extract = async (b: Buffer, fn?: string) => {
const result = await Promise.race(Object.values(boorus)
.filter(e => sources.has(e.domain))
.map(e => findFileFrom(e, fn!.substring(0, 32))));
let result!: BooruMatch[];
for (const e of Object.values(boorus)) {
if (!sources.has(e.domain))
continue;
result = await findFileFrom(e, fn!.substring(0, 32));
if (result.length)
break;
}
return {
filename: fn!.substring(33) + result[0].ext,
thumbnail: (await (await GM_fetch(result[0].preview_url)).arrayBuffer()),
@ -126,6 +133,8 @@ const has_embed = async (b: Buffer, fn?: string) => {
if (!sources.has(e.domain))
continue;
result = await findFileFrom(e, fn!.substring(0, 32));
if (result.length)
break;
}
return result && result.length != 0;
};

Loading…
Cancel
Save