From 5b402c205ef038f899838ad9a352610e4bac5cc1 Mon Sep 17 00:00:00 2001 From: coomdev Date: Tue, 29 Mar 2022 04:29:55 +0200 Subject: [PATCH] Throw an error if the user forgets to embed --- main.meta.js | 2 +- main.user.js | 54 ++++++++++++++++++++++++++++--- src/Components/PostOptions.svelte | 3 +- src/main.ts | 35 +++++++++++++++++++- 4 files changed, 86 insertions(+), 8 deletions(-) diff --git a/main.meta.js b/main.meta.js index 3dc5300..1dba47f 100644 --- a/main.meta.js +++ b/main.meta.js @@ -1,7 +1,7 @@ // ==UserScript== // @name PNGExtraEmbed // @namespace https://coom.tech/ -// @version 0.174 +// @version 0.175 // @description uhh // @author You // @match https://boards.4channel.org/* diff --git a/main.user.js b/main.user.js index 4c9ef81..9047a64 100644 --- a/main.user.js +++ b/main.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name PNGExtraEmbed // @namespace https://coom.tech/ -// @version 0.174 +// @version 0.175 // @description uhh // @author You // @match https://boards.4channel.org/* @@ -81,7 +81,7 @@ var define_BUILD_VERSION_default; var init_define_BUILD_VERSION = __esm({ ""() { - define_BUILD_VERSION_default = [0, 174]; + define_BUILD_VERSION_default = [0, 175]; } }); @@ -17312,7 +17312,7 @@ component_subscribe($$self, appState, ($$value) => $$invalidate(1, $appState = $$value)); let { processors: processors2 = [] } = $$props; let { textinput } = $$props; - let files = []; + let { files = [] } = $$props; const addContent = (...newfiles) => { $$invalidate(0, files = [...files, ...newfiles]); if (files.length > $settings.maxe) { @@ -17346,6 +17346,7 @@ name: file.name } })); + $$invalidate(0, files = []); fireNotification("success", `File${files.length > 1 ? "s" : ""} successfully embedded!`); } catch (err) { const e2 = err; @@ -17369,6 +17370,8 @@ $$invalidate(6, processors2 = $$props2.processors); if ("textinput" in $$props2) $$invalidate(5, textinput = $$props2.textinput); + if ("files" in $$props2) + $$invalidate(0, files = $$props2.files); }; return [ files, @@ -17384,7 +17387,7 @@ var PostOptions = class extends SvelteComponent { constructor(options) { super(); - init(this, options, instance9, create_fragment9, safe_not_equal, { processors: 6, textinput: 5 }, add_css8); + init(this, options, instance9, create_fragment9, safe_not_equal, { processors: 6, textinput: 5, files: 0 }, add_css8); } get processors() { return this.$$.ctx[6]; @@ -17400,6 +17403,13 @@ this.$$set({ textinput }); flush(); } + get files() { + return this.$$.ctx[0]; + } + set files(files) { + this.$$set({ files }); + flush(); + } }; var PostOptions_default = PostOptions; @@ -21317,10 +21327,21 @@ }); document.addEventListener("QRDialogCreation", (e) => { const a = document.createElement("span"); - new PostOptions_default({ + const po = new PostOptions_default({ target: a, props: { processors, textinput: (e.detail || e.target).querySelector("textarea") } }); + document.addEventListener("keydown", (e2) => { + if (e2.ctrlKey && (e2.key == "Enter" || e2.keyCode == 13)) { + if (po.files.length > 0) { + e2.preventDefault(); + e2.stopImmediatePropagation(); + e2.stopPropagation(); + fireNotification("error", "You have files you forgot to embed!", 3e3); + return false; + } + } + }, true); let target; if (!cappState.is4chanX) { target = e.detail; @@ -21329,6 +21350,29 @@ } else { target = e.target; target.querySelector("#qr-filename-container")?.appendChild(a); + const sub = target.querySelector("input[type=submit]"); + sub.addEventListener("click", (e2) => { + if (po.files.length > 0) { + e2.preventDefault(); + e2.stopImmediatePropagation(); + e2.stopPropagation(); + fireNotification("error", "You have files you forgot to embed!", 3e3); + return false; + } + }, true); + const obs = new MutationObserver((m) => { + for (const r of m) { + switch (r.type) { + case "attributes": + break; + case "characterData": + break; + } + } + }); + obs.observe(sub, { + attributes: true + }); } }, { once: !cappState.is4chanX }); var customStyles = document.createElement("style"); diff --git a/src/Components/PostOptions.svelte b/src/Components/PostOptions.svelte index 13d2398..ff522c2 100644 --- a/src/Components/PostOptions.svelte +++ b/src/Components/PostOptions.svelte @@ -7,7 +7,7 @@ export let processors: ImageProcessor[] = [] export let textinput: HTMLTextAreaElement - let files: File[] = [] + export let files: File[] = [] const addContent = (...newfiles: File[]) => { files = [...files, ...newfiles] @@ -51,6 +51,7 @@ detail: { file: new Blob([buff], { type }), name: file.name }, }), ) + files = []; fireNotification( 'success', `File${files.length > 1 ? 's' : ''} successfully embedded!`, diff --git a/src/main.ts b/src/main.ts index 8cf18b3..9468316 100644 --- a/src/main.ts +++ b/src/main.ts @@ -392,11 +392,27 @@ document.addEventListener('ThreadUpdate', (async (e: CustomEvent) => { document.addEventListener('QRDialogCreation', ((e: CustomEvent) => { const a = document.createElement('span'); - new PostOptions({ + const po = new PostOptions({ target: a, props: { processors, textinput: (e.detail || e.target).querySelector('textarea')! } }); + const checkEvent = (e: Event) => { + if ((po as any).files.length > 0) { + e.preventDefault(); + e.stopImmediatePropagation(); + e.stopPropagation(); + fireNotification("error", "You have files you forgot to embed!"); + return false; + } + }; + + document.addEventListener("keydown", (e) => { + if (e.ctrlKey && (e.key == "Enter" || e.keyCode == 13)) { + return checkEvent(e); + } + }, true); + let target; if (!cappState.is4chanX) { target = e.detail; @@ -406,6 +422,23 @@ document.addEventListener('QRDialogCreation', ((e: CustomEvent else { target = e.target as HTMLDivElement; target.querySelector('#qr-filename-container')?.appendChild(a); + const sub = target.querySelector("input[type=submit]") as HTMLElement; + + sub.addEventListener("click", checkEvent, true); + + const obs = new MutationObserver((m) => { + for (const r of m) { + switch (r.type) { + case "attributes": + break; + case "characterData": + break; + } + } + }); + obs.observe(sub, { + attributes: true + }); } }), { once: !cappState!.is4chanX }); // 4chan's normal extension destroys the QR form everytime