Skip to content

Commit

Permalink
0.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
double-beep committed Oct 15, 2024
1 parent 17839ac commit 1446550
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
| Name | @userscripters/comment-image-upload |
| Description | Userscript for uploading comment images |
| License | [GPL-3.0-or-later](https://spdx.org/licenses/GPL-3.0-or-later) |
| Version | 0.2.1 |
| Version | 0.2.2 |


# Support
Expand Down
2 changes: 1 addition & 1 deletion dist/headers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ==UserScript==
// @name Comment Image Upload
// @namespace userscripters
// @version 0.2.1
// @version 0.2.2
// @author Oleg Valter <[email protected]>
// @contributors double beep (https://github.com/double-beep)
// @description Userscript for uploading comment images
Expand Down
4 changes: 2 additions & 2 deletions dist/legacy/index.min.user.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/legacy/index.user.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ==UserScript==
// @name Comment Image Upload
// @namespace userscripters
// @version 0.2.1
// @version 0.2.2
// @author Oleg Valter <[email protected]>
// @contributors double beep (https://github.com/double-beep)
// @description Userscript for uploading comment images
Expand Down Expand Up @@ -178,10 +178,10 @@ function handleEvent(event) {
target = event.target;
if (!target.matches("textarea.js-comment-text-input"))
return [2];
event.preventDefault();
file = findImage(event);
if (!file)
return [2];
event.preventDefault();
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
Expand Down
4 changes: 2 additions & 2 deletions dist/modern/index.min.user.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ==UserScript==
// @name Comment Image Upload
// @namespace userscripters
// @version 0.2.1
// @version 0.2.2
// @author Oleg Valter <[email protected]>
// @contributors double beep (https://github.com/double-beep)
// @description Userscript for uploading comment images
Expand Down Expand Up @@ -33,4 +33,4 @@
// @homepage https://github.com/userscripters/comment-image-upload#readme
// ==/UserScript==

async function uploadImage(e){var t=new FormData,e=(t.append("file",e),t.append("fkey",StackExchange.options.user.fkey),new URL(location.origin+"/upload/image")),t=await fetch(e,{method:"POST",body:t});if(!t.ok)throw new Error(`Request to ${e.toString()} failed.`);var{Success:e,UploadedImage:a}=await t.json();if(e)return a;throw console.error(t),new Error("Failed to upload image.")}function findImage(e){e=e instanceof ClipboardEvent?e.clipboardData?.items||e.clipboardData?.files:e.dataTransfer?.items||e.dataTransfer?.files;if(e){e=[...e].find(e=>e.type.includes("image/"));if(e){e=e instanceof File?e:e.getAsFile();if(e){var t=StackExchange.settings.image.maxImageUploadSizeInBytes;if(!(e.size>=t))return e}}}}async function insertText(e,t){var a=t.selectionStart,n=`[Uploading ${e.name}...]()`,e=(t.setRangeText(n,a,t.selectionEnd,"start"),await uploadImage(e)),i="enter image description here";t.setRangeText(`[${i}](${e})`,a,a+n.length,"start"),t.setSelectionRange(a+1,a+1+i.length,"forward")}async function handleEvent(e){var t=e.target;if(t.matches("textarea.js-comment-text-input")){e.preventDefault();e=findImage(e);if(e)try{await insertText(e,t)}catch(e){console.error(e)}}}function appendButton(a){let n=document.createElement("input");n.type="file",n.addEventListener("change",async()=>{var e=n.files?.[0],t=a.querySelector("textarea.js-comment-text-input");if(e&&t)try{await insertText(e,t)}catch(e){console.error(e)}});var e=document.createElement("button"),t=(e.classList.add("s-btn","s-btn__link","ta-left","px2"),e.type="button",e.innerText="Upload image",e.addEventListener("click",()=>{n.click()}),a.querySelector(".js-comment-help-link"));t&&(t.parentElement?.classList.add("g4"),t.before(e))}window.addEventListener("paste",handleEvent),window.addEventListener("drop",handleEvent),document.querySelectorAll(".js-add-link").forEach(t=>{t.addEventListener("click",()=>{setTimeout(()=>{var e=t.closest(".post-layout--right");e&&appendButton(e)},200)})});
async function uploadImage(e){var t=new FormData,e=(t.append("file",e),t.append("fkey",StackExchange.options.user.fkey),new URL(location.origin+"/upload/image")),t=await fetch(e,{method:"POST",body:t});if(!t.ok)throw new Error(`Request to ${e.toString()} failed.`);var{Success:e,UploadedImage:a}=await t.json();if(e)return a;throw console.error(t),new Error("Failed to upload image.")}function findImage(e){e=e instanceof ClipboardEvent?e.clipboardData?.items||e.clipboardData?.files:e.dataTransfer?.items||e.dataTransfer?.files;if(e){e=[...e].find(e=>e.type.includes("image/"));if(e){e=e instanceof File?e:e.getAsFile();if(e){var t=StackExchange.settings.image.maxImageUploadSizeInBytes;if(!(e.size>=t))return e}}}}async function insertText(e,t){var a=t.selectionStart,n=`[Uploading ${e.name}...]()`,e=(t.setRangeText(n,a,t.selectionEnd,"start"),await uploadImage(e)),i="enter image description here";t.setRangeText(`[${i}](${e})`,a,a+n.length,"start"),t.setSelectionRange(a+1,a+1+i.length,"forward")}async function handleEvent(e){var t=e.target;if(t.matches("textarea.js-comment-text-input")){var a=findImage(e);if(a){e.preventDefault();try{await insertText(a,t)}catch(e){console.error(e)}}}}function appendButton(a){let n=document.createElement("input");n.type="file",n.addEventListener("change",async()=>{var e=n.files?.[0],t=a.querySelector("textarea.js-comment-text-input");if(e&&t)try{await insertText(e,t)}catch(e){console.error(e)}});var e=document.createElement("button"),t=(e.classList.add("s-btn","s-btn__link","ta-left","px2"),e.type="button",e.innerText="Upload image",e.addEventListener("click",()=>{n.click()}),a.querySelector(".js-comment-help-link"));t&&(t.parentElement?.classList.add("g4"),t.before(e))}window.addEventListener("paste",handleEvent),window.addEventListener("drop",handleEvent),document.querySelectorAll(".js-add-link").forEach(t=>{t.addEventListener("click",()=>{setTimeout(()=>{var e=t.closest(".post-layout--right");e&&appendButton(e)},200)})});
4 changes: 2 additions & 2 deletions dist/modern/index.user.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ==UserScript==
// @name Comment Image Upload
// @namespace userscripters
// @version 0.2.1
// @version 0.2.2
// @author Oleg Valter <[email protected]>
// @contributors double beep (https://github.com/double-beep)
// @description Userscript for uploading comment images
Expand Down Expand Up @@ -88,10 +88,10 @@ async function handleEvent(event) {
const target = event.target;
if (!target.matches("textarea.js-comment-text-input"))
return;
event.preventDefault();
const file = findImage(event);
if (!file)
return;
event.preventDefault();
try {
await insertText(file, target);
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@userscripters/comment-image-upload",
"version": "0.2.1",
"version": "0.2.2",
"description": "Userscript for uploading comment images",
"scripts": {
"test": "exit 0",
Expand Down

0 comments on commit 1446550

Please sign in to comment.