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