選択範囲に似ているリンクを入力補完するUserScript
code:script.js
var J,g,qe,dr,q,Ve,Xe,mr,Q={},Ge=[],fr=/acit|ex(?:s|g|n|p|$)|rph|grid|ows|mnc|ntw|inech|zoo|^ord|itera/i;function F(e,t){for(var r in t)er=tr;return e}function Ye(e){var t=e.parentNode;t&&t.removeChild(e)}function b(e,t,r){var n,s,o,c={};for(o in t)o=="key"?n=to:o=="ref"?s=to:co=to;if(arguments.length>2&&(c.children=arguments.length>3?J.call(arguments,2):r),typeof e=="function"&&e.defaultProps!=null)for(o in e.defaultProps)co===void 0&&(co=e.defaultPropso);return Z(e,c,n,s,null)}function Z(e,t,r,n,s){var o={type:e,props:t,key:r,ref:n,__k:null,__:null,__b:0,__e:null,__d:void 0,__c:null,__h:null,constructor:void 0,__v:s??++qe};return s==null&&g.vnode!=null&&g.vnode(o),o}function O(e){return e.children}function ee(e,t){this.props=e,this.context=t}function A(e,t){if(t==null)return e.__?A(e.__,e.__.__k.indexOf(e)+1):null;for(var r;t<e.__k.length;t++)if((r=e.__kt)!=null&&r.__e!=null)return r.__e;return typeof e.type=="function"?A(e):null}function ze(e){var t,r;if((e=e.__)!=null&&e.__c!=null){for(e.__e=e.__c.base=null,t=0;t<e.__k.length;t++)if((r=e.__kt)!=null&&r.__e!=null){e.__e=e.__c.base=r.__e;break}return ze(e)}}function Je(e){(!e.__d&&(e.__d=!0)&&q.push(e)&&!te.__r++||Xe!==g.debounceRendering)&&((Xe=g.debounceRendering)||Ve)(te)}function te(){for(var e;te.__r=q.length;)e=q.sort(function(t,r){return t.__v.__b-r.__v.__b}),q=[],e.some(function(t){var r,n,s,o,c,l;t.__d&&(c=(o=(r=t).__v).__e,(l=r.__P)&&(n=[],(s=F({},o)).__v=o.__v+1,be(l,o,s,r.__n,l.ownerSVGElement!==void 0,o.__h!=null?c:null,n,c??A(o),o.__h),ot(n,o),o.__e!=c&&ze(o)))})}function Qe(e,t,r,n,s,o,c,l,a,u){var i,m,d,p,f,y,x,h=n&&n.__k||Ge,_=h.length;for(r.__k=[],i=0;i<t.length;i++)if((p=r.__ki=(p=ti)==null||typeof p=="boolean"?null:typeof p=="string"||typeof p=="number"||typeof p=="bigint"?Z(null,p,null,null,p):Array.isArray(p)?Z(O,{children:p},null,null,null):p.__b>0?Z(p.type,p.props,p.key,null,p.__v):p)!=null){if(p.__=r,p.__b=r.__b+1,(d=hi)===null||d&&p.key==d.key&&p.type===d.type)hi=void 0;else for(m=0;m<_;m++){if((d=hm)&&p.key==d.key&&p.type===d.type){hm=void 0;break}d=null}be(e,p,d=d||Q,s,o,c,l,a,u),f=p.__e,(m=p.ref)&&d.ref!=m&&(x||(x=[]),d.ref&&x.push(d.ref,null,p),x.push(m,p.__c||f,p)),f!=null?(y==null&&(y=f),typeof p.type=="function"&&p.__k===d.__k?p.__d=a=Ze(p,a,e):a=et(e,p,d,h,f,a),typeof r.type=="function"&&(r.__d=a)):a&&d.__e==a&&a.parentNode!=e&&(a=A(d))}for(r.__e=y,i=_;i--;)hi!=null&&(typeof r.type=="function"&&hi.__e!=null&&hi.__e==r.__d&&(r.__d=A(n,i+1)),it(hi,hi));if(x)for(i=0;i<x.length;i++)st(xi,x++i,x++i)}function Ze(e,t,r){for(var n,s=e.__k,o=0;s&&o<s.length;o++)(n=so)&&(n.__=e,t=typeof n.type=="function"?Ze(n,t,r):et(r,n,n,s,n.__e,t));return t}function et(e,t,r,n,s,o){var c,l,a;if(t.__d!==void 0)c=t.__d,t.__d=void 0;else if(r==null||s!=o||s.parentNode==null)e:if(o==null||o.parentNode!==e)e.appendChild(s),c=null;else{for(l=o,a=0;(l=l.nextSibling)&&a<n.length;a+=2)if(l==s)break e;e.insertBefore(s,o),c=o}return c!==void 0?c:s.nextSibling}function gr(e,t,r,n,s){var o;for(o in r)o==="children"||o==="key"||o in t||re(e,o,null,ro,n);for(o in t)s&&typeof to!="function"||o==="children"||o==="key"||o==="value"||o==="checked"||ro===to||re(e,o,to,ro,n)}function tt(e,t,r){t0==="-"?e.setProperty(t,r):et=r==null?"":typeof r!="number"||fr.test(t)?r:r+"px"}function re(e,t,r,n,s){var o;e:if(t==="style")if(typeof r=="string")e.style.cssText=r;else{if(typeof n=="string"&&(e.style.cssText=n=""),n)for(t in n)r&&t in r||tt(e.style,t,"");if(r)for(t in r)n&&rt===nt||tt(e.style,t,rt)}else if(t0==="o"&&t1==="n")o=t!==(t=t.replace(/Capture$/,"")),t=t.toLowerCase()in e?t.toLowerCase().slice(2):t.slice(2),e.l||(e.l={}),e.lt+o=r,r?n||e.addEventListener(t,o?nt:rt,o):e.removeEventListener(t,o?nt:rt,o);else if(t!=="dangerouslySetInnerHTML"){if(s)t=t.replace(/xlinkH:h/,"h").replace(/sName$/,"s");else if(t!=="href"&&t!=="list"&&t!=="form"&&t!=="tabIndex"&&t!=="download"&&t in e)try{et=r??"";break e}catch{}typeof r=="function"||(r!=null&&(r!==!1||t0==="a"&&t1==="r")?e.setAttribute(t,r):e.removeAttribute(t))}}function rt(e){this.le.type+!1(g.event?g.event(e):e)}function nt(e){this.le.type+!0(g.event?g.event(e):e)}function be(e,t,r,n,s,o,c,l,a){var u,i,m,d,p,f,y,x,h,_,L,S=t.type;if(t.constructor!==void 0)return null;r.__h!=null&&(a=r.__h,l=t.__e=r.__e,t.__h=null,o=l),(u=g.__b)&&u(t);try{e:if(typeof S=="function"){if(x=t.props,h=(u=S.contextType)&&nu.__c,_=u?h?h.props.value:u.__:n,r.__c?y=(i=t.__c=r.__c).__=i.__E:("prototype"in S&&S.prototype.render?t.__c=i=new S(x,_):(t.__c=i=new ee(x,_),i.constructor=S,i.render=xr),h&&h.sub(i),i.props=x,i.state||(i.state={}),i.context=_,i.__n=n,m=i.__d=!0,i.__h=[]),i.__s==null&&(i.__s=i.state),S.getDerivedStateFromProps!=null&&(i.__s==i.state&&(i.__s=F({},i.__s)),F(i.__s,S.getDerivedStateFromProps(x,i.__s))),d=i.props,p=i.state,m)S.getDerivedStateFromProps==null&&i.componentWillMount!=null&&i.componentWillMount(),i.componentDidMount!=null&&i.__h.push(i.componentDidMount);else{if(S.getDerivedStateFromProps==null&&x!==d&&i.componentWillReceiveProps!=null&&i.componentWillReceiveProps(x,_),!i.__e&&i.shouldComponentUpdate!=null&&i.shouldComponentUpdate(x,i.__s,_)===!1||t.__v===r.__v){i.props=x,i.state=i.__s,t.__v!==r.__v&&(i.__d=!1),i.__v=t,t.__e=r.__e,t.__k=r.__k,t.__k.forEach(function(C){C&&(C.__=t)}),i.__h.length&&c.push(i);break e}i.componentWillUpdate!=null&&i.componentWillUpdate(x,i.__s,_),i.componentDidUpdate!=null&&i.__h.push(function(){i.componentDidUpdate(d,p,f)})}i.context=_,i.props=x,i.state=i.__s,(u=g.__r)&&u(t),i.__d=!1,i.__v=t,i.__P=e,u=i.render(i.props,i.state,i.context),i.state=i.__s,i.getChildContext!=null&&(n=F(F({},n),i.getChildContext())),m||i.getSnapshotBeforeUpdate==null||(f=i.getSnapshotBeforeUpdate(d,p)),L=u!=null&&u.type===O&&u.key==null?u.props.children:u,Qe(e,Array.isArray(L)?L:L,t,r,n,s,o,c,l,a),i.base=t.__e,t.__h=null,i.__h.length&&c.push(i),y&&(i.__E=i.__=null),i.__e=!1}else o==null&&t.__v===r.__v?(t.__k=r.__k,t.__e=r.__e):t.__e=hr(r.__e,t,r,n,s,o,c,a);(u=g.diffed)&&u(t)}catch(C){t.__v=null,(a||o!=null)&&(t.__e=l,t.__h=!!a,oo.indexOf(l)=null),g.__e(C,t,r)}}function ot(e,t){g.__c&&g.__c(t,e),e.some(function(r){try{e=r.__h,r.__h=[],e.some(function(n){n.call(r)})}catch(n){g.__e(n,r.__v)}})}function hr(e,t,r,n,s,o,c,l){var a,u,i,m=r.props,d=t.props,p=t.type,f=0;if(p==="svg"&&(s=!0),o!=null){for(;f<o.length;f++)if((a=of)&&"setAttribute"in a==!!p&&(p?a.localName===p:a.nodeType===3)){e=a,of=null;break}}if(e==null){if(p===null)return document.createTextNode(d);e=s?document.createElementNS("http://www.w3.org/2000/svg",p):document.createElement(p,d.is&&d),o=null,l=!1}if(p===null)m===d||l&&e.data===d||(e.data=d);else{if(o=o&&J.call(e.childNodes),u=(m=r.props||Q).dangerouslySetInnerHTML,i=d.dangerouslySetInnerHTML,!l){if(o!=null)for(m={},f=0;f<e.attributes.length;f++)m[e.attributes[f].name]=e.attributes[f].value;(i||u)&&(i&&(u&&i.__html==u.__html||i.__html===e.innerHTML)||(e.innerHTML=i&&i.__html||""))}if(gr(e,d,m,s,l),i)t.__k=[];else if(f=t.props.children,Qe(e,Array.isArray(f)?f:f,t,r,n,s&&p!=="foreignObject",o,c,o?o0:r.__k&&A(r,0),l),o!=null)for(f=o.length;f--;)of!=null&&Ye(of);l||("value"in d&&(f=d.value)!==void 0&&(f!==m.value||f!==e.value||p==="progress"&&!f)&&re(e,"value",f,m.value,!1),"checked"in d&&(f=d.checked)!==void 0&&f!==e.checked&&re(e,"checked",f,m.checked,!1))}return e}function st(e,t,r){try{typeof e=="function"?e(t):e.current=t}catch(n){g.__e(n,r)}}function it(e,t,r){var n,s;if(g.unmount&&g.unmount(e),(n=e.ref)&&(n.current&&n.current!==e.__e||st(n,null,t)),(n=e.__c)!=null){if(n.componentWillUnmount)try{n.componentWillUnmount()}catch(o){g.__e(o,t)}n.base=n.__P=null}if(n=e.__k)for(s=0;s<n.length;s++)ns&&it(ns,t,typeof e.type!="function");r||e.__e==null||Ye(e.__e),e.__e=e.__d=void 0}function xr(e,t,r){return this.constructor(e,r)}function ye(e,t,r){var n,s,o;g.__&&g.__(e,t),s=(n=typeof r=="function")?null:r&&r.__k||t.__k,o=[],be(t,e=(!n&&r||t).__k=b(O,null,e),s||Q,Q,t.ownerSVGElement!==void 0,!n&&r?r:s?null:t.firstChild?J.call(t.childNodes):null,o,!n&&r?r:s?s.__e:t.firstChild,n),ot(o,e)}J=Ge.slice,g={__e:function(e,t){for(var r,n,s;t=t.__;)if((r=t.__c)&&!r.__)try{if((n=r.constructor)&&n.getDerivedStateFromError!=null&&(r.setState(n.getDerivedStateFromError(e)),s=r.__d),r.componentDidCatch!=null&&(r.componentDidCatch(e),s=r.__d),s)return r.__E=r}catch(o){e=o}throw e}},qe=0,dr=function(e){return e!=null&&e.constructor===void 0},ee.prototype.setState=function(e,t){var r;r=this.__s!=null&&this.__s!==this.state?this.__s:this.__s=F({},this.state),typeof e=="function"&&(e=e(F({},r),this.props)),e&&F(r,e),e!=null&&this.__v&&(t&&this.__h.push(t),Je(this))},ee.prototype.forceUpdate=function(e){this.__v&&(this.__e=!0,e&&this.__h.push(e),Je(this))},ee.prototype.render=O,q=[],Ve=typeof Promise=="function"?Promise.prototype.then.bind(Promise.resolve()):setTimeout,te.__r=0,mr=0;var ne,j,at,V=0,we=[],ct=g.__b,lt=g.__r,ut=g.diffed,pt=g.__c,dt=g.unmount;function Ee(e,t){g.__h&&g.__h(j,e,V||t),V=0;var r=j.__H||(j.__H={__:[],__h:[]});return e>=r.__.length&&r.__.push({}),r.__e}function M(e){return V=1,ve(gt,e)}function ve(e,t,r){var n=Ee(ne++,2);return n.t=e,n.__c||(n.__=[r?r(t):gt(void 0,t),function(s){var o=n.t(n.__0,s);n.__0!==o&&(n.__=[o,n.__1],n.__c.setState({}))}],n.__c=j),n.__}function v(e,t){var r=Ee(ne++,3);!g.__s&&ft(r.__H,t)&&(r.__=e,r.__H=t,j.__H.__h.push(r))}function oe(e){return V=5,B(function(){return{current:e}},[])}function B(e,t){var r=Ee(ne++,7);return ft(r.__H,t)&&(r.__=e(),r.__H=t,r.__h=e),r.__}function T(e,t){return V=8,B(function(){return e},t)}function _r(){var e;for(we.sort(function(t,r){return t.__v.__b-r.__v.__b});e=we.pop();)if(e.__P)try{e.__H.__h.forEach(se),e.__H.__h.forEach(ke),e.__H.__h=[]}catch(t){e.__H.__h=[],g.__e(t,e.__v)}}g.__b=function(e){j=null,ct&&ct(e)},g.__r=function(e){lt&<(e),ne=0;var t=(j=e.__c).__H;t&&(t.__h.forEach(se),t.__h.forEach(ke),t.__h=[])},g.diffed=function(e){ut&&ut(e);var t=e.__c;t&&t.__H&&t.__H.__h.length&&(we.push(t)!==1&&at===g.requestAnimationFrame||((at=g.requestAnimationFrame)||function(r){var n,s=function(){clearTimeout(o),mt&&cancelAnimationFrame(n),setTimeout(r)},o=setTimeout(s,100);mt&&(n=requestAnimationFrame(s))})(_r)),j=null},g.__c=function(e,t){t.some(function(r){try{r.__h.forEach(se),r.__h=r.__h.filter(function(n){return!n.__||ke(n)})}catch(n){t.some(function(s){s.__h&&(s.__h=[])}),t=[],g.__e(n,r.__v)}}),pt&&pt(e,t)},g.unmount=function(e){dt&&dt(e);var t,r=e.__c;r&&r.__H&&(r.__H.__.forEach(function(n){try{se(n)}catch(s){t=s}}),t&&g.__e(t,r.__v))};var mt=typeof requestAnimationFrame=="function";function se(e){var t=j,r=e.__c;typeof r=="function"&&(e.__c=void 0,r()),j=t}function ke(e){var t=j;e.__c=e.__(),j=t}function ft(e,t){return!e||e.length!==t.length||t.some(function(r,n){return r!==en})}function gt(e,t){return typeof t=="function"?t(e):t}var X=e=>e==null,ie=e=>typeof e=="string",ae=e=>typeof e=="number";var br=e=>typeof e=="object"&&e!==null,yr=e=>br(e)?(e.name===void 0||typeof e.name=="string")&&typeof e.message=="string":!1,D=e=>{try{let t=typeof e=="string"?JSON.parse(e):e;return yr(t)?t:!1}catch(t){if(t instanceof SyntaxError)return!1;throw t}};var ht=(e,t)=>{if(!Array.isArray(e))throw new TypeError("${t}" must be an array but actual is "${e}")};var xt=(e,t)=>{if(!(e instanceof HTMLTextAreaElement))throw new TypeError("${t}" must be HTMLTextAreaElement but actual is "${e}")};var H=()=>{let e=document.getElementById("text-input");if(!!e)return xt(e,"textarea#text-input"),e};var _t=e=>{if(X(e))return;if(ae(e))return wr(e)?.id;if(ie(e))return e.startsWith("L")?e.slice(1):e;if(e.classList.contains("line"))return e.id.slice(1);let t=e.closest(".line");if(t)return t.id.slice(1)};var wr=e=>{if(X(e))return;if(ae(e))return bt()e;let t=_t(e);return t?bt().find(r=>r.id===t):void 0},Le=e=>{if(Er(e))return e;let t=_t(e);if(X(t))return t;let r=document.getElementById(L${t});if(!X(r))return r},Er=e=>e instanceof HTMLDivElement&&e.classList.contains("line");var bt=()=>(ht(scrapbox.Page.lines,"scrapbox.Page.lines"),scrapbox.Page.lines);var vr=e=>e instanceof HTMLSpanElement&&e.classList.contains("char-index");var G=(e,t)=>{let r=Le(e)?.getElementsByClassName?.(c-${t})?.0;return vr(r)?r:void 0};var K=(e,t)=>{let{noModifiedKeys:r=!1,...n}=t??{},s={bubbles:!0,cancelable:!0,keyCode:kre,...r?{}:{...n}},o=H();if(!o)throw Error("#text-input must exist.");o.dispatchEvent(new KeyboardEvent("keydown",s)),o.dispatchEvent(new KeyboardEvent("keyup",s))},kr={Backspace:8,Tab:9,Enter:13,Delete:46,Escape:27," ":32,PageUp:33,PageDown:34,End:35,Home:36,ArrowLeft:37,ArrowUp:38,ArrowRight:39,ArrowDown:40,a:65,A:65,b:66,B:66,c:67,C:67,d:68,D:68,e:69,E:69,f:70,F:70,g:71,G:71,h:72,H:72,i:73,I:73,j:74,J:74,k:75,K:75,l:76,L:76,m:77,M:77,n:78,N:78,o:79,O:79,p:80,P:80,q:81,Q:81,r:82,R:82,s:83,S:83,t:84,T:84,u:85,U:85,v:86,V:86,w:87,W:87,x:88,X:88,y:89,Y:89,z:90,Z:90,0:48,1:49,2:50,3:51,4:52,5:53,6:54,7:55,8:56,9:57,F1:113,F2:114,F3:115,F4:116,F5:117,F6:118,F7:119,F8:120,F9:121,F10:122,F11:123,F12:124,":":186,"*":186,";":187,"+":187,"-":189,"=":189,".":190,">":190,"/":191,"?":191,"@":192,"":192,"[":219,"{":219,"\\":220,"|":220,"]":221,"}":221,"^":222,"~":222,_:226};var ce=e=>new Promise(t=>setTimeout(()=>t(),e));var yt=async(e,t)=>{let r={button:t.button??0,clientX:t.X,clientY:t.Y,bubbles:!0,cancelable:!0,shiftKey:t.shiftKey,ctrlKey:t.ctrlKey,altKey:t.altKey,view:window};e.dispatchEvent(new MouseEvent("mousedown",r)),e.dispatchEvent(new MouseEvent("mouseup",r)),e.dispatchEvent(new MouseEvent("click",r)),await ce(10)};var wt=e=>{let{top:t,bottom:r}=e.getBoundingClientRect();return t>=0&&r<=window.innerHeight};function*Se(e,t){for(let r=e;r<t;r++)yield r}var Et=()=>{K("Home"),K("Home")};var vt=async e=>{await Sr(Le(e))},Sr=async e=>{if(!e)throw Error("The target line DOM is failed to find.");wt(e)||e.scrollIntoView({block:"center"});let{right:t,top:r,height:n}=e.getElementsByClassName("text")[0].getBoundingClientRect();await yt(e,{X:t+1,Y:r+n/2})};var kt=async(e,t,r)=>{await vt(e),Et();for(let n of Se(e,t))K("ArrowDown",{shiftKey:!0});K("End",{shiftKey:!0}),await Te(r)};var Te=async e=>{let t=H();if(!t)throw Error("#text-input is not ditected.");t.focus(),t.value=e;let r=new InputEvent("input",{bubbles:!0});t.dispatchEvent(r),await ce(1)};var le=e=>e.replaceAll(" ","_").toLowerCase(),Lt=e=>e.replaceAll("_"," "),W=e=>[...e].map((t,r)=>t===" "?"_":!Tr.includes(t)||r===e.length-1&&Pr.includes(t)?encodeURIComponent(t):t).join(""),Tr='@$&+=:;",',Pr=':;",';var ue=()=>{let e=H();if(!e)throw Error("#text-input is not found.");let t=Object.keys(e).find(o=>o.startsWith("__reactFiber"));if(!t)throw Error('#text-input must has the property whose name starts with "__reactFiber"');let r=e[t].return.return.stateNode._stores,n=r.find(o=>o.constructor.name==="Cursor");if(!n)throw Error('#text-input must has a "Cursor" store.');let s=r.find(o=>o.constructor.name==="Selection");if(!s)throw Error('#text-input must has a "Selection" store.');return{cursor:n,selection:s}};var Y=()=>ue().cursor;var St=()=>ue().selection;var Tt=()=>{let[e,t]=M({start:{line:0,char:0},end:{line:0,char:0}}),[r,n]=M("");return v(()=>{let s=St(),o=()=>{t(s.getRange()),n(s.getSelectedText())};return s.addChangeListener(o),()=>s.removeChangeListener(o)},[]),{text:r,range:e}};var Pt=({title:e,projects:t,selected:r,confirm:n})=>b("div",{className:candidate${r?" selected":""}},b(Ir,{title:e,confirm:n}),t.map(s=>b(Mr,{project:s.name,title:e,mark:s.mark,confirm:s.confirm}))),Ir=({title:e,confirm:t})=>b("a",{className:"button",tabIndex:0,role:"menuitem",href:./${W(e)},onClick:It(t)},e),Mr=({project:e,title:t,mark:r,confirm:n})=>r===""?b(O,null):b("a",{className:"mark",tabIndex:0,href:../${e}/${W(t)},onClick:It(n),title:/${e}/${W(t)}},r instanceof URL?b("img",{src:r.href}):${r}),It=e=>T(t=>{t.metaKey||t.ctrlKey||t.altKey||t.shiftKey||(t.preventDefault(),t.stopPropagation(),e())},[e]);var Mt=e=>{let[t,r]=M(-1);v(()=>r(-1),[e]);let n=T(l=>r(a=>l?.cyclic?(a+1)%e:Math.min(a+1,e)),[e]),s=T(l=>r(a=>a<0?e-1:l?.cyclic?(a+e-1)%e:Math.max(a-1,0)),[e]),o=T(()=>r(0),[]),c=T(()=>r(e-1),[e]);return{selectedIndex:t,next:n,prev:s,selectFirst:o,selectLast:c}};var Pe=(e,t)=>t.some(r=>e instanceof r),Ct,Nt;function Cr(){return Ct||(Ct=[IDBDatabase,IDBObjectStore,IDBIndex,IDBCursor,IDBTransaction])}function Nr(){return Nt||(Nt=[IDBCursor.prototype.advance,IDBCursor.prototype.continue,IDBCursor.prototype.continuePrimaryKey])}var Rt=new WeakMap,Ie=new WeakMap,Dt=new WeakMap,Me=new WeakMap,pe=new WeakMap;function Rr(e){let t=new Promise((r,n)=>{let s=()=>{e.removeEventListener("success",o),e.removeEventListener("error",c)},o=()=>{r(U(e.result)),s()},c=()=>{n(e.error),s()};e.addEventListener("success",o),e.addEventListener("error",c)});return t.then(r=>{r instanceof IDBCursor&&Rt.set(r,e)}).catch(()=>{}),pe.set(t,e),t}function Dr(e){if(Ie.has(e))return;let t=new Promise((r,n)=>{let s=()=>{e.removeEventListener("complete",o),e.removeEventListener("error",c),e.removeEventListener("abort",c)},o=()=>{r(),s()},c=()=>{n(e.error||new DOMException("AbortError","AbortError")),s()};e.addEventListener("complete",o),e.addEventListener("error",c),e.addEventListener("abort",c)});Ie.set(e,t)}var Ce={get(e,t,r){if(e instanceof IDBTransaction){if(t==="done")return Ie.get(e);if(t==="objectStoreNames")return e.objectStoreNames||Dt.get(e);if(t==="store")return r.objectStoreNames[1]?void 0:r.objectStore(r.objectStoreNames[0])}return U(e[t])},set(e,t,r){return e[t]=r,!0},has(e,t){return e instanceof IDBTransaction&&(t==="done"||t==="store")?!0:t in e}};function jt(e){Ce=e(Ce)}function jr(e){return e===IDBDatabase.prototype.transaction&&!("objectStoreNames"in IDBTransaction.prototype)?function(t,...r){let n=e.call(de(this),t,...r);return Dt.set(n,t.sort?t.sort():[t]),U(n)}:Nr().includes(e)?function(...t){return e.apply(de(this),t),U(Rt.get(this))}:function(...t){return U(e.apply(de(this),t))}}function Or(e){return typeof e=="function"?jr(e):(e instanceof IDBTransaction&&Dr(e),Pe(e,Cr())?new Proxy(e,Ce):e)}function U(e){if(e instanceof IDBRequest)return Rr(e);if(Me.has(e))return Me.get(e);let t=Or(e);return t!==e&&(Me.set(e,t),pe.set(t,e)),t}var de=e=>pe.get(e);function Ot(e,t,{blocked:r,upgrade:n,blocking:s,terminated:o}={}){let c=indexedDB.open(e,t),l=U(c);return n&&c.addEventListener("upgradeneeded",a=>{n(U(c.result),a.oldVersion,a.newVersion,U(c.transaction),a)}),r&&c.addEventListener("blocked",a=>r(a.oldVersion,a.newVersion,a)),l.then(a=>{o&&a.addEventListener("close",()=>o()),s&&a.addEventListener("versionchange",u=>s(u.oldVersion,u.newVersion,u))}).catch(()=>{}),l}var Br=["get","getKey","getAll","getAllKeys","count"],Hr=["put","add","delete","clear"],Ne=new Map;function Bt(e,t){if(!(e instanceof IDBDatabase&&!(t in e)&&typeof t=="string"))return;if(Ne.get(t))return Ne.get(t);let r=t.replace(/FromIndex$/,""),n=t!==r,s=Hr.includes(r);if(!(r in(n?IDBIndex:IDBObjectStore).prototype)||!(s||Br.includes(r)))return;let o=async function(c,...l){let a=this.transaction(c,s?"readwrite":"readonly"),u=a.store;return n&&(u=u.index(l.shift())),(await Promise.all([u[r](...l),s&&a.done]))[0]};return Ne.set(t,o),o}jt(e=>({...e,get:(t,r,n)=>Bt(t,r)||e.get(t,r,n),has:(t,r)=>!!Bt(t,r)||e.has(t,r)}));var Fr=["continue","continuePrimaryKey","advance"],Ht={},Re=new WeakMap,Ft=new WeakMap,Ur={get(e,t){if(!Fr.includes(t))return e[t];let r=Ht[t];return r||(r=Ht[t]=function(...n){Re.set(this,Ft.get(this)[t](...n))}),r}};async function*$r(...e){let t=this;if(t instanceof IDBCursor||(t=await t.openCursor(...e)),!t)return;t=t;let r=new Proxy(t,Ur);for(Ft.set(r,t),pe.set(r,de(t));t;)yield r,t=await(Re.get(r)||t.continue()),Re.delete(r)}function Ut(e,t){return t===Symbol.asyncIterator&&Pe(e,[IDBIndex,IDBObjectStore,IDBCursor])||t==="iterate"&&Pe(e,[IDBIndex,IDBObjectStore])}jt(e=>({...e,get(t,r,n){return Ut(t,r)?$r:e.get(t,r,n)},has(t,r){return Ut(t,r)||e.has(t,r)}}));var N=e=>{let{fetch:t=globalThis.fetch,hostName:r="scrapbox.io",...n}=e;return{fetch:t,hostName:r,...n}};var P=class extends Error{constructor(t){super(${t.status} ${t.statusText} when fetching ${t.path.toString()});this.name="UnexpectedResponseError";this.status=t.status,this.statusText=t.statusText,this.body=t.body,this.path=t.path,Error.captureStackTrace&&Error.captureStackTrace(this,P)}};var R=e=>connect.sid=${e};var De=async(e,t)=>{let{sid:r,hostName:n,fetch:s}=N(t??{}),o=https://${n}/api/projects/${e},c=await s(o,r?{headers:{Cookie:R(r)}}:void 0);if(!c.ok){let a=await c.text(),u=D(a);if(!u)throw new P({path:new URL(o),...c,body:a});return{ok:!1,value:u}}let l=await c.json();return{ok:!0,value:l}},je=async(e,t)=>{let{sid:r,hostName:n,fetch:s}=N(t??{}),o=new URLSearchParams;for(let u of e)o.append("ids",u);let c=https://${n}/api/projects?${o.toString()},l=await s(c,r?{headers:{Cookie:R(r)}}:void 0);if(!l.ok){let u=await l.text(),i=D(u);if(!i)throw new P({path:new URL(c),...l,body:u});return{ok:!1,value:i}}let a=await l.json();return{ok:!0,value:a}};var $t=async(e,t)=>{let{sid:r,hostName:n,fetch:s,followingId:o}=N(t??{}),c=https://${n}/api/pages/${e}/search/titles${o??followingId=${o}:""},l=await s(c,r?{headers:{Cookie:R(r)}}:void 0);if(!l.ok){if(l.status===422)return{ok:!1,value:{name:"InvalidFollowingIdError",message:await l.text()}};let u=await l.text(),i=D(u);if(!i)throw new P({path:new URL(c),...l,body:u});return{ok:!1,value:i}}let a=await l.json();return{ok:!0,value:{pages:a,followingId:l.headers.get("X-following-id")??""}}},Be=async(e,t)=>{let r=await $t(e,t);return r.ok?async function*(){yield r.value.pages;let n=r.value.followingId;for(;n;){let s=await $t(e,{followingId:n,...t});if(!s.ok)throw new Error("The authorization cannot be unavailable");yield s.value.pages,n=s.value.followingId}}():r.value};var At=!1,Kt=e=>{At=e},Ar="[scrapbox-select-suggestion]",k=Object.fromEntries([...Object.entries(console)].map(([e,t])=>typeof t!="function"?[e,t]:[e,(r,...n)=>{!At||t(typeof r=="string"?${Ar} ${r}:r,...n)}]));var Wt=async(e,t)=>{let r=await He(),n="[scrapbox-select-suggestion]";k.debug("check updates of links...");let s=[],o=[];try{{let a=r.transaction("status","readwrite");await Promise.all(e.map(async u=>{let i=await a.store.get(u);if(i?.isValid===!1)return;let m=i?.checked??0,d=new Date().getTime()/1e3;if(m+t>d||i?.updating&&m+600>d)return;let p={project:u,id:i?.id,isValid:!0,checked:m,updated:i?.updated??0,updating:!0};s.push(p),a.store.put(p)})),await a.done}if(k.debug(checked. ${s.length} projects maybe need upgrade.),s.length===0)return[];let c=new BroadcastChannel(Xt),l=[];for await(let a of Kr(s)){if(!a.ok)switch(o.push({project:a.value.project,isValid:!1}),a.value.name){case"NotFoundError":console.warn(${n} "${a.value.project}" is not found.);continue;case"NotMemberError":console.warn(${n} You are not a member of "${a.value.project}".);continue;case"NotLoggedInError":console.warn(${n} You are not a member of "${a.value.project}" or You are not logged in yet.);continue}if(a.value.updated<a.value.checked)k.debug(no updates in "${a.value.name}");else{let u={project:a.value.name,links:await qr(a.value.name)};l.push(u),k.time(write data of "${a.value.name}"),await Wr(u),c.postMessage({type:"update",project:a.value.name}),k.timeEnd(write data of "${a.value.name}")}o.push({project:a.value.name,isValid:!0,id:a.value.id,checked:new Date().getTime()/1e3,updated:a.value.updated,updating:!1})}return c.close(),l}finally{let c=r.transaction("status","readwrite"),l=c.store;await Promise.all(o.map(a=>l.put(a))),await c.done}},qt=async e=>{let t=[],r=read links of ${e.length} projects;k.time(r);{let n=(await He()).transaction("source","readonly");await Promise.all(e.map(async s=>{let o=await n.store.get(s);o?t.push(o):t.push({project:s,links:[]})})),await n.done}return k.timeEnd(r),t},Vt=(e,t)=>{let r=new BroadcastChannel(Xt),n=s=>{!e.includes(s.data.project)||t(s.data)};return r.addEventListener("message",n),()=>{r.removeEventListener("message",n),r.close()}},me,He=async()=>me||(me=await Ot("userscript-links",5,{upgrade(e){k.time("update DB");for(let t of e.objectStoreNames)e.deleteObjectStore(t);e.createObjectStore("source",{keyPath:"project"}),e.createObjectStore("status",{keyPath:"project"}),k.timeEnd("update DB")}}),me);async function*Kr(e){let t=[],r=[],n=new Map;for(let o of e)o.id?t.push(o.id):r.push(o.project),n.set(o.project,o.checked);let s=await je(t);if(!s.ok)r=e.map(o=>o.project);else for(let o of s.value.projects)!n.has(o.name)||(yield{ok:!0,value:{...o,checked:n.get(o.name)??0}});for(let o of r){let c=await De(o);yield c.ok?{ok:!0,value:{...c.value,checked:n.get(o)??0}}:{ok:!1,value:{...c.value,project:o}}}}var Wr=async e=>(await He()).put("source",e),Xt="userscript-store-notify",qr=async e=>{let t=await Be(e);if("name"in t)throw console.error(t),new Error(${t.name}: ${t.message});let r=download and create Links of "${e}";k.time(r);let n=new Map;for await(let s of t)for(let o of s){let c=le(o.title),l=n.get(c);n.set(c,{title:o.title,hasIcon:o.hasIcon,updated:o.updated,links:o.links.length,linked:l?.linked??0});for(let a of o.links){let u=le(a),i=n.get(u);n.set(u,{title:i?.title??a,hasIcon:i?.hasIcon??!1,updated:i?.updated??0,links:i?.links??0,linked:(i?.linked??0)+1})}}return k.timeEnd(r),[...n.entries()].map(([s,o])=>[o.title,s,o.hasIcon,o.links,o.linked,o.updated])};var Gt=e=>{let[t,r]=M([]),n=T(s=>{let o=new Map;for(let{project:c,links:l}of s)for(let[a,u,i,,,m]of l){let d=o.get(u);o.set(u,{title:d?.title??a,updated:Math.max(d?.updated??0,m),metadata:[...d?.metadata??[],{project:c,hasIcon:i}]})}r([...o.entries()].map(([c,l])=>({titleLc:c,...l})))},[]);return v(()=>{let s=!1,o=async()=>{let i=await qt(e);s||n(i)};o();let c,l=Vt(e,()=>{clearTimeout(c),c=setTimeout(o,1e4)}),a=async()=>{(await Wt(e,600)).length===0||s||o()};a();let u=setInterval(a,600*1e3);return()=>{s=!0,clearTimeout(c),clearInterval(u),l()}},[e]),t};var Yt=e=>{let t=oe(null),r=B(()=>{if(!t.current)return{};let n=t.current.parentNode;if(!(n instanceof ShadowRoot))throw Error('The parent of "div.container" must be ShadowRoot');let s=n.host?.parentElement?.getBoundingClientRect?.(),c=G(e.line,e.char)?.getBoundingClientRect?.();return!c||!s?{}:{top:${c.bottom-s.top}px,left:${c?.left??0-s.left}px,right:${s.right-(c?.left??0)}px}},[e.line,e.char]);return{ref:t,...r}};var zt=e=>{let t=new Map,r=[...e].reverse();{let c=1;for(let l of r){t.set(l,(t.get(l)??0)|c);let a=l.toLowerCase();t.set(a,(t.get(a)??0)|c);let u=l.toUpperCase();t.set(u,(t.get(u)??0)|c),c<<=1}}let n=r.length,s=~(~0<<n),o=1<<n-1;return c=>{let l=0,a=s,u=[...c].reverse(),i=[],m=u.length;i[m]=n;for(let d of u){let p=t.get(d)??0,f=p|l,y=(p&a)+a^a|p,x=l|~(y|a),h=a&y;i[m-1]=i[m]+((x&o)!=0?1:(h&o)!=0?-1:0),a=h<<1|~(f|x<<1),l=x<<1&f,m--}return i}};var Vr=[0,0,0,1,1,2,2,2,2,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6],Jt=e=>{let t=Lt(e.trim()).split(/\s+/).sort((r,n)=>n.length-r.length);if(!(t.length===0||t.every(r=>r==="")))return r=>{let n=[...r],s=Vr[[...t.join("")].length];for(let o of t)n=Xr(o,s,n);return n}},Xr=(e,t,r)=>{let n=[...e].length,s=zt(e);return r.flatMap(({title:o,dist:c,matches:l,...a})=>{l??=[],c??=0;let u=s(o).flatMap((d,p)=>c+d<=t&&l.every(([f,y])=>p+n<=f||y<p)?[[p,d]]:[]);if(u.length===0)return[];let i=u.reduce((d,[p,f])=>(d.dist<=f||(d.dist=f,d.start=p,d.end=p+n-1),d),{dist:n,start:0,end:n-1}),m=i.dist+c;return m>t?[]:(l.push([i.start,i.end]),[{title:o,dist:m,matches:l,...a}])})},Qt=(e,t)=>{let r=Object.fromEntries(t.map((n,s)=>[n,s]));return[...e].sort((n,s)=>{let o=n.dist-s.dist;if(o!==0)return o;let c=n.matches.map(([i])=>i).sort(),l=s.matches.map(([i])=>i).sort();for(let i=0;i<c.length;i++){let m=c[i]-(l[i]??l.length);if(m!==0)return m}let a=n.title.length-s.title.length;if(a!==0)return a;let u=Math.min(...n.metadata.map(i=>r[i.project]??t.length))-Math.min(...s.metadata.map(i=>r[i.project]??t.length));return u!==0?u:s.updated-n.updated})};var Zt=(e,t,r,n)=>{let s=Jt(e);if(!s)return r([]),()=>{};let o=!1,c,l=[],a=()=>{r(l),c=void 0},u=n?.chunk??1e3,i=Math.floor(t.length/u)+1;return(async()=>{for(let m=0;m<i;m++){if(await new Promise(d=>requestAnimationFrame(d)),o)return;k.time(${m}/${i-1} search for "${e}"),l.push(...s(t.slice(m*u,(m+1)*u))),k.timeEnd(${m}/${i-1} search for "${e}"),c===void 0&&(a(),c=setTimeout(a,n?.interval??500))}})(),()=>{o=!0,clearTimeout(c)}};var Fe="enableProjectsOnSuggestion",er=(e,t)=>{let[r,n]=M(fe(e,t)),s=T((o,c)=>{Gr(o,c,e,t),n(fe(e,t))},[e,t.enableSelfProjectOnStart]);return v(()=>{let o=c=>{c.key===Fe&&n(fe(e,t))};return addEventListener("storage",o),()=>removeEventListener("storage",o)},[e,t.enableSelfProjectOnStart]),{projects:r,set:s}},Ue=!0,fe=(e,t)=>{try{let r=localStorage.getItem(Fe);if(r===null)return ge(e),e;let n=JSON.parse(r);return Array.isArray(n)&&n.every(s=>typeof s=="string")?!t.enableSelfProjectOnStart||!e.includes(scrapbox.Project.name)?n:n.includes(scrapbox.Project.name)?Ue?n:n.filter(s=>s!==scrapbox.Project.name):Ue?[...n,scrapbox.Project.name]:n:(ge(e),e)}catch(r){if(!(r instanceof TypeError))throw r;return ge(e),e}},Gr=(e,t,r,n)=>{let s=fe(r,n);n.enableSelfProjectOnStart&&e===scrapbox.Project.name&&(Ue=t),ge(t?[...s,e]:s.filter(o=>o!==e))},ge=e=>localStorage.setItem(Fe,JSON.stringify(e));var $e=(e,t)=>{if(e instanceof URL)return e;try{return new URL(e)}catch(r){if(!(r instanceof TypeError))throw r;if(!t||!/^\.\/|\.\.\/|\//.test(e))return e;try{return new URL(e,t)}catch(n){if(!(n instanceof TypeError))throw n;return e}}};var tr=({query:e,position:t,state:r,limit:n,range:s,context:o,enableSelfProjectOnStart:c,callback:l,projects:a,dispatch:u,mark:i,hideSelfMark:m})=>{let{projects:d,set:p}=er(a,{enableSelfProjectOnStart:c}),f=Gt(a),[y,x]=M([]);v(()=>{if(r!=="completion"){x([]);return}return Zt(e,f,E=>x(Qt(E,a).map(w=>({title:w.title,projects:w.metadata.map(({project:I})=>I)}))),{chunk:5e3})},[r,f,e]);let h=T((E,w)=>{u({type:"cancel"});let I=w?/${w}/${E}:${E};if(o==="selection"){Te(I);return}let _e=Y().getPosition().line;if(scrapbox.Layout!=="page")return;let We=scrapbox.Page.lines[_e].text;kt(_e,_e,${We.slice(0,s?.start??0)}${I}${We.slice((s?.end??0)+1)})},[o,s?.start,s?.end]),_=B(()=>{k.time("filtering by projects");let E=y.filter(w=>w.projects.some(I=>d.includes(I))).slice(0,n).map(w=>({title:w.title,projects:w.projects.flatMap(I=>d.includes(I)?[{name:I,mark:m&&I===scrapbox.Project.name?"":$e(i[I]??"",import.meta.url)||I[0],confirm:()=>h(w.title,I)}]:[]),confirm:()=>h(w.title)}));return k.timeEnd("filtering by projects"),E},[d,y,n,i,m,h]),{selectedIndex:L,next:S,prev:C,selectLast:z,selectFirst:Ae}=Mt(_.length),Ke=T(()=>L===-1?!1:(_.at(L)?.confirm?.(),!0),[L,_]);v(()=>l(_.length===0?void 0:{selectNext:E=>(S(E),!0),selectPrev:E=>(C(E),!0),selectFirst:()=>(Ae(),!0),selectLast:()=>(z(),!0),confirm:Ke,cancel:()=>(u({type:"cancel"}),!0)}),[l,S,C,Ae,z,Ke]);let cr=B(()=>{let E=new Set;for(let w of y)for(let I of w.projects)E.add(I);return a.flatMap(w=>E.has(w)?[{name:w,enable:d.includes(w),mark:$e(i[w]??"",import.meta.url)||w[0],onClick:()=>{p(w,!d.includes(w)),H().focus()}}]:[])},[y,a,d,i]),{ref:lr,top:$,left:he,right:xe}=Yt(t??{line:0,char:0}),ur=B(()=>r==="completion"&&_.length>0&&$!=null&&he!=null?{top:$,left:he}:{display:"none"},[_.length,$,he,r]),pr=B(()=>r==="completion"&&y.length>0&&$!=null&&xe!=null&&a.length>1?{top:$,right:xe}:{display:"none"},[$,xe,y.length,a.length,r]);return b(O,null,b("div",{className:"container projects",style:pr},cr.map(E=>b(Yr,{...E}))),b("div",{ref:lr,className:"container candidates",style:ur},_.map((E,w)=>b(Pt,{key:E.title,...E,selected:L===w})),y.length>n&&b("div",{className:"counter"},${y.length-n} more links)))},Yr=e=>b("div",{className:e.enable?"mark":"mark disabled",onClick:e.onClick,title:e.name},e.mark instanceof URL?b("img",{src:e.mark.href}):${e.mark});var rr=()=>b("style",null,.container { position: absolute;
margin-top: 14px;
max-height: 80vh;
z-index: 301;
background-color: var(--select-suggest-bg, #111); font-family: var(--select-suggest-font-family, "Open Sans", Helvetica, Arial, "Hiragino Sans", sans-serif);
color: var(--select-suggest-text-color, #eee); border-radius: 4px;
}
.candidates {
max-width: 80vw;
}
.projects {
max-width: 10vw;
margin-right: 4px;
}
.container.candidates > :not(:first-child) {
border-top: 1px solid var(--select-suggest-border-color, #eee); }
.container.candidates > *{
font-size: 11px;
line-height: 1.2em;
padding: 0.5em 10px;
}
.candidate {
display: flex;
}
a {
display: block;
text-decoration: none;
color: inherit;
}
a:not(.mark) {
width: 100%;
}
.selected a {
background-color: var(--select-suggest-selected-bg, #222); text-decoration: underline
}
img {
height: 1.3em;
width: 1.3em;
position: relative;
object-fit: cover;
object-position: 0% 0%;
}
.disabled {
filter: grayscale(1.0) opacity(0.5);
}
.counter {
color: var(--select-suggest-information-text-color, #aaa); font-size: 80%;
font-style: italic;
});var nr=(e,t)=>{let{state:r,...n}=e;if(r==="disabled")return t.type==="enable"?{state:"idle",...n}:e;switch(t.type){case"completionupdate":{let{type:s,...o}=t;return e.state==="canceled"?e:{state:"completion",...o}}case"completionend":return r==="idle"?e:{state:"idle",...n};case"cancel":return r==="canceled"?e:{state:"canceled",...n};case"disable":return{state:"disabled",...n};case"enable":return e}};var or={selectNext:()=>!1,selectPrev:()=>!1,selectFirst:()=>!1,selectLast:()=>!1,confirm:()=>!1,cancel:()=>!1},sr=e=>{let{callback:t,projects:r,...n}=e,[s,o]=ve(nr,{state:"idle",query:""}),[c,l]=M(!1);v(()=>{if(c){o({type:"disable"});return}let h=()=>o({type:scrapbox.Layout==="page"?"enable":"disable"});return h(),scrapbox.addListener("layout:changed",h),()=>scrapbox.removeListener("layout:changed",h)},[c]);let{text:a,range:u}=Tt();v(()=>{if(s.state!=="idle"&&s.state!=="completion"&&s.state!=="canceled"||scrapbox.Layout!=="page")return;let h=scrapbox.Page.lines[u.start.line];if(!zr(h,a)){s.state==="completion"&&s.context==="input"||(console.info("End completion due to no selection"),o({type:"completionend"}));return}if(s.state==="canceled")return;let _=Math.min(u.start.line,u.end.line),L=u.start.line<u.end.line?u.start.char:u.start.line===u.start.line?Math.min(u.start.char,u.end.char):u.end.char;o({type:"completionupdate",query:a.trim(),context:"selection",position:{line:_,char:L}})},[s.state,s.context,a,u]);let[i,m]=M(!1);v(()=>{if(s.state!=="idle"&&s.state!=="completion"&&s.state!=="canceled"){m(!1);return}if(s.state==="completion"&&s.context==="selection"){m(!1);return}let h=Y(),_=()=>{let{line:L,char:S}=h.getPosition(),C=ir(L,S);if(!C){m(!1),o({type:"completionend"});return}if(m(s.state!=="canceled"),scrapbox.Layout!=="page"||s.state==="canceled")return;let z=scrapbox.Page.lines[L];o({type:"completionupdate",query:z.text.slice(C.start+1,C.end-1),context:"input",range:C,position:{line:L,char:C.start}})};return scrapbox.addListener("lines:changed",_),()=>scrapbox.removeListener("lines:changed",_)},[s.state,s.context]),v(()=>{if(!i)return;let h=Y(),_=()=>{let{line:L,char:S}=h.getPosition();ir(L,S)||o({type:"completionend"})};return h.addChangeListener(_),()=>h.removeChangeListener(_)},[i]);let d=T(()=>l(!1),[]),p=T(()=>l(!0),[]),f=oe({...or,enable:d,disable:p}),[y,x]=M();return v(()=>{Object.assign(f.current,s.state!=="completion"||!y?or:y)},[s.state,y]),v(()=>t(f.current),[t]),b(O,null,b(rr,null),b(tr,{callback:x,projects:r,dispatch:o,...s,...n}))},zr=(e,t)=>!(t.trim()===""||t.includes(
`)||"codeBlock"in e||"title"in e||"tableBlock"in e&&e.tableBlock.start),ir=(e,t)=>{let r=G(e,t);if(!r)return;let n=r.closest('a.page-link:not(type="hashTag")');if(!n)return;if(!(n instanceof HTMLAnchorElement))throw TypeError('a.page-link:not(type="hashTag") is not HTMLAnchorElement');let s=Array.from(n.getElementsByClassName("char-index"));if(s.length===0)throw Error("a.page-link must have a char at least.");let o=n.closest(".cursor-line")!=null,c=parseInt(s0.dataset.charIndex??"0"),l=parseInt(ss.length-1.dataset.charIndex??"0");return o?{start:c,end:l}:{start:c-1,end:l+1}};var ar=e=>{let t=document.createElement("div");t.dataset.userscriptName="scrapbox-select-suggestion";let r=t.attachShadow({mode:"open"});document.body.append(t);let{limit:n=5,debug:s=!1,mark:o={},hideSelfMark:c=!0,enableSelfProjectOnStart:l=!0}=e??{},a=(()=>{if(!e?.projects||e.projects.length===0)returnscrapbox.Project.name;let u=l?scrapbox.Project.name,...e.projects:e.projects;return u.filter((i,m)=>!u.some((d,p)=>p<m&&d===i))})();return Kt(s),new Promise(u=>ye(b(sr,{limit:n,projects:a,mark:o,hideSelfMark:c,callback:u,enableSelfProjectOnStart:l}),r))};await ar();