|
// ==UserScript== |
|
// @name zwsp_emoji_joiner |
|
// @namespace https://sftblw.moe/don.naru.cafe |
|
// @version 0.1 |
|
// @description joins selected custom emoji with zwsp. only works inside of textarea and input |
|
// @author sftblw & stackoverflow |
|
// @match https://*/* |
|
// @exclude file://* |
|
// @grant GM_registerMenuCommand |
|
// ==/UserScript== |
|
|
|
|
|
(function() { |
|
'use strict'; |
|
|
|
// only works on textbox & textarea |
|
// https://stackoverflow.com/q/42800590/4394750 |
|
// https://stackoverflow.com/a/3997896/4394750 |
|
|
|
// for react |
|
// https://stackoverflow.com/a/61110332/4394750 |
|
|
|
function replaceSelected(replaceFn) { |
|
let curElem = document.activeElement; |
|
if (curElem instanceof HTMLTextAreaElement || curElem instanceof HTMLInputElement) { |
|
let text_all = curElem.value; |
|
|
|
let sel_start = curElem.selectionStart; |
|
let sel_end = curElem.selectionEnd; |
|
|
|
let text_head = text_all.substring(0, sel_start); |
|
let text_body = text_all.substring(sel_start, sel_end); |
|
let text_tail = text_all.substring(sel_end, text_all.length); |
|
|
|
let text_body_replaced = replaceFn(text_body); |
|
|
|
let text_full_replaced = text_head + text_body_replaced + text_tail; |
|
|
|
let proto = (curElem instanceof HTMLTextAreaElement |
|
? window.HTMLTextAreaElement.prototype |
|
: window.HTMLInputElement.prototype |
|
); |
|
|
|
let nativeSetter = Object.getOwnPropertyDescriptor( |
|
proto, |
|
"value" |
|
).set; |
|
|
|
nativeSetter.call(curElem, text_full_replaced); |
|
|
|
const event = new Event('input', { bubbles: true }); |
|
curElem.dispatchEvent(event); |
|
|
|
curElem.setSelectionRange(sel_start, sel_start + text_body_replaced.length); |
|
} |
|
} |
|
|
|
function replaceFn(inStr) { |
|
return ( |
|
inStr |
|
.split('\n') |
|
.map( |
|
line => line.split(' ').map(it => it.trim()).filter(it => it !='').join('\u200b') |
|
).join('\n') |
|
); |
|
} |
|
|
|
GM_registerMenuCommand("emoji join with zwsp", () => replaceSelected(replaceFn), "rr"); |
|
})(); |