画面外のテキストを編集する
code:test.ts
import { takeStores, insertText } from "../scrapbox-userscript-std/dom.ts";
const { cursor, selection } = takeStores();
globalThis.insertText = insertText;
globalThis.cursor = cursor;
globalThis.selection = selection;
code:js
var u=(e,t)=>{if(!(e instanceof HTMLTextAreaElement))throw new TypeError("${t}" must be HTMLTextAreaElement but actual is "${e}")};var n=()=>{let e=document.getElementById("text-input");if(!!e)return u(e,"textarea#text-input"),e};var i=e=>new Promise(t=>setTimeout(()=>t(),e));var l=async e=>{let t=n();if(!t)throw Error("#text-input is not ditected.");t.focus(),t.value=e;let o=new InputEvent("input",{bubbles:!0});t.dispatchEvent(o),await i(1)};var s=()=>{let e=n();if(!e)throw Error("#text-input is not found.");let t=Object.keys(e).find(r=>r.startsWith("__reactFiber"));if(!t)throw Error('#text-input must has the property whose name starts with "__reactFiber"');let o=et.return.return.stateNode._stores,c=o.find(r=>r.constructor.name==="Cursor");if(!c)throw Error('#text-input must has a "Cursor" store.');let a=o.find(r=>r.constructor.name==="Selection");if(!a)throw Error('#text-input must has a "Selection" store.');return{cursor:c,selection:a}};var{cursor:x,selection:h}=s();globalThis.insertText=l;globalThis.cursor=x;globalThis.selection=h; 選択範囲上書きテスト