Last active 1 month ago

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