Last active 1 month ago

Revision b69a763acd43c8d600553f33ed06d1a808f0d751

dwhelper.user.js Raw
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
13function 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 const v = document.querySelector('.qr-body textarea').value;
19 const textBefore = v.substring(0, cursorPos);
20 if (selectionLength > 0) {
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 } else {
25 const textAfter = v.substring(cursorPos, v.length);
26 textarea.value = textBefore + text + textAfter;
27 }
28 }
29
30 function createButton(parent, text, before, after) {
31 const btn = document.createElement('button');
32 btn.type = 'button';
33 btn.style.paddingTop = 0;
34 btn.style.paddingBottom = 0;
35 btn.textContent = text;
36 btn.addEventListener('click', function() {
37 insertText(before, after);
38 });
39 parent.append(btn);
40 }
41
42 const elt = document.createElement('div');
43 elt.id = 'helper-buttons';
44 createButton(elt, 'txt', `<span style="font-family: 'Courier New', monospace;">`, `</span>`);
45 createButton(elt, 'b', `<b>`, `</b>`);
46 createButton(elt, 'i', `<i>`, `</i>`);
47 createButton(elt, 'sm', `<small>`, `</small>`);
48 document.querySelector('body').addEventListener('click', function(e) {
49 if (e.target.getAttribute('href').match('reply') !== null) {
50 if (document.querySelectorAll('.qr-meta').length > 0 && document.querySelectorAll('.qr-meta .block-icon-controls #helper-buttons').length < 1) {
51 e.preventDefault();
52 const metaParent = document.querySelector('.qr-meta .block-icon-controls');
53 metaParent.appendChild(elt);
54 }
55 }
56 });
57}
58
59main();