dwhelper.user.js
· 1.9 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(text) {
var cursorPos = document.querySelector('.qr-body textarea').selectionStart;
var v = document.querySelector('.qr-body textarea').value;
var textBefore = v.substring(0, cursorPos);
var textAfter = v.substring(cursorPos, v.length);
document.querySelector('.qr-body textarea').value = textBefore + text + textAfter;
}
function createButton(parent, text, snippet) {
let btn = document.createElement('button');
btn.type = 'button';
btn.style.paddingTop = 0;
btn.style.paddingBottom = 0;
btn.textContent = text;
btn.addEventListener('click', function() {
insertText(snippet);
});
parent.append(btn);
}
let 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>`);
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) {
let 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(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 | |
| 50 | main(); |