dwhelper.user.js
· 2.1 KiB · JavaScript
Raw
// ==UserScript==
// @name DW Helper
// @namespace http://veryroundbird.house/
// @version 1.1
// @description adds some html shortcuts for the dreamwidth comment box
// @author Carly Smallbird
// @match https://*.dreamwidth.org/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=dreamwidth.org
// @grant none
// ==/UserScript==
// the guts of this userscript
function main() {
function insertText(before, after) {
const textarea = document.querySelector('.qr-body textarea');
const cursorPos = textarea.selectionStart;
const selectionLength = textarea.selectionStart !== textarea.selectionEnd ? textarea.selectionEnd - textarea.selectionStart : 0;
console.log(selectionLength);
const v = document.querySelector('.qr-body textarea').value;
const textBefore = v.substring(0, cursorPos);
const textAfter = v.substring(textarea.selectionEnd, v.length);
const textInner = v.substring(cursorPos, selectionLength);
textarea.value = textBefore + before + textInner + after + textAfter;
}
function createButton(parent, text, before, after) {
const btn = document.createElement('button');
btn.type = 'button';
btn.style.paddingTop = 0;
btn.style.paddingBottom = 0;
btn.textContent = text;
btn.addEventListener('click', function() {
insertText(before, after);
});
parent.append(btn);
}
const elt = document.createElement('div');
elt.id = 'helper-buttons';
createButton(elt, 'txt', `<span style="font-family: 'Courier New', monospace;">`, `</span>`);
createButton(elt, 'b', `<b>`, `</b>`);
createButton(elt, 'i', `<i>`, `</i>`);
createButton(elt, 'sm', `<small>`, `</small>`);
document.querySelector('body').addEventListener('click', function(e) {
if (e.target.getAttribute('href').match('reply') !== null) {
if (document.querySelectorAll('.qr-meta').length > 0 && document.querySelectorAll('.qr-meta .block-icon-controls #helper-buttons').length < 1) {
e.preventDefault();
const metaParent = document.querySelector('.qr-meta .block-icon-controls');
metaParent.appendChild(elt);
}
}
});
}
main();
| 1 | // ==UserScript== |
| 2 | // @name DW Helper |
| 3 | // @namespace http://veryroundbird.house/ |
| 4 | // @version 1.1 |
| 5 | // @description adds some html shortcuts for the dreamwidth comment box |
| 6 | // @author Carly Smallbird |
| 7 | // @match https://*.dreamwidth.org/* |
| 8 | // @icon https://www.google.com/s2/favicons?sz=64&domain=dreamwidth.org |
| 9 | // @grant none |
| 10 | // ==/UserScript== |
| 11 | |
| 12 | // the guts of this userscript |
| 13 | function main() { |
| 14 | function insertText(before, after) { |
| 15 | const textarea = document.querySelector('.qr-body textarea'); |
| 16 | const cursorPos = textarea.selectionStart; |
| 17 | const selectionLength = textarea.selectionStart !== textarea.selectionEnd ? textarea.selectionEnd - textarea.selectionStart : 0; |
| 18 | console.log(selectionLength); |
| 19 | const v = document.querySelector('.qr-body textarea').value; |
| 20 | const textBefore = v.substring(0, cursorPos); |
| 21 | const textAfter = v.substring(textarea.selectionEnd, v.length); |
| 22 | const textInner = v.substring(cursorPos, selectionLength); |
| 23 | textarea.value = textBefore + before + textInner + after + textAfter; |
| 24 | } |
| 25 | |
| 26 | function createButton(parent, text, before, after) { |
| 27 | const btn = document.createElement('button'); |
| 28 | btn.type = 'button'; |
| 29 | btn.style.paddingTop = 0; |
| 30 | btn.style.paddingBottom = 0; |
| 31 | btn.textContent = text; |
| 32 | btn.addEventListener('click', function() { |
| 33 | insertText(before, after); |
| 34 | }); |
| 35 | parent.append(btn); |
| 36 | } |
| 37 | |
| 38 | const elt = document.createElement('div'); |
| 39 | elt.id = 'helper-buttons'; |
| 40 | createButton(elt, 'txt', `<span style="font-family: 'Courier New', monospace;">`, `</span>`); |
| 41 | createButton(elt, 'b', `<b>`, `</b>`); |
| 42 | createButton(elt, 'i', `<i>`, `</i>`); |
| 43 | createButton(elt, 'sm', `<small>`, `</small>`); |
| 44 | document.querySelector('body').addEventListener('click', function(e) { |
| 45 | if (e.target.getAttribute('href').match('reply') !== null) { |
| 46 | if (document.querySelectorAll('.qr-meta').length > 0 && document.querySelectorAll('.qr-meta .block-icon-controls #helper-buttons').length < 1) { |
| 47 | e.preventDefault(); |
| 48 | const metaParent = document.querySelector('.qr-meta .block-icon-controls'); |
| 49 | metaParent.appendChild(elt); |
| 50 | } |
| 51 | } |
| 52 | }); |
| 53 | } |
| 54 | |
| 55 | main(); |