Browse Source

Add URL-encoded support for host formats with filename refs

pull/46/head
coomdev 2 years ago
parent
commit
293bfe9dc3
  1. 38
      src/pomf.ts

38
src/pomf.ts

@ -5,9 +5,10 @@ import thumbnail from "./assets/hasembed.png";
import { settings } from "./stores"; import { settings } from "./stores";
const sources = [ const sources = [
{ host: 'Catbox', prefix: 'https://files.catbox.moe/' }, { host: 'Catbox', prefix: 'files.catbox.moe/' },
{ host: 'Litter', prefix: 'https://litter.catbox.moe/' }, { host: 'Litter', prefix: 'litter.catbox.moe/' },
{ host: 'Pomf', prefix: 'https://a.pomf.cat/' }, { host: 'Zzzz', prefix: 'z.zz.fo/' },
{ host: 'Pomf', prefix: 'a.pomf.cat/' },
]; ];
export let csettings: Parameters<typeof settings['set']>[0]; export let csettings: Parameters<typeof settings['set']>[0];
@ -20,28 +21,39 @@ const getExt = (fn: string) => {
const isB64 = fn!.match(/^((?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=))?\.(gif|jpe?g|png|webm)/); const isB64 = fn!.match(/^((?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=))?\.(gif|jpe?g|png|webm)/);
const isExt = fn!.match(/\[.*=(.*)\]/); const isExt = fn!.match(/\[.*=(.*)\]/);
let ext; let ext;
let source: string | undefined;
try { try {
if (isDum) { if (isDum) {
ext = fn.split('.').slice(0, -1).join('.'); ext = fn.split('.').slice(0, -1).join('.');
} else if (isB64) { } else if (isB64) {
ext = atob(isB64[1]); ext = atob(isB64[1]);
} else if (isExt) { } else if (isExt) {
ext = isExt[1]; ext = decodeURIComponent(isExt[1]);
if (ext.startsWith('https://'))
ext = ext.slice('https://'.length);
for (const cs of sources)
if (ext.startsWith(cs.prefix)) {
source = cs.prefix;
ext = ext.slice(cs.prefix.length);
break;
}
} }
} catch { } catch {
/**/ /**/
} }
return ext; return { ext, source };
}; };
const extract = async (b: Buffer, fn?: string) => { const extract = async (b: Buffer, fn?: string) => {
const ext = getExt(fn!); const { ext, source } = getExt(fn!);
let rsource: string; let rsource: string;
for (const source of sources) { for (const cs of sources) {
if (source && cs.prefix != source)
continue;
try { try {
await GM_head(source.prefix + ext); await GM_head('https://' + cs.prefix + ext);
rsource = source.prefix + ext; rsource = 'https://' + cs.prefix + ext;
break; break;
} catch { } catch {
// 404 // 404
@ -62,12 +74,14 @@ const extract = async (b: Buffer, fn?: string) => {
}; };
const has_embed = async (b: Buffer, fn?: string) => { const has_embed = async (b: Buffer, fn?: string) => {
const ext = getExt(fn!); const { ext, source } = getExt(fn!);
if (!ext) if (!ext)
return false; return false;
for (const source of sources) { for (const cs of sources) {
if (source && cs.prefix != source)
continue;
try { try {
const e = await GM_head(source.prefix + ext); const e = await GM_head('https://' + cs.prefix + ext);
return true; return true;
} catch { } catch {
// 404 // 404

Loading…
Cancel
Save