Skip to content

Instantly share code, notes, and snippets.

@0xBADDCAFE
Last active September 11, 2019 05:46
Show Gist options
  • Save 0xBADDCAFE/57d71ef16ecd89eaafd4f90f94dd0ad8 to your computer and use it in GitHub Desktop.
Save 0xBADDCAFE/57d71ef16ecd89eaafd4f90f94dd0ad8 to your computer and use it in GitHub Desktop.
Upload clipboard image to imgur and paste link
// ==UserScript==
// @name kokoro.io clipboard image uploader
// @namespace http://0xbd.cf/
// @version 0.1
// @description Upload clipboard image to imgur and paste link
// @author You
// @match https://kokoro.io/
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Put your access key for upload with account
// const auth = "Bearer YOUR_ACCESS_TOKEN";
// Or client id for anonymous upload
// const auth = "Client-ID YOUR_CLIENT_ID";
document.addEventListener('paste', async ev => {
const textArea = document.querySelector('#say > textarea');
if (!textArea) return;
const items = (ev.clipboardData || ev.originalEvent.clipboardData).items;
for (const item of items) {
if (item.type.indexOf("image") != -1) {
// console.log('Clipboard has image');
ev.preventDefault();
const blob = item.getAsFile();
const method = "POST";
const headers = {
'Authorization': auth
};
const body = new FormData();
body.append('image', blob);
const placeholderOrig = textArea.placeholder;
textArea.placeholder = 'Uploading...';
textArea.disabled = true;
try {
const response = await fetch("https://api.imgur.com/3/image", {method, headers, body});
const json = await response.json()
textArea.value = json.data.link;
// Fake event to enable send with enter key
textArea.dispatchEvent(new Event('input'));
console.dir(json);
} catch(e) {
alert('Failed to upload (See the console log)');
console.error(e);
}
textArea.placeholder = placeholderOrig;
textArea.disabled = false;
}
};
});
})();
@0xBADDCAFE
Copy link
Author

アップロード中にチャンネル切り替えたりするとどうなるかわからん

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment