Last active
September 11, 2019 05:46
-
-
Save 0xBADDCAFE/57d71ef16ecd89eaafd4f90f94dd0ad8 to your computer and use it in GitHub Desktop.
Upload clipboard image to imgur and paste link
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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; | |
} | |
}; | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
アップロード中にチャンネル切り替えたりするとどうなるかわからん