Skip to content

Instantly share code, notes, and snippets.

@azu
Created November 23, 2022 05:58
Show Gist options
  • Save azu/180c3ae2ae5a04677b4eb670571f0516 to your computer and use it in GitHub Desktop.
Save azu/180c3ae2ae5a04677b4eb670571f0516 to your computer and use it in GitHub Desktop.
get input text
// ==UserScript==
// @name Visual Textarea
// @namespace textarea
// @include http://*
// @include https://*
// @grant none
// @version 1.0
// @author -
// @description 2022/11/23 14:19:51
// ==/UserScript==
const onInput = (event) => {
const target = event.target;
if(target.contentEditable === "true"){
console.log(target.textContent);
} else {
console.log(target.value);
}
};
const weakSet = new WeakSet();
const onUpdate = (element) => {
if(weakSet.has(element)){
return;
}
element.addEventListener("input", onInput, {
passive: false
});
weakSet.add(element);
};
const onFocusin = (event) => {
const element = event.target;
if(element){
onUpdate(element);
}
};
const onFocusout = (event) => {
const element = event.target;
if(element){
element.removeEventListener("input", onInput);
weakSet.delete(element);
}
};
document.addEventListener("focusin", onFocusin, {
passive: false
});
document.addEventListener("focusout", onFocusout, {
passive: false
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment