Repository navigation
Expand file tree
/
Copy pathpaste_url_formatter.user.js
More file actions
166 lines (128 loc) · 4.04 KB
/
Copy pathpaste_url_formatter.user.js
File metadata and controls
166 lines (128 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// ==UserScript==
// @name Bitcointalk Paste URL Formatter
// @namespace https://bitcointalk.org/
// @version 1.3
// @description Automatically formats copy/pasted URLs as [url=URL]change_text[/url] BBCode. Press Ctrl+Shift+U to enable or disable.
// @match https://bitcointalk.org/index.php?action=post*
// @grant none
// @author PX-Z
// ==/UserScript==
(function () {
'use strict';
function addStyles() {
const style = document.createElement('style');
style.textContent = `
#url_formatter_note_text {
padding: 3px 15px;
border: 1px solid #e2cf6b;
background: #fffbe4;
}
#url_formatter_toggle {
margin-left: 8px;
}
`;
document.head.appendChild(style);
}
let enabled = true;
// Update the note text
function updateNote() {
const noteText = document.querySelector('#url_formatter_note_text');
if (!noteText) {
return;
}
noteText.innerHTML = enabled
? 'Note: URL Formatter is ON. Press Ctrl+Shift+U to disable. Or click<a href="#" id="url_formatter_toggle">Disable</a>'
: 'Note: URL Formatter is OFF. Press Ctrl+Shift+U to enable. Or click<a href="#" id="url_formatter_toggle">Enable</a>';
const toggle = document.querySelector('#url_formatter_toggle');
if (toggle) {
toggle.addEventListener('click', function (e) {
e.preventDefault();
enabled = !enabled;
updateNote();
});
}
}
// Add note after #lock_warning
function addNote() {
const lockWarning = document.querySelector('#lock_warning');
if (!lockWarning) {
return;
}
// Prevent duplicate note
if (document.querySelector('#url_formatter_note')) {
return;
}
const noteRow = document.createElement('tr');
noteRow.id = 'url_formatter_note';
noteRow.innerHTML = `
<td></td>
<td align="left">
<span id="url_formatter_note_text"></span>
</td>
`;
lockWarning.insertAdjacentElement('afterend', noteRow);
updateNote();
}
// Keyboard shortcut: Ctrl + Shift + U
document.addEventListener('keydown', function (e) {
if (
e.ctrlKey &&
e.shiftKey &&
e.key.toLowerCase() === 'u'
) {
e.preventDefault();
enabled = !enabled;
updateNote();
}
});
// Handle paste
document.addEventListener('paste', function (e) {
// Do nothing when disabled
if (!enabled) {
return;
}
const target = e.target;
// Only handle textareas
if (!(target instanceof HTMLTextAreaElement)) {
return;
}
const text = e.clipboardData.getData('text/plain').trim();
// Only process a single HTTP/HTTPS URL
if (!/^https?:\/\/\S+$/i.test(text)) {
return;
}
// Get selected text
const start = target.selectionStart;
const end = target.selectionEnd;
const selectedText = target.value.substring(start, end);
e.preventDefault();
let bbcode;
if (start !== end) {
// Use selected text as anchor text
bbcode = `[url=${text}]${selectedText}[/url]`;
} else {
// No selected text
bbcode = `[url=${text}]change_text[/url]`;
}
target.setRangeText(
bbcode,
start,
end,
'end'
);
// Notify site's JavaScript that the textarea changed
target.dispatchEvent(new Event('input', {
bubbles: true
}));
});
const observer = new MutationObserver(function () {
addNote();
});
observer.observe(document.body, {
childList: true,
subtree: true
});
// Initial check
addNote();
addStyles();
})();