Last active 1 month ago

Revision e024c11c5e275a5f1d84e975875c7db532b86705

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