Last active 1 month ago

veryroundbird's Avatar veryroundbird revised this gist 1 month ago. Go to revision

1 file changed, 1 insertion, 5 deletions

dwhelper.user.js

@@ -15,14 +15,10 @@ function main() {
15 15 const textarea = document.querySelector('.qr-body textarea');
16 16 const cursorPos = textarea.selectionStart;
17 17 const selectionLength = textarea.selectionStart !== textarea.selectionEnd ? textarea.selectionEnd - textarea.selectionStart : 0;
18 - console.log(selectionLength);
19 18 const v = document.querySelector('.qr-body textarea').value;
20 19 const textBefore = v.substring(0, cursorPos);
21 - console.log(textBefore);
22 - const textAfter = v.substring(textarea.selectionEnd+1, v.length);
23 - console.log(textAfter);
20 + const textAfter = v.substring(textarea.selectionEnd, v.length);
24 21 const textInner = v.substring(cursorPos, selectionLength+cursorPos);
25 - console.log(textInner);
26 22 textarea.value = textBefore + before + textInner + after + textAfter;
27 23 }
28 24

veryroundbird's Avatar veryroundbird revised this gist 1 month ago. Go to revision

1 file changed, 1 insertion, 1 deletion

dwhelper.user.js

@@ -21,7 +21,7 @@ function main() {
21 21 console.log(textBefore);
22 22 const textAfter = v.substring(textarea.selectionEnd+1, v.length);
23 23 console.log(textAfter);
24 - const textInner = v.substring(cursorPos+1, selectionLength+cursorPos+1);
24 + const textInner = v.substring(cursorPos, selectionLength+cursorPos);
25 25 console.log(textInner);
26 26 textarea.value = textBefore + before + textInner + after + textAfter;
27 27 }

veryroundbird's Avatar veryroundbird revised this gist 1 month ago. Go to revision

1 file changed, 1 insertion, 1 deletion

dwhelper.user.js

@@ -21,7 +21,7 @@ function main() {
21 21 console.log(textBefore);
22 22 const textAfter = v.substring(textarea.selectionEnd+1, v.length);
23 23 console.log(textAfter);
24 - const textInner = v.substring(cursorPos+1, selectionLength);
24 + const textInner = v.substring(cursorPos+1, selectionLength+cursorPos+1);
25 25 console.log(textInner);
26 26 textarea.value = textBefore + before + textInner + after + textAfter;
27 27 }

veryroundbird's Avatar veryroundbird revised this gist 1 month ago. Go to revision

1 file changed, 5 insertions, 2 deletions

dwhelper.user.js

@@ -18,8 +18,11 @@ function main() {
18 18 console.log(selectionLength);
19 19 const v = document.querySelector('.qr-body textarea').value;
20 20 const textBefore = v.substring(0, cursorPos);
21 - const textAfter = v.substring(textarea.selectionEnd, v.length);
22 - const textInner = v.substring(cursorPos, selectionLength);
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);
25 + console.log(textInner);
23 26 textarea.value = textBefore + before + textInner + after + textAfter;
24 27 }
25 28

veryroundbird's Avatar veryroundbird revised this gist 1 month ago. Go to revision

1 file changed, 4 insertions, 8 deletions

dwhelper.user.js

@@ -15,16 +15,12 @@ function main() {
15 15 const textarea = document.querySelector('.qr-body textarea');
16 16 const cursorPos = textarea.selectionStart;
17 17 const selectionLength = textarea.selectionStart !== textarea.selectionEnd ? textarea.selectionEnd - textarea.selectionStart : 0;
18 + console.log(selectionLength);
18 19 const v = document.querySelector('.qr-body textarea').value;
19 20 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 - }
21 + const textAfter = v.substring(textarea.selectionEnd, v.length);
22 + const textInner = v.substring(cursorPos, selectionLength);
23 + textarea.value = textBefore + before + textInner + after + textAfter;
28 24 }
29 25
30 26 function createButton(parent, text, before, after) {

veryroundbird's Avatar veryroundbird revised this gist 1 month ago. Go to revision

1 file changed, 40 insertions, 31 deletions

dwhelper.user.js

@@ -11,40 +11,49 @@
11 11
12 12 // the guts of this userscript
13 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);
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;
33 27 }
28 + }
34 29
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 - }
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);
47 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 + });
48 57 }
49 58
50 59 main();

veryroundbird's Avatar veryroundbird revised this gist 5 months ago. Go to revision

No changes

veryroundbird revised this gist 5 months ago. Go to revision

1 file changed, 50 insertions

dwhelper.user.js(file created)

@@ -0,0 +1,50 @@
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();
Newer Older