url-customizer-script
code:script.js
scrapbox.PopupMenu.addButton({
title: 'link',
onClick: text => {
if (text.match(/https?:\/\//)) {
return text.replace(/(https?:\/\/^\s+)/g, '$1') }
}
});
var Ve=t=>typeof t=="object"&&t!==null,Xe=t=>Ve(t)?(t.name===void 0||typeof t.name=="string")&&typeof t.message=="string":!1,ee=t=>{try{let r=typeof t=="string"?JSON.parse(t):t;return Xe(r)?r:!1}catch(r){if(r instanceof SyntaxError)return!1;throw r}};var te=!1;scrapbox.addListener("lines:changed",()=>te=!1);scrapbox.addListener("layout:changed",()=>te=!1);var re=(t,r)=>{if(!(t instanceof HTMLDivElement))throw new TypeError("${r}" must be HTMLDivElememt but actual is "${t}")};var oe=(t,r)=>{if(!(t instanceof HTMLTextAreaElement))throw new TypeError("${r}" must be HTMLTextAreaElement but actual is "${t}")};var R=()=>{let t=document.getElementById("text-input");if(!!t)return oe(t,"textarea#text-input"),t};var ne=()=>Je(document.getElementsByClassName("status-bar")?.0,"div.status-bar"),Je=(t,r)=>{if(!!t)return re(t,r),t};var G=t=>new Promise(r=>setTimeout(()=>r(),t));var ie=async t=>{let r=R();if(!r)throw Error("#text-input is not ditected.");r.focus(),r.value=t;let e=new InputEvent("input",{bubbles:!0});r.dispatchEvent(e),await G(1)};var ae=()=>{let t=ne();if(!t)throw new Error("div.status-bar can't be found");let r=document.createElement("div");return t.append(r),{render:(...e)=>{r.textContent="";let o=ce(...e);o&&r.append(o)},dispose:()=>r.remove()}},ce=(...t)=>{let r=t.flatMap(o=>{switch(o.type){case"spinner":returnrt();case"check-circle":returnot();case"exclamation-triangle":returnnt();case"text":returnO(o.text);case"group":{let n=ce(...o.items);return n?n:[]}}});if(r.length===0)return;if(r.length===1)return r0;let e=document.createElement("span");return e.classList.add("item-group"),e.append(...r),e},O=t=>{let r=document.createElement("span");return r.classList.add("item"),r.append(t),r},rt=()=>{let t=document.createElement("i");return t.classList.add("fa","fa-spinner"),O(t)},ot=()=>{let t=document.createElement("i");return t.classList.add("kamon","kamon-check-circle"),O(t)},nt=()=>{let t=document.createElement("i");return t.classList.add("fas","fa-exclamation-triangle"),O(t)};var x=t=>...t.map((r,e)=>r===" "?"_":!st.includes(r)||e===t.length-1&&it.includes(r)?encodeURIComponent(r):r).join(""),st='@$&+=:;",',it=':;",';var g=t=>typeof t=="string";var z=(t,...r)=>{let e=t;for(let o of r){let n=e instanceof Promise?e.then(s=>g(s)?s:o(s)):o(e);if(g(n))return n;e=n instanceof URL?new URL(n):n.then(s=>g(s)?s:new URL(s))}return e instanceof Promise?e.then(o=>${o}):${t}};var L=t=>t.ok?void 0:{name:"HTTPError",message:${t.status} ${t.statusText},response:t};var pe=t=>{if(!window.GM_fetch)return;let r=window.GM_fetch;return(async()=>{let e=await r(https://cdn.syndication.twimg.com/tweet-result?id=${t}&token=x),o=L(e);if(o)return{ok:!1,value:o};let n=await e.json();return{ok:!0,value:n}})()};var j=t=>{let{fetch:r=globalThis.fetch,...e}=t;return{fetch:r,...e}},de=t=>typeof t=="object"&&t!==null;var T=class extends Error{constructor(r){super(${r.status} ${r.statusText} when fetching ${r.path.toString()});this.name="UnexpectedResponseError";this.status=r.status,this.statusText=r.statusText,this.body=r.body,this.path=r.path,Error.captureStackTrace&&Error.captureStackTrace(this,T)}};var B=async t=>{let r=await t.text();if(t.ok)return{ok:!0,value:r};if(t.status===400)return{ok:!1,value:{name:"BadRequestError",message:r}};try{let e=JSON.parse(r);if(!de(e)||typeof e.message!="string")throw new T({status:t.status,statusText:t.statusText,body:r,path:new URL(t.url)});switch(t.status){case 401:return{ok:!1,value:{name:"UnauthorizedError",message:e.message}};case 403:return{ok:!1,value:{name:"NotPrivilegeError",message:e.message}};case 404:return{ok:!1,value:{name:"NotFoundError",message:e.message}};case 422:return{ok:!1,value:{name:"InvalidParameterError",message:e.message}};case 429:return{ok:!1,value:{name:"RateLimitError",message:e.message}};default:throw new T({status:t.status,statusText:t.statusText,body:r,path:new URL(t.url)})}}catch(e){throw e instanceof SyntaxError?new T({status:t.status,statusText:t.statusText,body:r,path:new URL(t.url)}):e}};var me=async(t,r)=>{let{title:e,description:o,metadataIsPublic:n,collectionId:s,refererURL:a,accessToken:i,created:p,app:f,fetch:m}=j(r),d=new FormData;d.append("imagedata",t),d.append("access_token",i),a&&d.append("referer_url",a.toString()),f!==void 0&&d.append("app",f),e!==void 0&&d.append("title",e),o!=null&&d.append("desc",o),s&&d.append("collection_id",s),n&&d.append("metadata_is_public","true"),p!==void 0&&d.append("created_at",${p});let k=await m("https://upload.gyazo.com/api/upload",{method:"POST",mode:"cors",credentials:"omit",body:d}),h=await B(k);return h.ok?{ok:!0,value:JSON.parse(h.value)}:h};var c=t=>{let{fetch:r=globalThis.fetch,hostName:e="scrapbox.io",...o}=t;return{fetch:r,hostName:e,...o}};var y=class extends Error{constructor(r){super(${r.status} ${r.statusText} when fetching ${r.url});this.response=r;this.name="UnexpectedResponseError";Error.captureStackTrace&&Error.captureStackTrace(this,y)}},l=async t=>{let r=t.clone(),e=await r.text(),o=ee(e);if(!o)throw new y(r);return{ok:!1,value:o}};var K=async t=>{let{sid:r,hostName:e,fetch:o}=c(t??{}),n=new Request(https://${e}/api/users/me,r?{headers:{Cookie:u(r)}}:void 0),s=await o(n);if(!s.ok)throw new y(s);return await s.json()};var u=t=>connect.sid=${t},E=async t=>window._csrf?window._csrf:(await K(t)).csrfToken;var ue=(t,r,e)=>{let{sid:o,hostName:n,followRename:s,projects:a}=c(e??{}),i=new URLSearchParams;i.append("followRename",${s??!0});for(let f of a??[])i.append("projects",f);let p=https://${n}/api/pages/${t}/${x(r)}?${i.toString()};return new Request(p,o?{headers:{Cookie:u(o)}}:void 0)},le=async t=>{if(!t.ok)return t.status===414?{ok:!1,value:{name:"TooLongURIError",message:"project ids may be too much."}}:l(t);let r=await t.json();return{ok:!0,value:r}},W=async(t,r,e)=>{let{fetch:o}=c(e??{}),n=ue(t,r,e),s=await o(n);return await le(s)};W.toRequest=ue;W.fromResponse=le;var fe=(t,r)=>{let{sid:e,hostName:o,sort:n,limit:s,skip:a}=c(r??{}),i=new URLSearchParams;n!==void 0&&i.append("sort",n),s!==void 0&&i.append("limit",${s}),a!==void 0&&i.append("skip",${a});let p=https://${o}/api/pages/${t}?${i.toString()};return new Request(p,e?{headers:{Cookie:u(e)}}:void 0)},ge=async t=>{if(!t.ok)return l(t);let r=await t.json();return{ok:!0,value:r}},he=async(t,r)=>{let{fetch:e}=c(r??{}),o=await e(fe(t,r));return await ge(o)};he.toRequest=fe;he.fromResponse=ge;var xe=(t,r,e,o)=>{let{sid:n,hostName:s}=c(o??{}),a=https://${s}/api/table/${t}/${x(r)}/${encodeURIComponent(e)}.csv;return new Request(a,n?{headers:{Cookie:u(n)}}:void 0)},we=async t=>t.ok?{ok:!0,value:await t.text()}:t.status===404?{ok:!1,value:{name:"NotFoundError",message:"Table not found."}}:l(t),ye=async(t,r,e,o)=>{let{fetch:n}=c(o??{}),s=xe(t,r,e,o),a=await n(s);return await we(a)};ye.toRequest=xe;ye.fromResponse=we;var Ee=(t,r)=>{let{sid:e,hostName:o}=c(r??{});return new Request(https://${o}/api/projects/${t},e?{headers:{Cookie:u(e)}}:void 0)},be=async t=>{if(!t.ok)return l(t);let r=await t.json();return{ok:!0,value:r}},V=async(t,r)=>{let{fetch:e}=c(r??{}),o=Ee(t,r),n=await e(o);return be(n)};V.toRequest=Ee;V.fromResponse=be;var ke=(t,r)=>{let{sid:e,hostName:o}=c(r??{}),n=new URLSearchParams;for(let s of t)n.append("ids",s);return new Request(https://${o}/api/projects?${n.toString()},e?{headers:{Cookie:u(e)}}:void 0)},Le=async t=>{if(!t.ok)return l(t);let r=await t.json();return{ok:!0,value:r}},Te=async(t,r)=>{let{fetch:e}=c(r??{}),o=await e(ke(t,r));return Le(o)};Te.toRequest=ke;Te.fromResponse=Le;var Re=async(t,r)=>{let{sid:e,hostName:o,fetch:n,csrf:s}=c(r??{}),a=new Request(https://${o}/api/embed-text/url?url=${encodeURIComponent(t.toString())},{method:"POST",headers:{"Content-Type":"application/json;charset=utf-8","X-CSRF-TOKEN":s??await E(r),...e?{Cookie:u(e)}:{}},body:JSON.stringify({timeout:3e3})}),i=await n(a);if(!i.ok)return i.status===422?{ok:!1,value:{name:"InvalidURLError",message:(await i.json()).message}}:l(i);let{title:p}=await i.json();return{ok:!0,value:p}};var Pe=async(t,r)=>{let{sid:e,hostName:o,fetch:n,csrf:s}=c(r??{}),a=new Request(https://${o}/api/embed-text/twitter?url=${encodeURIComponent(t.toString())},{method:"POST",headers:{"Content-Type":"application/json;charset=utf-8","X-CSRF-TOKEN":s??await E(r),...e?{Cookie:u(e)}:{}},body:JSON.stringify({timeout:3e3})}),i=await n(a);if(!i.ok)return i.status===422?{ok:!1,value:{name:"InvalidURLError",message:(await i.json()).message}}:l(i);let p=await i.json();return{ok:!0,value:p}};var Ie=async t=>{let{sid:r,hostName:e,gyazoTeamsName:o}=c(t??{}),n=new Request(https://${e}/api/login/gyazo/oauth-upload/token${o??gyazoTeamsName=${o}:""},r?{headers:{Cookie:u(r)}}:void 0),s=await fetch(n);if(!s.ok)return l(s);let{token:a}=await s.json();return{ok:!0,value:a}};var zs=new Map;var ve=(t,r,e,o)=>{let{sid:n,hostName:s}=c(o??{}),a=https://${s}/api/code/${t}/${x(r)}/${x(e)};return new Request(a,n?{headers:{Cookie:u(n)}}:void 0)},Ce=async t=>t.ok?{ok:!0,value:await t.text()}:t.status===404&&t.headers.get("Content-Type")?.includes?.("text/plain")?{ok:!1,value:{name:"NotFoundError",message:"Code block is not found"}}:l(t),Ne=async(t,r,e,o)=>{let{fetch:n}=c(o??{}),s=ve(t,r,e,o),a=await n(s);return await Ce(a)};Ne.toRequest=ve;Ne.fromResponse=Ce;var Me=new TextEncoder().encode("0123456789abcdef");function Ue(t){let r=new Uint8Array(t.length*2);for(let e=0;e<r.length;e++){let o=te;re*2=Meo>>4,re*2+1=Meo&15}return r}var Se="md5: data is invalid type",b=64,X=class{#e;#t;#r;#o;#n;#i;#s;#a;constructor(){this.#e=1732584193,this.#t=4023233417,this.#r=2562383102,this.#o=271733878,this.#n=new Uint8Array(b),this.#i=0,this.#s=0,this.#a=0}#p(r){let e=this.#s;e+=r,e>4294967295&&(this.#a+=1),this.#s=e>>>0}#c(r){let e=this.#e,o=this.#t,n=this.#r,s=this.#o,a=w=>rw|rw+1<<8|rw+2<<16|rw+3<<24,i=(w,Z)=>w<<Z|w>>>32-Z,p=a(0),f=a(4),m=a(8),d=a(12),k=a(16),h=a(20),P=a(24),I=a(28),v=a(32),C=a(36),N=a(40),M=a(44),U=a(48),S=a(52),_=a(56),$=a(60);e=o+i(((n^s)&o^s)+e+p+3614090360,7),s=e+i(((o^n)&e^n)+s+f+3905402710,12),n=s+i(((e^o)&s^o)+n+m+606105819,17),o=n+i(((s^e)&n^e)+o+d+3250441966,22),e=o+i(((n^s)&o^s)+e+k+4118548399,7),s=e+i(((o^n)&e^n)+s+h+1200080426,12),n=s+i(((e^o)&s^o)+n+P+2821735955,17),o=n+i(((s^e)&n^e)+o+I+4249261313,22),e=o+i(((n^s)&o^s)+e+v+1770035416,7),s=e+i(((o^n)&e^n)+s+C+2336552879,12),n=s+i(((e^o)&s^o)+n+N+4294925233,17),o=n+i(((s^e)&n^e)+o+M+2304563134,22),e=o+i(((n^s)&o^s)+e+U+1804603682,7),s=e+i(((o^n)&e^n)+s+S+4254626195,12),n=s+i(((e^o)&s^o)+n+_+2792965006,17),o=n+i(((s^e)&n^e)+o+$+1236535329,22),e=o+i(((o^n)&s^n)+e+f+4129170786,5),s=e+i(((e^o)&n^o)+s+P+3225465664,9),n=s+i(((s^e)&o^e)+n+M+643717713,14),o=n+i(((n^s)&e^s)+o+p+3921069994,20),e=o+i(((o^n)&s^n)+e+h+3593408605,5),s=e+i(((e^o)&n^o)+s+N+38016083,9),n=s+i(((s^e)&o^e)+n+$+3634488961,14),o=n+i(((n^s)&e^s)+o+k+3889429448,20),e=o+i(((o^n)&s^n)+e+C+568446438,5),s=e+i(((e^o)&n^o)+s+_+3275163606,9),n=s+i(((s^e)&o^e)+n+d+4107603335,14),o=n+i(((n^s)&e^s)+o+v+1163531501,20),e=o+i(((o^n)&s^n)+e+S+2850285829,5),s=e+i(((e^o)&n^o)+s+m+4243563512,9),n=s+i(((s^e)&o^e)+n+I+1735328473,14),o=n+i(((n^s)&e^s)+o+U+2368359562,20),e=o+i((o^n^s)+e+h+4294588738,4),s=e+i((e^o^n)+s+v+2272392833,11),n=s+i((s^e^o)+n+M+1839030562,16),o=n+i((n^s^e)+o+_+4259657740,23),e=o+i((o^n^s)+e+f+2763975236,4),s=e+i((e^o^n)+s+k+1272893353,11),n=s+i((s^e^o)+n+I+4139469664,16),o=n+i((n^s^e)+o+N+3200236656,23),e=o+i((o^n^s)+e+S+681279174,4),s=e+i((e^o^n)+s+p+3936430074,11),n=s+i((s^e^o)+n+d+3572445317,16),o=n+i((n^s^e)+o+P+76029189,23),e=o+i((o^n^s)+e+C+3654602809,4),s=e+i((e^o^n)+s+U+3873151461,11),n=s+i((s^e^o)+n+$+530742520,16),o=n+i((n^s^e)+o+m+3299628645,23),e=o+i((n^(o|~s))+e+p+4096336452,6),s=e+i((o^(e|~n))+s+I+1126891415,10),n=s+i((e^(s|~o))+n+_+2878612391,15),o=n+i((s^(n|~e))+o+h+4237533241,21),e=o+i((n^(o|~s))+e+U+1700485571,6),s=e+i((o^(e|~n))+s+d+2399980690,10),n=s+i((e^(s|~o))+n+N+4293915773,15),o=n+i((s^(n|~e))+o+f+2240044497,21),e=o+i((n^(o|~s))+e+v+1873313359,6),s=e+i((o^(e|~n))+s+$+4264355552,10),n=s+i((e^(s|~o))+n+P+2734768916,15),o=n+i((s^(n|~e))+o+S+1309151649,21),e=o+i((n^(o|~s))+e+k+4149444226,6),s=e+i((o^(e|~n))+s+M+3174756917,10),n=s+i((e^(s|~o))+n+m+718787259,15),o=n+i((s^(n|~e))+o+C+3951481745,21),this.#e=this.#e+e>>>0,this.#t=this.#t+o>>>0,this.#r=this.#r+n>>>0,this.#o=this.#o+s>>>0}update(r){let e;if(typeof r=="string")e=new TextEncoder().encode(r);else if(typeof r=="object")if(r instanceof ArrayBuffer||ArrayBuffer.isView(r))e=new Uint8Array(r);else throw new TypeError(Se);else throw new TypeError(Se);let o=this.#i,n=b-o;if(e.length<n)this.#n.set(e,o),o+=e.length;else{this.#n.set(e.slice(0,n),o),this.#c(this.#n);let s=n;for(;s+b<=e.length;)this.#c(e.slice(s,s+b)),s+=b;this.#n.fill(0).set(e.slice(s),0),o=e.length-s}return this.#i=o,this.#p(e.length),this}digest(){let r=b-this.#i;r<9&&(r+=b);let e=new Uint8Array(r);e0=128;let o=this.#s<<3,n=this.#a<<3|this.#s>>>29;ee.length-8=o&255,ee.length-7=o>>>8&255,ee.length-6=o>>>16&255,ee.length-5=o>>>24&255,ee.length-4=n&255,ee.length-3=n>>>8&255,ee.length-2=n>>>16&255,ee.length-1=n>>>24&255,this.update(e.buffer);let s=new ArrayBuffer(16),a=new DataView(s);return a.setUint32(0,this.#e,!0),a.setUint32(4,this.#t,!0),a.setUint32(8,this.#r,!0),a.setUint32(12,this.#o,!0),s}toString(r="hex"){let e=this.digest();switch(r){case"hex":return new TextDecoder().decode(Ue(new Uint8Array(e)));case"base64":{let o=new Uint8Array(e),n="";for(let s=0;s<o.length;++s)n+=String.fromCharCode(os);return btoa(n)}default:throw new Error("md5: invalid format")}}};var D="",_e=!1,$e=new Map,Oe=async(t,r,e)=>{if(t.hostname!=="pbs.twimg.com"||!t.pathname.startsWith("/media"))return;let o=$e.get(t.href);if(o)return o;if(_e){if(!D)return}else{let i=await Ie();if(_e=!0,!i.ok){alert("You haven't logged in Gyazo yet, so you can only upload images to scrapbox.io.");return}if(D=i.value||"",!D){alert("You haven't connect Gyazo to scrapbox.io yet.");return}}let n=await GM_fetch(t);if(!n.ok)return;let s=await me(await n.blob(),{accessToken:D,refererURL:r,description:e});if(!s.ok)throw Error(s.value.name);let a=new URL(s.value.permalink_url);return $e.set(t.href,a),a};var dt={"<":"<",">":">","&":"&",""":'"',"'":"'","`":""},J=t=>t.replace(/&(lt|gt|amp|quot|#x27|#x60);/g,r=>dt[r]);var F=t=>{let r={name:t.user.name,screenName:t.user.screen_name},e=new Date(t.created_at),o=[...t.entities.hashtags.map(a=>({type:"hashtag",...a})),...t.entities.symbols.map(a=>({type:"symbol",...a})),...t.entities.user_mentions.map(a=>({type:"mention",name:a.name,screenName:a.screen_name,indices:a.indices})),...t.entities.urls.map(a=>{let i={type:"url",indices:a.indices,url:new URL(a.expanded_url)};if(t.card&&t.card?.url===a.url){let{description:p,title:f}=t.card.binding_values,m="STRING";p?.type===m&&(i.description=p.string_value),f?.type===m&&(i.title=f.string_value)}return i}),...t.entities.media?.map?.(a=>({type:"media",indices:a.indices,media:t.mediaDetails?.flatMap?.(i=>i.url===a.url?[{type:i.type,url:new URL(i.video_info?.variants?.sort?.((p,f)=>(f.bitrate??0)-(p.bitrate??0))?.[0].url??i.media_url_https)}]:[])??[]}))??[]].sort((a,i)=>a.indices[0]-i.indices[0]),n=[];{let a=0,i=t.text;for(let{indices:p,...f}of o){let m=[...i].slice(0,p[0]-a).join("");n.push({type:"plain",text:J(m)}),n.push(f),i=[...i].slice(p[1]-a).join(""),a=p[1]}i&&n.push({type:"plain",text:J(i)})}let s={id:t.id_str,content:n,author:r,posted:e,replyCount:"reply_count"in t?t.reply_count:t.conversation_count};return t.self_thread&&(s.rootId=t.self_thread.id_str),t.in_reply_to_status_id_str&&(s.replyId=t.in_reply_to_status_id_str),t.parent&&(s.replyTo=F(t.parent)),t.quoted_tweet&&(s.quote=F(t.quoted_tweet)),s};var mt=["landing","product","enterprise","pricing","try-enterprise","contact","terms","privacy","jp-commercial-act","support","case","features","business","auth","login","logout","oauth2","_","api","app.html","assets","file","files","billing","billings","config","feed","index","io","new","opensearch","project","projects","search","setting","settings","setup-profile","slide","socket.io","stream","user","users"],H=(t=scrapbox.Project.name,r=location.host)=>e=>{if(e.host!==r)return e;let[,o,n]=e.pathname.match(/^\/([\w\d][\w\d-]{0,22}[\w\d])(?:\/?|\/(.+))$/)??[];return!o||mt.includes(o)?e:n?o===t?${decodeURIComponent(n)}:/${o}/${decodeURIComponent(n)}:/${o}};var je=(t=ut)=>r=>{let e=r.href.match(/^https:\/\/(?:www\.|mobile\.|m\.|)((vx|fx)?twitter|(fixup)?x)\.com\/[\w\d_]+\/(?:status|statuses)\/(\d+)/),o=e?e.slice(-1)[0]:void 0;return o?(async()=>{let n=await(pe(o)??Pe(r.href));if(!n.ok)throw n.value;return t(n.value,r)})():r},ut=async(t,r)=>{if("images"in t)return[${A(t.screenName)} ${r.origin}${r.pathname},...t.description.split( ).map(s=>> ${A(s)}),...t.images.length>0?[> ${t.images.map(s=>[${s}])}]:[]].join(
);let{quote:e,replyTo:o,...n}=F(t);return[...o?[...(await q(o)).map(s=> > ${s}),...o.quote?(await q(o.quote)).map(s=> > ${s}):[]]:[],...(await q(n)).map(s=>> ${s}),...e?(await q(e)).map(s=>> > ${s}):[]].join(
${s.join(`
`)}
}case"url":return${H()(n.url)} }}))).join("").split(
)]},Y=async(t,r)=>t.type==="photo"?${t.url}.endsWith(".svg")?${t.url}:${await Oe(t.url,r,"")??t.url}:[${t.url}${/\.(?:mp4|webm)$/.test(${t.url})?"":"#.mp4"}],A=t=>t.replace(/\b/gm,"").replace(/[\s\r\n\u2028\u2029]+/gm," ").replace(/\s*[[\]]\s*/g," ").trim();var Be=t=>{if(!window.GM_fetch)return;let r=window.GM_fetch;return(async()=>{let e=await r(https://t.co/${t}),o=L(e);if(o)return{ok:!1,value:o};let n=new DOMParser().parseFromString(await e.text(),"text/html");return{ok:!0,value:n.title}})()};var De=t=>{if("bit.ly","amzn.to","amzn.asia","goo.gl","s.nikkei.com","apple.co","nico.ms","w.wiki".includes(t.hostname)&&window.GM_fetch)return window.GM_fetch(t).then(e=>e.ok?new URL(e.url):t);if(t.hostname!=="t.co")return t;let r=Be(t.pathname.slice(1));return r?(async()=>{let e=await r;if(!e.ok)throw e.value;try{return new URL(e.value)}catch(o){if(o instanceof TypeError)return t;throw o}})():t};var Fe=t=>{if(!t.pathname.startsWith("/wiki/"))return t;if(!/^\w+\.wikipedia\.org$/.test(t.hostname)){let,n=t.hostname.match(/^(\w+)\.m\.wikipedia\.org$/)??[];if(!n)return t;t.hostname=${n}.wikipedia.org}let r=decodeURIComponent(t.pathname.slice(6)),e=t.hash?decodeURIComponent(t.hash.slice(1)):"",o=${t.origin}/wiki/${r};return e?[${e} | ${r} - Wikipedia ${o}#${e}]:[${r} - Wikipedia ${o}]};var He=t=>{if(t.hostname!=="www.wikiwand.com")return t;let,r,e=t.pathname.match(/^\/(^\/+)\/(^\/+)/)??[];return!r||!e||(t.hostname=${r}.wikipedia.org,t.pathname=/wiki/${e},t.hash=t.hash.startsWith("#/")?#${t.hash.slice(2)}:t.hash),t};var Q=t=>{if(!t.hostname.startsWith("www.google."))return t;let r=t.searchParams.get("url");return r?new URL(decodeURIComponent(r)):t};var qe=/charset=(^;+)/,Ae=t=>{if(!window.GM_fetch)return;let r=window.GM_fetch;return(async()=>{let e=await r(${t}),o=L(e);if(o)return{ok:!1,value:o};let n=e.headers.get("content-type")?.match?.(qe)?.1??await lt(e.clone()),s=new TextDecoder(n).decode(await e.arrayBuffer());return{ok:!0,value:s}})()},lt=async t=>{let r=new DOMParser().parseFromString(await t.text(),"text/html");return r.querySelector("metacharset")?.getAttribute?.("charset")??r.querySelector('metahttp-equiv="content-type"')?.getAttribute?.("content")?.match?.(qe)?.1??"utf-8"};var Ge=(t=ft)=>async r=>{let e=scrapbox.Project.name;if("work4ai","wogikaze-study", "mncore".includes(e)){if(r.host==="github.com"){let n=r.href.split("/").slice(-2);return[. ${r}][${n[0]}]/[${n[1]}]}else if(r.host==="huggingface.co"&&!r.href.includes("huggingface.co/papers")&&!r.href.includes("huggingface.co/blog")){let n=r.href.split("/").slice(-2);return[. ${r}][${n[0]}]/[${n[1]}]}}return t(await gt(r),r)},ft=(t,r)=>{let e=(g(t)?t:t.title).replace(/\s/g," ").replaceAll("","[").replaceAll("","]"),o=scrapbox.Project.name;return"work4ai","wogikaze-study", "mncore".includes(o)?r.host==="arxiv.org"?e?[. ${r.hash?${decodeURIComponent(r.hash.slice(1))} | :""}${r}][${e.replace(/\[[\d\.]+\]\s/,"")}]:${r}:r.host.includes("github.io")?e?[. ${r.hash?${decodeURIComponent(r.hash.slice(1))} | :""}${r}][${e}]:${r}:r.href.includes("huggingface.co/papers")?e?[. ${r.hash?${decodeURIComponent(r.hash.slice(1))} | :""}${r}][${e.replace(/Paper page -\s/,"")}]:${r}:e?[. ${r.hash?${decodeURIComponent(r.hash.slice(1))} | :""}${r}]${e.split("|")[0]}:${r}:e?[${r.hash?${decodeURIComponent(r.hash.slice(1))} | :""}${e} ${r}]:${r}},gt=async t=>{let r=Ae(t);if(!r){let o=await Re(t);if(!o.ok)throw o.value;return o.value}let e=await r;if(!e.ok)throw e.value;return new DOMParser().parseFromString(e.value,"text/html")};var ze=t=>{if(!/^(?:www\.)?amazon(?:\.co|com)?\.(?:au|br|ca|fr|de|in|it|jp|mx|nl|sg|es|tr|ae|uk|cn)$/.test(t.hostname))return t;let,r=t.pathname.match(/\/dp\/(\w\d+)/)??t.pathname.match(/\/gp\/product\/(\w\d+)/)??t.pathname.match(/\/exec\/obidos\/asin\/(\w\d+)/)??t.pathname.match(/\/o\/ASIN\/(\w\d+)/)??[];return r&&(t.hash="",t.search="",t.pathname=/dp/${r}),t};var Ke=t=>{if(!/(?:0-9a-z-\.)?gyazo\.com/.test(t.hostname))return t;let,r=t.pathname.match(/^\/(0-9a-f{32})(?:\/raw)?$/)??[];return r?[https://gyazo.com/${r}]:t};var We=(t,...r)=>{if(t instanceof URL)return z(new URL(t),...r);let e=0,o=0,n=0,s=!1,a=t.split(/(https?:\/\/\S+)/g).map(m=>{if(!/^https?:\/\/\S+$/.test(m))return m;e++;try{let d=z(new URL(m),...r);return g(d)?(o++,d):(s=!0,d,m)}catch(d){return console.error(d),n++,m}});if(!s)return a.join("");let{render:i,dispose:p}=ae(),f=()=>i({type:"spinner"},{type:"text",text:URL: ${o}/${e} converted, ${n} failed});return f(),Promise.all(a.map(async m=>{if(g(m))return m;try{let d=await m0;return o++,d}catch(d){return console.error(d),n++,m1}finally{f()}})).then(m=>(i({type:"check-circle"},{type:"text",text:URL: ${o}/${e} converted, ${n} failed}),m.join(""))).finally(()=>{setTimeout(p,1e3)})};var ht=Q,De,Q,He,ze,H(),Ke,je(),Fe,Ge();scrapbox.PopupMenu.addButton({title:t=>/https?:\/\/\S+/.test(t)?"URL":"",onClick:t=>{let r=We(t,...ht);if(typeof r=="string")return t===r?void 0:r;r.then(e=>{if(t!==e)return ie(e)})}});