// ==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); console.log(textBefore); const textAfter = v.substring(textarea.selectionEnd+1, v.length); console.log(textAfter); const textInner = v.substring(cursorPos+1, selectionLength); console.log(textInner); 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', ``, ``); createButton(elt, 'b', ``, ``); createButton(elt, 'i', ``, ``); createButton(elt, 'sm', ``, ``); 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();