generated at
import-devにbackgroundでテストコードを貼り付けるUserScript
script.js
var Le=e=>typeof e=="object"&&e!==null,Te=e=>Le(e)?(e.name===void 0||typeof e.name=="string")&&typeof e.message=="string":!1,W=e=>{try{let t=typeof e=="string"?JSON.parse(e):e;return Te(t)?t:!1}catch(t){if(t instanceof SyntaxError)return!1;throw t}};var Y=!1;scrapbox.addListener("lines:changed",()=>Y=!1);scrapbox.addListener("layout:changed",()=>Y=!1);var J=(e,t)=>{if(!(e instanceof HTMLDivElement))throw new TypeError(`"${t}" must be HTMLDivElememt but actual is "${e}"`)};var X=(e,t)=>{if(!(e instanceof HTMLTextAreaElement))throw new TypeError(`"${t}" must be HTMLTextAreaElement but actual is "${e}"`)};var w=()=>{let e=document.getElementById("text-input");if(!!e)return X(e,"textarea#text-input"),e};var V=()=>Ce(document.getElementsByClassName("status-bar")?.[0],"div.status-bar"),Ce=(e,t)=>{if(!!e)return J(e,t),e};var O=e=>new Promise(t=>setTimeout(()=>t(),e));var ee=()=>{let e=V();if(!e)throw new Error("div.status-bar can't be found");let t=document.createElement("div");return e.append(t),{render:(...n)=>{t.textContent="";let r=te(...n);r&&t.append(r)},dispose:()=>t.remove()}},te=(...e)=>{let t=e.flatMap(r=>{switch(r.type){case"spinner":return[Se()];case"check-circle":return[Ne()];case"exclamation-triangle":return[De()];case"text":return[S(r.text)];case"group":{let o=te(...r.items);return o?[o]:[]}}});if(t.length===0)return;if(t.length===1)return t[0];let n=document.createElement("span");return n.classList.add("item-group"),n.append(...t),n},S=e=>{let t=document.createElement("span");return t.classList.add("item"),t.append(e),t},Se=()=>{let e=document.createElement("i");return e.classList.add("fa","fa-spinner"),S(e)},Ne=()=>{let e=document.createElement("i");return e.classList.add("kamon","kamon-check-circle"),S(e)},De=()=>{let e=document.createElement("i");return e.classList.add("fas","fa-exclamation-triangle"),S(e)};var A=e=>[...e].map((t,n)=>t===" "?"_":!je.includes(t)||n===e.length-1&&Be.includes(t)?encodeURIComponent(t):t).join(""),je='@$&+=:;",',Be=':;",';var ne=e=>{if(e===0)return;let t={};return e&1&&(t.capture=!0),e&2&&(t.once=!0),e&4&&(t.passive=!0),t};var Ue=new Map,Ar=new Map;scrapbox.on("layout:changed",()=>{let e=w();if(!!e)for(let[t,n]of Ue)for(let[r,o]of n)for(let s of o)e.addEventListener(t,r,ne(s))});var re=e=>Fe.includes(e.name),Fe=["SocketIOError","DuplicateTitleError","NotFastForwardError"];var N=async()=>{let t=(await _e())("https://scrapbox.io",{reconnectionDelay:5e3,transports:["websocket"]});return await new Promise((n,r)=>{let o=s=>r(s);t.once("connect",()=>{t.off("disconnect",o),n()}),t.once("disconnect",o)}),t},$e="4.2.0",oe=`https://cdnjs.cloudflare.com/ajax/libs/socket.io/${$e}/socket.io.min.js`,U,_e=async()=>{if(U)throw U;if(!document.querySelector(`script[src="${oe}"]`)){let e=document.createElement("script");e.src=oe,await new Promise((t,n)=>{e.onload=()=>t(),e.onerror=r=>{U=r,n(r)},document.head.append(e)})}return new Promise(e=>{let t=setInterval(()=>{!io||(clearInterval(t),e(io))},500)})};var F=(e,t=9e4)=>{let n=(o,s)=>{let a;return new Promise((i,p)=>{let u=c=>{clearTimeout(a),p(new Error(c))};e.emit(o,s,c=>{switch(clearTimeout(a),e.off("disconnect",u),o){case"socket.io-request":"error"in c?typeof c.error=="object"&&c.error&&"name"in c.error&&typeof c.error.name=="string"&&re({name:c.error.name})?i({ok:!1,value:c.error}):i({ok:!1,value:{name:"UnexpectedError",value:c.error}}):"data"in c&&i({ok:!0,value:c.data});break;case"cursor":"error"in c?i({ok:!1,value:{name:"UnexpectedError",value:c.error}}):"data"in c&&i({ok:!0,value:c.data});break}p(new Error('Invalid response: missing "data" or "error" field'))}),a=setTimeout(()=>{e.off("disconnect",u),i({ok:!1,value:{name:"TimeoutError",message:`Timeout: exceeded ${t}ms`}})},t),e.once("disconnect",u)})};async function*r(...o){let s,a=()=>new Promise(p=>s=p),i=p=>{s?.(p)};for(let p of o)e.on(p,i);try{for(;;)yield await a()}finally{for(let p of o)e.off(p,i)}}return{request:n,response:r}};var $=async e=>{if(e.connected)return;let t=new Promise(n=>e.once("connect",()=>n()));e.connect(),await t},_=async e=>{if(e.disconnected)return;let t=new Promise(n=>{let r=o=>{o==="io client disconnect"&&(n(),e.off("disconnect",r))};e.on("disconnect",r)});e.disconnect(),await t};var q=(e,t)=>{let n=e.length>t.length,r=n?t:e,o=n?e:t,s=r.length+1,a=r.length+o.length+3,i=new Array(a);i.fill(-1);let p=[];function u(d,m,g){let b=Math.max(m,g),R=b-d;for(;R<r.length&&b<o.length&&r[R]===o[b];)++R,++b;return i[d+s]=p.length,p.push([{x:R,y:b},i[d+(m>g?-1:1)+s]]),b}let c=new Array(a);c.fill(-1);let x=-1,l=o.length-r.length;do{++x;for(let d=-x;d<=l-1;++d)c[d+s]=u(d,c[d-1+s]+1,c[d+1+s]);for(let d=l+x;d>=l+1;--d)c[d+s]=u(d,c[d-1+s]+1,c[d+1+s]);c[l+s]=u(l,c[l-1+s]+1,c[l+1+s])}while(c[l+s]!==o.length);let E=[],f=i[l+s];for(;f!==-1;)E.push(p[f][0]),f=p[f][1];return{from:e,to:t,editDistance:l+x*2,buildSES:function*(){let d=0,m=0;for(let{x:g,y:b}of qe(E))for(;d<g||m<b;)b-g>m-d?(yield{value:o[m],type:n?"deleted":"added"},++m):b-g<m-d?(yield{value:r[d],type:n?"added":"deleted"},++d):(yield{value:r[d],type:"common"},++d,++m)}}};function*K(e){let t=[],n=[];function*r(){if(t.length>n.length){for(let o=0;o<n.length;o++)yield se(t[o],n[o]);for(let o=n.length;o<t.length;o++)yield t[o]}else{for(let o=0;o<t.length;o++)yield se(t[o],n[o]);for(let o=t.length;o<n.length;o++)yield n[o]}t=[],n=[]}for(let o of e)switch(o.type){case"added":t.push(o);break;case"deleted":n.push(o);break;case"common":yield*r(),yield o;break}yield*r()}var se=(e,t)=>({value:e.value,oldValue:t.value,type:"replaced"});function*qe(e){for(let t=e.length-1;t>=0;t--)yield e[t]}var v=e=>{let{fetch:t=globalThis.fetch,hostName:n="scrapbox.io",...r}=e;return{fetch:t,hostName:n,...r}};var L=class extends Error{constructor(t){super(`${t.status} ${t.statusText} when fetching ${t.url}`);this.response=t;this.name="UnexpectedResponseError";Error.captureStackTrace&&Error.captureStackTrace(this,L)}},T=async e=>{let t=e.clone(),n=await t.text(),r=W(n);if(!r)throw new L(t);return{ok:!1,value:r}};var G=async e=>{let{sid:t,hostName:n,fetch:r}=v(e??{}),o=new Request(`https://${n}/api/users/me`,t?{headers:{Cookie:k(t)}}:void 0),s=await r(o);if(!s.ok)throw new L(s);return await s.json()};var k=e=>`connect.sid=${e}`;var ie=(e,t)=>{let{sid:n,hostName:r}=v(t??{});return new Request(`https://${r}/api/projects/${e}`,n?{headers:{Cookie:k(n)}}:void 0)},ae=async e=>{if(!e.ok)return T(e);let t=await e.json();return{ok:!0,value:t}},D=async(e,t)=>{let{fetch:n}=v(t??{}),r=ie(e,t),o=await n(r);return ae(o)};D.toRequest=ie;D.fromResponse=ae;var ce=(e,t)=>{let{sid:n,hostName:r}=v(t??{}),o=new URLSearchParams;for(let s of e)o.append("ids",s);return new Request(`https://${r}/api/projects?${o.toString()}`,n?{headers:{Cookie:k(n)}}:void 0)},pe=async e=>{if(!e.ok)return T(e);let t=await e.json();return{ok:!0,value:t}},le=async(e,t)=>{let{fetch:n}=v(t??{}),r=await n(ce(e,t));return pe(r)};le.toRequest=ce;le.fromResponse=pe;var j,de=async()=>{if(j!==void 0)return j;let e=await G();if(e.isGuest)throw new Error("this script can only be executed by Logged in users");return j=e.id,j},ue=new Map,z=async e=>{let t=ue.get(e);if(t!==void 0)return t;let n=await D(e);if(!n.ok){let{name:o,message:s}=n.value;throw new Error(`${o} ${s}`)}let{id:r}=n.value;return ue.set(e,r),r},me=e=>e.padStart(8,"0"),I=e=>{let t=Math.floor(new Date().getTime()/1e3).toString(16),n=Math.floor(16777214*Math.random()).toString(16);return`${me(t).slice(-8)}${e.slice(-6)}0000${me(n)}`};var h=(e,{parseOnNested:t,parseOnQuoted:n,patterns:r})=>(o,s,a)=>{var i,p,u,c,x,l;if(!t&&s.nested)return(i=a?.())!==null&&i!==void 0?i:[];if(!n&&s.quoted)return(p=a?.())!==null&&p!==void 0?p:[];for(let E of r){let f=E.exec(o);if(f===null)continue;let d=o.substring(0,f.index),m=o.substring(f.index+((c=(u=f[0])===null||u===void 0?void 0:u.length)!==null&&c!==void 0?c:0)),g=e((x=f[0])!==null&&x!==void 0?x:"",s);return[...C(d,s),...g,...C(m,s)]}return(l=a?.())!==null&&l!==void 0?l:[]},y=e=>[{type:"plain",raw:e,text:e}],Ke=h(y,{parseOnNested:!0,parseOnQuoted:!0,patterns:[/^()(.*)()$/]}),Ge=/^>.*$/,ze=(e,t)=>t.context==="table"?y(e,t):[{type:"quote",raw:e,nodes:C(e.substring(1),{...t,quoted:!0})}],Qe=h(ze,{parseOnNested:!1,parseOnQuoted:!1,patterns:[Ge]}),We=/^\? .+$/,Ye=(e,t)=>t.context==="table"?y(e,t):[{type:"helpfeel",raw:e,text:e.substring(2)}],Je=h(Ye,{parseOnNested:!1,parseOnQuoted:!1,patterns:[We]}),Xe=/\[\[https?:\/\/[^\s\]]+\.(?:png|jpe?g|gif|svg)\]\]/i,Ve=/\[\[https?:\/\/(?:[0-9a-z-]+\.)?gyazo\.com\/[0-9a-f]{32}\]\]/,Ze=(e,t)=>{if(t.context==="table")return y(e,t);let n=e.substring(2,e.length-2),r=/^https?:\/\/([0-9a-z-]\.)?gyazo\.com\/[0-9a-f]{32}$/.test(n);return[{type:"strongImage",raw:e,src:r?`${n}/thumb/1000`:n}]},et=h(Ze,{parseOnNested:!1,parseOnQuoted:!0,patterns:[Xe,Ve]}),tt=/\[[^[\]]*\.icon(?:\*[1-9]\d*)?\]/;function fe(e){return(t,n)=>{if(e==="strongIcon"&&n.context==="table")return y(t,n);let r=e==="icon"?t.substring(1,t.length-1):t.substring(2,t.length-2),o=r.lastIndexOf(".icon"),s=r.substring(0,o),a=s.startsWith("/")?"root":"relative",i=r.substring(o+5,r.length),p=i.startsWith("*")?parseInt(i.substring(1),10):1;return new Array(p).fill({}).map(()=>({path:s,pathType:a,type:e,raw:t}))}}var nt=fe("icon"),rt=h(nt,{parseOnNested:!0,parseOnQuoted:!0,patterns:[tt]}),ot=/\[\[[^[\]]*\.icon(?:\*\d+)?\]\]/,st=fe("strongIcon"),it=h(st,{parseOnNested:!1,parseOnQuoted:!0,patterns:[ot]}),at=/\[\[(?:[^[]|\[[^[]).*?\]*\]\]/,ct=(e,t)=>t.context==="table"?y(e,t):[{type:"strong",raw:e,nodes:C(e.substring(2,e.length-2),{...t,nested:!0})}],pt=h(ct,{parseOnNested:!1,parseOnQuoted:!0,patterns:[at]}),lt=/\[\$ .+? \]/,dt=/\[\$ [^\]]+\]/,ut=(e,t)=>t.context==="table"?y(e,t):[{type:"formula",raw:e,formula:e.substring(3,e.length-(e.endsWith(" ]")?2:1))}],mt=h(ut,{parseOnNested:!1,parseOnQuoted:!0,patterns:[lt,dt]}),ft=/\[[!"#%&'()*+,\-./{|}<>_~]+ (?:\[[^[\]]+\]|[^\]])+\]/,gt=(e,t)=>{if(t.context==="table")return y(e,t);let n=e.indexOf(" "),r=e.substring(1,n),o=e.substring(n+1,e.length-1),s=new Set(r);if(s.has("*")){let a=r.split("*").length-1;s.delete("*"),s.add(`*-${Math.min(a,10)}`)}return[{type:"decoration",raw:e,rawDecos:r,decos:Array.from(s),nodes:C(o,{...t,nested:!0})}]},ht=h(gt,{parseOnNested:!1,parseOnQuoted:!0,patterns:[ft]}),xt=/`.*?`/,yt=(e,t)=>t.context==="table"?y(e,t):[{type:"code",raw:e,text:e.substring(1,e.length-1)}],Et=h(yt,{parseOnNested:!1,parseOnQuoted:!0,patterns:[xt]}),bt=/^[$%] .+$/,vt=(e,t)=>{var n;if(t.context==="table")return y(e,t);let r=(n=e[0])!==null&&n!==void 0?n:"",o=e.substring(2);return[{type:"commandLine",raw:e,symbol:r,text:o}]},kt=h(vt,{parseOnNested:!1,parseOnQuoted:!1,patterns:[bt]}),wt=/\[\s+\]/,Lt=(e,t)=>t.context==="table"?y(e,t):[{type:"blank",raw:e,text:e.substring(1,e.length-1)}],Tt=h(Lt,{parseOnNested:!1,parseOnQuoted:!0,patterns:[wt]}),Ct=/\[https?:\/\/[^\s\]]+\.(?:png|jpe?g|gif|svg)(?:\?[^\]\s]+)?(?:\s+https?:\/\/[^\s\]]+)?\]/i,Pt=/\[https?:\/\/[^\s\]]+\s+https?:\/\/[^\s\]]+\.(?:png|jpe?g|gif|svg)(?:\?[^\]\s]+)?\]/i,It=/\[https?:\/\/(?:[0-9a-z-]+\.)?gyazo\.com\/[0-9a-f]{32}(?:\/raw)?(?:\s+https?:\/\/[^\s\]]+)?\]/,Mt=/\[https?:\/\/[^\s\]]+\s+https?:\/\/(?:[0-9a-z-]+\.)?gyazo\.com\/[0-9a-f]{32}(?:\/raw)?\]/,Rt=e=>/^https?:\/\/[^\s\]]+\.(png|jpe?g|gif|svg)(\?[^\]\s]+)?$/i.test(e)||Ot(e),Ot=e=>/^https?:\/\/([0-9a-z-]\.)?gyazo\.com\/[0-9a-f]{32}(\/raw)?$/.test(e),St=(e,t)=>{if(t.context==="table")return y(e,t);let n=e.search(/\s/),r=n!==-1?e.substring(1,n):e.substring(1,e.length-1),o=n!==-1?e.substring(n,e.length-1).trimLeft():"",[s,a]=Rt(o)?[o,r]:[r,o];return[{type:"image",raw:e,src:/^https?:\/\/([0-9a-z-]\.)?gyazo\.com\/[0-9a-f]{32}$/.test(s)?`${s}/thumb/1000`:s,link:a}]},Nt=h(St,{parseOnNested:!0,parseOnQuoted:!0,patterns:[Ct,Pt,It,Mt]}),Dt=/\[https?:\/\/[^\s\]]+\s+[^\]]*[^\s]\]/,jt=/\[[^[\]]*[^\s]\s+https?:\/\/[^\s\]]+\]/,Bt=/\[https?:\/\/[^\s\]]+\]/,Ht=/https?:\/\/[^\s]+/,At=(e,t)=>{if(t.context==="table")return y(e,t);let n=e.startsWith("[")&&e.endsWith("]")?e.substring(1,e.length-1):e,r=/^https?:\/\/[^\s\]]/.test(n),o=(r?/^https?:\/\/[^\s\]]+/:/https?:\/\/[^\s\]]+$/).exec(n);if(o?.[0]===void 0)return[];let s=r?n.substring(o[0].length):n.substring(0,o.index-1);return[{type:"link",raw:e,pathType:"absolute",href:o[0],content:s.trim()}]},Ut=h(At,{parseOnNested:!0,parseOnQuoted:!0,patterns:[Dt,jt,Bt,Ht]}),ge=/\[([^\]]*[^\s])\s+([NS]\d+(?:\.\d+)?,[EW]\d+(?:\.\d+)?(?:,Z\d+)?)\]/,he=/\[([NS]\d+(?:\.\d+)?,[EW]\d+(?:\.\d+)?(?:,Z\d+)?)(?:\s+([^\]]*[^\s]))?\]/,Ft=e=>{let[t="",n="",r=""]=e.split(","),o=parseFloat(t.replace(/^N/,"").replace(/^S/,"-")),s=parseFloat(n.replace(/^E/,"").replace(/^W/,"-")),a=/^Z\d+$/.test(r)?parseInt(r.replace(/^Z/,""),10):14;return{latitude:o,longitude:s,zoom:a}},$t=(e,t)=>{var n;if(t.context==="table")return y(e,t);let r=(n=e.match(ge))!==null&&n!==void 0?n:e.match(he);if(r===null)return[];let o=e.startsWith("[N")||e.startsWith("[S"),[,s="",a=""]=o?r:[r[0],r[2],r[1]],{latitude:i,longitude:p,zoom:u}=Ft(s),c=a!==""?`https://www.google.com/maps/place/${encodeURIComponent(a)}/@${i},${p},${u}z`:`https://www.google.com/maps/@${i},${p},${u}z`;return[{type:"googleMap",raw:e,latitude:i,longitude:p,zoom:u,place:a,url:c}]},_t=h($t,{parseOnNested:!1,parseOnQuoted:!0,patterns:[ge,he]}),qt=/\[\/?[^[\]]+\]/,Kt=e=>{let t=e.substring(1,e.length-1);return[{type:"link",raw:e,pathType:t.startsWith("/")?"root":"relative",href:t,content:""}]},Gt=h(Kt,{parseOnNested:!0,parseOnQuoted:!0,patterns:[qt]}),zt=/(?:^|\s)#\S+/,Qt=(e,t)=>{if(t.context==="table")return y(e,t);if(e.startsWith("#"))return[{type:"hashTag",raw:e,href:e.substring(1)}];let n=e.substring(0,1),r=e.substring(1);return[...y(n,t),{type:"hashTag",raw:r,href:r.substring(1)}]},Wt=h(Qt,{parseOnNested:!0,parseOnQuoted:!0,patterns:[zt]}),Yt=/^[0-9]+\. .*$/,Jt=(e,t)=>{if(t.context==="table")return y(e,t);let n=e.indexOf(" "),r=e.substring(0,n-1),o=parseInt(r,10),s=e.substring(n+1,e.length);return[{type:"numberList",raw:e,rawNumber:r,number:o,nodes:C(s,{...t,nested:!0})}]},Xt=h(Jt,{parseOnNested:!1,parseOnQuoted:!1,patterns:[Yt]}),Vt=(e,t,n)=>{var r;return e===""?[]:(r=n?.())!==null&&r!==void 0?r:[]},Zt=(...e)=>(t,n)=>e.reduceRight((r,o)=>()=>o(t,n,r),()=>Ke(t,n))(),C=Zt(Vt,Qe,Je,Et,kt,mt,Tt,ht,et,it,pt,Nt,Ut,rt,_t,Gt,Wt,Xt);var xe=(e,t,n)=>{let{sid:r,hostName:o,followRename:s,projects:a}=v(n??{}),i=new URLSearchParams;i.append("followRename",`${s??!0}`);for(let u of a??[])i.append("projects",u);let p=`https://${o}/api/pages/${e}/${A(t)}?${i.toString()}`;return new Request(p,r?{headers:{Cookie:k(r)}}:void 0)},ye=async e=>{if(!e.ok)return e.status===414?{ok:!1,value:{name:"TooLongURIError",message:"project ids may be too much."}}:T(e);let t=await e.json();return{ok:!0,value:t}},B=async(e,t,n)=>{let{fetch:r}=v(n??{}),o=xe(e,t,n),s=await r(o);return await ye(s)};B.toRequest=xe;B.fromResponse=ye;var Ee=(e,t)=>{let{sid:n,hostName:r,sort:o,limit:s,skip:a}=v(t??{}),i=new URLSearchParams;o!==void 0&&i.append("sort",o),s!==void 0&&i.append("limit",`${s}`),a!==void 0&&i.append("skip",`${a}`);let p=`https://${r}/api/pages/${e}?${i.toString()}`;return new Request(p,n?{headers:{Cookie:k(n)}}:void 0)},be=async e=>{if(!e.ok)return T(e);let t=await e.json();return{ok:!0,value:t}},ve=async(e,t)=>{let{fetch:n}=v(t??{}),r=await n(Ee(e,t));return await be(r)};ve.toRequest=Ee;ve.fromResponse=be;var M=async(e,t)=>{let n=await B(e,t);if(!n.ok)throw new Error(`You have no privilege of editing "/${e}/${t}".`);return n.value};var P=async(e,t,n,r)=>{let[o,s,a]=await Promise.all([M(e,t),z(e),de()]),i={...o,projectId:s,userId:a},p=r?.socket,u=p??await N();await $(u);try{let{request:c}=F(u),x=0,l=[],E;for(;r?.maxAttempts===void 0||x<r.maxAttempts;){let f=n(i,x,l,E);if(l=f instanceof Promise?await f:f,x++,l.length===0)return{ok:!0,value:i.commitId};let d={kind:"page",projectId:s,pageId:i.id,parentId:i.commitId,userId:a,changes:l,cursor:null,freeze:!0};for(;;){let m=await c("socket.io-request",{method:"commit",data:d});if(m.ok)return i.commitId=m.value.commitId,{ok:!0,value:i.commitId};let g=m.value.name;if(g==="UnexpectedError"){let b=new Error;throw b.name=m.value.name,b.message=JSON.stringify(m.value),b}if(g==="TimeoutError"||g==="SocketIOError"){await O(3e3);break}g==="NotFastForwardError"&&(i={...await M(e,t),projectId:s,userId:a}),E=g}}return{ok:!1,value:{name:"RetryError",attempts:x,message:`Retrying exceeded the maxAttempts (${x}).`}}}finally{p||await _(u)}};var Q=e=>{let t=e.match(/^(\s*)code:(.+?)(\(.+\)){0,1}\s*$/);if(t===null)return null;let n=t[2].trim(),r="";if(t[3]===void 0){let o=n.match(/.+\.(.*)$/);if(o===null)r=n;else{if(o[1]==="")return null;r=o[1].trim()}}else r=t[3].slice(1,-1);return{filename:n,lang:r,indent:t[1].length}};function H(e){return e.titleLine.text.length-e.titleLine.text.trimStart().length+1}var ke=async(e,t)=>{let n=await tn(e),r=[],o={isCodeBlock:!1,filename:"",lang:"",indent:0};for(let s of n)if(o.isCodeBlock){if(rn(s.text,o.indent)===null){r[r.length-1].nextLine=s,o.isCodeBlock=!1;continue}r[r.length-1].bodyLines.push(s)}else{let a=Q(s.text);if(a===null){o.isCodeBlock=!1;continue}o={isCodeBlock:!0,...a},r.push({filename:o.filename,lang:o.lang,titleLine:s,bodyLines:[],nextLine:null,pageInfo:{projectName:e.project,pageTitle:e.title}})}return r.filter(s=>nn(s,t))},tn=async e=>e.lines!==void 0?e.lines:(await M(e.project,e.title)).lines,nn=(e,t)=>{let n=(r,o)=>!r||r===o;return n(t?.filename,e.filename)&&n(t?.lang,e.lang)&&n(t?.titleLineId,e.titleLine.id)},rn=(e,t)=>{let n=e.replaceAll("\r","").match(/^(\s*)(.*)$/);if(n===null||n.length<2)return null;let r=n[1],o=n[2];return r.length<=t?null:r.slice(r.length-t)+o};var we=(e,t,n,r)=>{let o={insertPositionIfNotExist:"notInsert",isInsertEmptyLineInTail:!0,debug:!1},s=r?{...o,...r}:o;return P(t,n,async a=>{let i=a.lines,p=await ke({project:t,title:n,lines:i},{filename:e.filename}),u=[...sn(p,e,i,{...s,userId:a.userId})];if(s.debug){console.log("%cvvv original code Blocks vvv","color: limegreen;"),console.log(p),console.log("%cvvv new codes vvv","color: limegreen;");let c=Array.isArray(e.content)?e.content:e.content.split(` `);console.log(c),console.log("%cvvv commits vvv","color: limegreen;"),console.log(u)}return u},r)},on=e=>e.map(t=>{let n=H(t);return t.bodyLines.map(r=>({...r,text:r.text.slice(n)}))}).flat();function*sn(e,t,n,{userId:r,insertPositionIfNotExist:o,isInsertEmptyLineInTail:s}){function a(l){return" ".repeat(H(l))}let i=[...e],p=on(e);if(i.length<=0){if(o==="notInsert")return;let l=o==="top"&&n.length>1?n[1]:null,E={_insert:l?.id??"_end",lines:{id:I(r),text:an(t)}};yield E,i.push({titleLine:{...E.lines,userId:r,created:-1,updated:-1},bodyLines:[],nextLine:l})}let{buildSES:u}=q(p.map(l=>l.text),Array.isArray(t.content)?t.content:t.content.split(` `)),c=0,x=!1;for(let l of K(u())){let{lineId:E,codeIndex:f}=(()=>{if(c>=p.length){let m=i.length-1;return{lineId:i[m].nextLine?.id??"_end",codeIndex:m}}return{lineId:p[c].id,codeIndex:i.findIndex(m=>m.bodyLines.some(g=>g.id==p[c].id))}})(),d=i[f];if(l.type=="added"){let m=E==d.bodyLines[0]?.id&&f>=1?i[f-1]:i[f],g=m?.nextLine?.id??"_end";yield{_insert:g,lines:{id:I(r),text:a(m)+l.value}},g=="_end"&&(x=!0);continue}else l.type=="deleted"?yield{_delete:E,lines:-1}:l.type=="replaced"&&(yield{_update:E,lines:{text:a(d)+l.value}});c++}x&&s&&(yield{_insert:"_end",lines:{id:I(r),text:""}})}var an=e=>`code:${e.filename+(e.lang?`(${e.lang})`:"")}`;scrapbox.PageMenu.addMenu({title:"Update dev UserScript",image:"https://i.gyazo.com/95e1d28e25db16bf7c4fdeeb16452179.png",onClick:async()=>{let e=globalThis.prompt("type new code");if(!e?.trim?.())return;let t=ee();t.render({type:"group",items:[{type:"spinner"},{type:"text",text:"Update import-dev/mod.js..."}]});try{await we({filename:"mod.js",content:e},"takker","import-dev"),t.render({type:"group",items:[{type:"check-circle"},{type:"text",text:"Updated"}]})}catch(n){if(console.error(n),!(n instanceof Error))throw n;t.render({type:"group",items:[{type:"exclamation-triangle"},{type:"text",text:`${n.name} ${n.message}`}]})}finally{setTimeout(()=>t.dispose(),2e3)}}});