Skip to content

Instantly share code, notes, and snippets.

@marktaiwan
Created January 9, 2021 17:05
Show Gist options
  • Save marktaiwan/8824c5d6d1a760eae6d2878e769fcd07 to your computer and use it in GitHub Desktop.
Save marktaiwan/8824c5d6d1a760eae6d2878e769fcd07 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Ponepaste - load from file
// @version 0.0.1
// @author Marker
// @license WTFPL
// @namespace https://github.com/marktaiwan/
// @match https://ponepaste.org/
// @grant none
// @inject-into content
// @noframes
// ==/UserScript==
(function () {
'use strict';
const $ = (selector, parent = document) => parent.querySelector(selector);
function loadFromFile() {
const filePicker = document.createElement('input');
filePicker.type = 'file';
filePicker.addEventListener('change', () => {
const title = $('form[name="mainForm"] input[name="title"]');
const textarea = $('form[name="mainForm"] textarea.form-control');
if (!title || !textarea) return;
const reader = new FileReader();
const file = filePicker.files[0];
const name = file.name.endsWith('.txt') ? file.name.slice(0, -4) : file.name;
reader.readAsText(file);
reader.onload = () => {
title.value = name.trim();
textarea.value = reader.result;
};
}, {once: true});
filePicker.click();
}
function init() {
const importButton = document.createElement('button');
importButton.innerText = 'Load from file';
importButton.style.marginBottom = '20px';
importButton.addEventListener('click', loadFromFile);
$('form[name="mainForm"]')?.before(importButton);
}
init();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment