Skip to content

Instantly share code, notes, and snippets.

@ninjastic
Created February 23, 2022 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ninjastic/377880cea83c6fed96b94e5dbe258dd5 to your computer and use it in GitHub Desktop.
Save ninjastic/377880cea83c6fed96b94e5dbe258dd5 to your computer and use it in GitHub Desktop.
Adds an button to the post creation form that uploads an image and inserts it in the post
// ==UserScript==
// @name Bitcointalk Image Upload Button
// @version 0.1
// @description Adds an button to the post creation form that uploads an image and inserts it in the post
// @author TryNinja
// @match https://bitcointalk.org/index.php?action=post*
// @icon https://www.google.com/s2/favicons?sz=64&domain=bitcointalk.org
// @grant none
// ==/UserScript==
(function() {
'use strict';
const imgButton = document.querySelector('a:nth-child(24)');
const imgUploadButton = document.createElement('a');
imgUploadButton.innerHTML = `<a href="javascript:void(0);" onclick="document.getElementById('uploadImage').click()"><img src="https://i.imgur.com/ukC9kf1.png" align="bottom" width="23" height="22" alt="Upload and Insert Image" title="Upload and Insert Image"></a>`;
imgButton.after(imgUploadButton);
const uploadInput = document.createElement('input');
uploadInput.id = 'uploadImage';
uploadInput.type = 'file';
uploadInput.accept = 'image/*';
uploadInput.style.display = 'none';
imgButton.after(uploadInput);
const insertImageUrl = imgUrl => {
const postTextArea = document.querySelector('textarea');
postTextArea.value += `\n[img]${imgUrl}[/img]`;
};
uploadInput.onchange = async () => {
const { files } = uploadInput;
if (files[0].size) {
const formData = new FormData();
formData.append('type', 'file');
formData.append('image', files[0]);
console.log('uploading image');
const upload = await fetch('https://api.imgur.com/3/image', {
method: 'POST',
headers: { authorization: 'Client-ID 09727b421c2303e' },
body: formData,
});
const result = await upload.json();
if (result.success) {
insertImageUrl(result.data.link);
}
}
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment