scrapbox-url-customizer
code: script.js
var _e=t=>typeof t=="object"&&t!==null,je=t=>_e(t)?(t.name===void 0||typeof t.name=="string")&&typeof t.message=="string":!1,X=t=>{try{let o=typeof t=="string"?JSON.parse(t):t;return je(o)?o:!1}catch(o){if(o instanceof SyntaxError)return!1;throw o}};var Y=!1;scrapbox.addListener("lines:changed",()=>Y=!1);scrapbox.addListener("layout:changed",()=>Y=!1);var Q=(t,o)=>{if(!(t instanceof HTMLDivElement))throw new TypeError("${o}" must be HTMLDivElememt but actual is "${t}")};var J=(t,o)=>{if(!(t instanceof HTMLTextAreaElement))throw new TypeError("${o}" must be HTMLTextAreaElement but actual is "${t}")};var L=()=>{let t=document.getElementById("text-input");if(!!t)return J(t,"textarea#text-input"),t};var Z=()=>$e(document.getElementsByClassName("status-bar")?.0,"div.status-bar"),$e=(t,o)=>{if(!!t)return Q(t,o),t};var F=t=>new Promise(o=>setTimeout(()=>o(),t));var te=async t=>{let o=L();if(!o)throw Error("#text-input is not ditected.");o.focus(),o.value=t;let e=new InputEvent("input",{bubbles:!0});o.dispatchEvent(e),await F(1)};var re=()=>{let t=Z();if(!t)throw new Error("div.status-bar can't be found");let o=document.createElement("div");return t.append(o),{render:(...e)=>{o.textContent="";let r=ne(...e);r&&o.append(r)},dispose:()=>o.remove()}},ne=(...t)=>{let o=t.flatMap(r=>{switch(r.type){case"spinner":returnqe();case"check-circle":returnAe();case"exclamation-triangle":returnGe();case"text":return$(r.text);case"group":{let n=ne(...r.items);return n?n:[]}}});if(o.length===0)return;if(o.length===1)return o0;let e=document.createElement("span");return e.classList.add("item-group"),e.append(...o),e},$=t=>{let o=document.createElement("span");return o.classList.add("item"),o.append(t),o},qe=()=>{let t=document.createElement("i");return t.classList.add("fa","fa-spinner"),$(t)},Ae=()=>{let t=document.createElement("i");return t.classList.add("kamon","kamon-check-circle"),$(t)},Ge=()=>{let t=document.createElement("i");return t.classList.add("fas","fa-exclamation-triangle"),$(t)};var k=t=>...t.map((o,e)=>o===" "?"_":!Ke.includes(o)||e===t.length-1&&ze.includes(o)?encodeURIComponent(o):o).join(""),Ke='@$&+=:;",',ze=':;",';var g=t=>typeof t=="string";var H=(t,...o)=>{let e=t;for(let r of o){let n=e instanceof Promise?e.then(s=>g(s)?s:r(s)):r(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(r=>${r}):${t}};var E=t=>t.ok?void 0:{name:"HTTPError",message:${t.status} ${t.statusText},response:t};var oe=t=>{if(!window.GM_fetch)return;let o=window.GM_fetch;return(async()=>{let e=await o(https://cdn.syndication.twimg.com/tweet-result?id=${t}),r=E(e);if(r)return{ok:!1,value:r};let n=await e.json();return{ok:!0,value:n}})()};var p=t=>{let{fetch:o=globalThis.fetch,hostName:e="scrapbox.io",...r}=t;return{fetch:o,hostName:e,...r}};var w=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,w)}},f=async t=>{let o=t.clone(),e=await o.text(),r=X(e);if(!r)throw new w(o);return{ok:!1,value:r}};var q=async t=>{let{sid:o,hostName:e,fetch:r}=p(t??{}),n=new Request(https://${e}/api/users/me,o?{headers:{Cookie:l(o)}}:void 0),s=await r(n);if(!s.ok)throw new w(s);return await s.json()};var l=t=>connect.sid=${t},y=async t=>window._csrf?window._csrf:(await q(t)).csrfToken;var se=(t,o,e)=>{let{sid:r,hostName:n,followRename:s,projects:a}=p(e??{}),i=new URLSearchParams;i.append("followRename",${s??!0});for(let m of a??[])i.append("projects",m);let c=https://${n}/api/pages/${t}/${k(o)}?${i.toString()};return new Request(c,r?{headers:{Cookie:l(r)}}:void 0)},ie=async t=>{if(!t.ok)return t.status===414?{ok:!1,value:{name:"TooLongURIError",message:"project ids may be too much."}}:f(t);let o=await t.json();return{ok:!0,value:o}},A=async(t,o,e)=>{let{fetch:r}=p(e??{}),n=se(t,o,e),s=await r(n);return await ie(s)};A.toRequest=se;A.fromResponse=ie;var ae=(t,o)=>{let{sid:e,hostName:r,sort:n,limit:s,skip:a}=p(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 c=https://${r}/api/pages/${t}?${i.toString()};return new Request(c,e?{headers:{Cookie:l(e)}}:void 0)},ce=async t=>{if(!t.ok)return f(t);let o=await t.json();return{ok:!0,value:o}},pe=async(t,o)=>{let{fetch:e}=p(o??{}),r=await e(ae(t,o));return await ce(r)};pe.toRequest=ae;pe.fromResponse=ce;var de=(t,o,e,r)=>{let{sid:n,hostName:s}=p(r??{}),a=https://${s}/api/table/${t}/${k(o)}/${encodeURIComponent(e)}.csv;return new Request(a,n?{headers:{Cookie:l(n)}}:void 0)},me=async t=>t.ok?{ok:!0,value:await t.text()}:t.status===404?{ok:!1,value:{name:"NotFoundError",message:"Table not found."}}:f(t),le=async(t,o,e,r)=>{let{fetch:n}=p(r??{}),s=de(t,o,e,r),a=await n(s);return await me(a)};le.toRequest=de;le.fromResponse=me;var ue=(t,o)=>{let{sid:e,hostName:r}=p(o??{});return new Request(https://${r}/api/projects/${t},e?{headers:{Cookie:l(e)}}:void 0)},fe=async t=>{if(!t.ok)return f(t);let o=await t.json();return{ok:!0,value:o}},G=async(t,o)=>{let{fetch:e}=p(o??{}),r=ue(t,o),n=await e(r);return fe(n)};G.toRequest=ue;G.fromResponse=fe;var ge=(t,o)=>{let{sid:e,hostName:r}=p(o??{}),n=new URLSearchParams;for(let s of t)n.append("ids",s);return new Request(https://${r}/api/projects?${n.toString()},e?{headers:{Cookie:l(e)}}:void 0)},xe=async t=>{if(!t.ok)return f(t);let o=await t.json();return{ok:!0,value:o}},he=async(t,o)=>{let{fetch:e}=p(o??{}),r=await e(ge(t,o));return xe(r)};he.toRequest=ge;he.fromResponse=xe;var we=async(t,o)=>{let{sid:e,hostName:r,fetch:n,csrf:s}=p(o??{}),a=new Request(https://${r}/api/embed-text/url?url=${encodeURIComponent(t.toString())},{method:"POST",headers:{"Content-Type":"application/json;charset=utf-8","X-CSRF-TOKEN":s??await y(o),...e?{Cookie:l(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}}:f(i);let{title:c}=await i.json();return{ok:!0,value:c}};var ye=async(t,o)=>{let{sid:e,hostName:r,fetch:n,csrf:s}=p(o??{}),a=new Request(https://${r}/api/embed-text/twitter?url=${encodeURIComponent(t.toString())},{method:"POST",headers:{"Content-Type":"application/json;charset=utf-8","X-CSRF-TOKEN":s??await y(o),...e?{Cookie:l(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}}:f(i);let c=await i.json();return{ok:!0,value:c}};var Qo=new Map;var be=new TextEncoder().encode("0123456789abcdef");function Ee(t){let o=new Uint8Array(t.length*2);for(let e=0;e<o.length;e++){let r=te;oe*2=ber>>4,oe*2+1=ber&15}return o}var Le="md5: data is invalid type",b=64,K=class{#e;#t;#r;#n;#o;#i;#s;#a;constructor(){this.#e=1732584193,this.#t=4023233417,this.#r=2562383102,this.#n=271733878,this.#o=new Uint8Array(b),this.#i=0,this.#s=0,this.#a=0}#p(o){let e=this.#s;e+=o,e>4294967295&&(this.#a+=1),this.#s=e>>>0}#c(o){let e=this.#e,r=this.#t,n=this.#r,s=this.#n,a=h=>oh|oh+1<<8|oh+2<<16|oh+3<<24,i=(h,V)=>h<<V|h>>>32-V,c=a(0),m=a(4),d=a(8),u=a(12),x=a(16),R=a(20),v=a(24),P=a(28),I=a(32),C=a(36),N=a(40),M=a(44),S=a(48),U=a(52),_=a(56),j=a(60);e=r+i(((n^s)&r^s)+e+c+3614090360,7),s=e+i(((r^n)&e^n)+s+m+3905402710,12),n=s+i(((e^r)&s^r)+n+d+606105819,17),r=n+i(((s^e)&n^e)+r+u+3250441966,22),e=r+i(((n^s)&r^s)+e+x+4118548399,7),s=e+i(((r^n)&e^n)+s+R+1200080426,12),n=s+i(((e^r)&s^r)+n+v+2821735955,17),r=n+i(((s^e)&n^e)+r+P+4249261313,22),e=r+i(((n^s)&r^s)+e+I+1770035416,7),s=e+i(((r^n)&e^n)+s+C+2336552879,12),n=s+i(((e^r)&s^r)+n+N+4294925233,17),r=n+i(((s^e)&n^e)+r+M+2304563134,22),e=r+i(((n^s)&r^s)+e+S+1804603682,7),s=e+i(((r^n)&e^n)+s+U+4254626195,12),n=s+i(((e^r)&s^r)+n+_+2792965006,17),r=n+i(((s^e)&n^e)+r+j+1236535329,22),e=r+i(((r^n)&s^n)+e+m+4129170786,5),s=e+i(((e^r)&n^r)+s+v+3225465664,9),n=s+i(((s^e)&r^e)+n+M+643717713,14),r=n+i(((n^s)&e^s)+r+c+3921069994,20),e=r+i(((r^n)&s^n)+e+R+3593408605,5),s=e+i(((e^r)&n^r)+s+N+38016083,9),n=s+i(((s^e)&r^e)+n+j+3634488961,14),r=n+i(((n^s)&e^s)+r+x+3889429448,20),e=r+i(((r^n)&s^n)+e+C+568446438,5),s=e+i(((e^r)&n^r)+s+_+3275163606,9),n=s+i(((s^e)&r^e)+n+u+4107603335,14),r=n+i(((n^s)&e^s)+r+I+1163531501,20),e=r+i(((r^n)&s^n)+e+U+2850285829,5),s=e+i(((e^r)&n^r)+s+d+4243563512,9),n=s+i(((s^e)&r^e)+n+P+1735328473,14),r=n+i(((n^s)&e^s)+r+S+2368359562,20),e=r+i((r^n^s)+e+R+4294588738,4),s=e+i((e^r^n)+s+I+2272392833,11),n=s+i((s^e^r)+n+M+1839030562,16),r=n+i((n^s^e)+r+_+4259657740,23),e=r+i((r^n^s)+e+m+2763975236,4),s=e+i((e^r^n)+s+x+1272893353,11),n=s+i((s^e^r)+n+P+4139469664,16),r=n+i((n^s^e)+r+N+3200236656,23),e=r+i((r^n^s)+e+U+681279174,4),s=e+i((e^r^n)+s+c+3936430074,11),n=s+i((s^e^r)+n+u+3572445317,16),r=n+i((n^s^e)+r+v+76029189,23),e=r+i((r^n^s)+e+C+3654602809,4),s=e+i((e^r^n)+s+S+3873151461,11),n=s+i((s^e^r)+n+j+530742520,16),r=n+i((n^s^e)+r+d+3299628645,23),e=r+i((n^(r|~s))+e+c+4096336452,6),s=e+i((r^(e|~n))+s+P+1126891415,10),n=s+i((e^(s|~r))+n+_+2878612391,15),r=n+i((s^(n|~e))+r+R+4237533241,21),e=r+i((n^(r|~s))+e+S+1700485571,6),s=e+i((r^(e|~n))+s+u+2399980690,10),n=s+i((e^(s|~r))+n+N+4293915773,15),r=n+i((s^(n|~e))+r+m+2240044497,21),e=r+i((n^(r|~s))+e+I+1873313359,6),s=e+i((r^(e|~n))+s+j+4264355552,10),n=s+i((e^(s|~r))+n+v+2734768916,15),r=n+i((s^(n|~e))+r+U+1309151649,21),e=r+i((n^(r|~s))+e+x+4149444226,6),s=e+i((r^(e|~n))+s+M+3174756917,10),n=s+i((e^(s|~r))+n+d+718787259,15),r=n+i((s^(n|~e))+r+C+3951481745,21),this.#e=this.#e+e>>>0,this.#t=this.#t+r>>>0,this.#r=this.#r+n>>>0,this.#n=this.#n+s>>>0}update(o){let e;if(typeof o=="string")e=new TextEncoder().encode(o);else if(typeof o=="object")if(o instanceof ArrayBuffer||ArrayBuffer.isView(o))e=new Uint8Array(o);else throw new TypeError(Le);else throw new TypeError(Le);let r=this.#i,n=b-r;if(e.length<n)this.#o.set(e,r),r+=e.length;else{this.#o.set(e.slice(0,n),r),this.#c(this.#o);let s=n;for(;s+b<=e.length;)this.#c(e.slice(s,s+b)),s+=b;this.#o.fill(0).set(e.slice(s),0),r=e.length-s}return this.#i=r,this.#p(e.length),this}digest(){let o=b-this.#i;o<9&&(o+=b);let e=new Uint8Array(o);e0=128;let r=this.#s<<3,n=this.#a<<3|this.#s>>>29;ee.length-8=r&255,ee.length-7=r>>>8&255,ee.length-6=r>>>16&255,ee.length-5=r>>>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.#n,!0),s}toString(o="hex"){let e=this.digest();switch(o){case"hex":return new TextDecoder().decode(Ee(new Uint8Array(e)));case"base64":{let r=new Uint8Array(e),n="";for(let s=0;s<r.length;++s)n+=String.fromCharCode(rs);return btoa(n)}default:throw new Error("md5: invalid format")}}};var O=t=>{let o={name:t.user.name,screenName:t.user.screen_name},e=new Date(t.created_at),r=[...t.entities.hashtags.map(c=>({type:"hashtag",...c})),...t.entities.symbols.map(c=>({type:"symbol",...c})),...t.entities.user_mentions.map(c=>({type:"mention",name:c.name,screenName:c.screen_name,indices:c.indices})),...t.entities.urls.map(c=>{let m={type:"url",indices:c.indices,url:new URL(c.expanded_url)};if(t.card&&t.card?.url===c.url){let{description:d,title:u}=t.card.binding_values,x="STRING";d?.type===x&&(m.description=d.string_value),u?.type===x&&(m.title=u.string_value)}return m}),...t.entities.media?.map?.(c=>({type:"media",indices:c.indices,media:t.mediaDetails?.flatMap?.(m=>m.url===c.url?[{type:m.type,url:new URL(m.video_info?.variants?.sort?.((d,u)=>u.bitrate-d.bitrate)?.0.url??m.media_url_https)}]:[])??[]}))??[]].sort((c,m)=>c.indices0-m.indices0),n=0,s=t.text,a=[];for(let{indices:c,...m}of r){let d=s.slice(0,c0-n);a.push({type:"plain",text:d}),a.push(m),s=s.slice(c1-n),n=c1}s&&a.push({type:"plain",text:s});let i={id:t.id_str,content:a,author:o,posted:e,replyCount:"reply_count"in t?t.reply_count:t.conversation_count};return t.self_thread&&(i.rootId=t.self_thread.id_str),t.in_reply_to_status_id_str&&(i.replyId=t.in_reply_to_status_id_str),t.parent&&(i.replyTo=O(t.parent)),t.quoted_tweet&&(i.quote=O(t.quoted_tweet)),i};var Ye="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",B=(t=scrapbox.Project.name,o=location.host)=>e=>{if(e.host!==o)return e;let,r,n=e.pathname.match(/^\/(\w\d\w\d-{0,22}\w\d)(?:\/?|\/(.+))$/)??[];return!r||Ye.includes(r)?e:n?r===t?[${decodeURIComponent(n)}]:[/${r}/${decodeURIComponent(n)}]:[/${r}]};var ke=(t=Qe)=>o=>{let,e=o.href.match(/^https:\/\/(?:www\.|mobile\.|m\.|)twitter\.com\/\w\d_+\/(?:status|statuses)\/(\d+)/)??[];return e?(async()=>{let r=await(oe(e)??ye(o.href));if(!r.ok)throw r.value;return t(r.value,o)})():o},Qe=(t,o)=>{if("images"in t)return[[${T(t.screenName)} ${o.origin}${o.pathname}],...t.description.split(` ).map(s=>> ${T(s)}),...t.images.length>0?[> ${t.images.map(s=>[${s}])}]:[]].join(
);let{quote:e,replyTo:r,...n}=O(t);return[...r?[...D(r).map(s=> > ${s}),...r.quote?D(r.quote).map(s=> > ${s}):[]]:[],...D(n).map(s=>> ${s}),...e?D(e).map(s=>> > ${s}):[]].join(
${s.join(`
`)}
}case"url":return${B()(n.url)} }}).join("").split(
)]},z=t=>[${t.url}${t.type==="photo"?/\.(?:png|jpe?g|gif|svg)$/.test(${t.url})?"":"#.jpg":/\.(?:mp4|webm)$/.test(${t.url})?"":"#.mp4"}],T=t=>t.replace(/\b/gm,"").replace(/[\s\r\n\u2028\u2029]+/gm," ").replace(/\s*[[\]]\s*/g," ").trim();var Te=t=>{if(!window.GM_fetch)return;let o=window.GM_fetch;return(async()=>{let e=await o(https://t.co/${t}),r=E(e);if(r)return{ok:!1,value:r};let n=new DOMParser().parseFromString(await e.text(),"text/html");return{ok:!0,value:n.title}})()};var Re=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 o=Te(t.pathname.slice(1));return o?(async()=>{let e=await o;if(!e.ok)throw e.value;try{return new URL(e.value)}catch(r){if(r instanceof TypeError)return t;throw r}})():t};var ve=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 o=decodeURIComponent(t.pathname.slice(6)),e=t.hash?decodeURIComponent(t.hash.slice(1)):"",r=${t.origin}/wiki/${o};return e?[${e} | ${o} - Wikipedia ${r}#${e}]:[${o} - Wikipedia ${r}]};var Pe=t=>{if(t.hostname!=="www.wikiwand.com")return t;let,o,e=t.pathname.match(/^\/(^\/+)\/(^\/+)/)??[];return!o||!e||(t.hostname=${o}.wikipedia.org,t.pathname=/wiki/${e},t.hash=t.hash.startsWith("#/")?#${t.hash.slice(2)}:t.hash),t};var W=t=>{if(!t.hostname.startsWith("www.google."))return t;let o=t.searchParams.get("url");return o?new URL(decodeURIComponent(o)):t};var Ie=/charset=(^;+)/,Ce=t=>{if(!window.GM_fetch)return;let o=window.GM_fetch;return(async()=>{let e=await o(${t}),r=E(e);if(r)return{ok:!1,value:r};let n=e.headers.get("content-type")?.match?.(Ie)?.1??await Je(e.clone()),s=new TextDecoder(n).decode(await e.arrayBuffer());return{ok:!0,value:s}})()},Je=async t=>{let o=new DOMParser().parseFromString(await t.text(),"text/html");return o.querySelector("metacharset")?.getAttribute?.("charset")??o.querySelector('metahttp-equiv="content-type"')?.getAttribute?.("content")?.match?.(Ie)?.1??"utf-8"};var Ne=(t=Ze)=>async o=>t(await et(o),o),Ze=(t,o)=>{let e=(g(t)?t:t.title).replace(/\s/g," ").replaceAll("","[").replaceAll("","]");return e?[${o.hash?${decodeURIComponent(o.hash.slice(1))} | :""}${e} ${o}]:${o}},et=async t=>{let o=Ce(t);if(!o){let r=await we(t);if(!r.ok)throw r.value;return r.value}let e=await o;if(!e.ok)throw e.value;return new DOMParser().parseFromString(e.value,"text/html")};var Me=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,o=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 o&&(t.hash="",t.search="",t.pathname=/dp/${o}),t};var Se=t=>{if(!/(?:0-9a-z-\.)?gyazo\.com/.test(t.hostname))return t;let,o=t.pathname.match(/^\/(0-9a-f{32})(?:\/raw)?$/)??[];return o?[https://gyazo.com/${o}]:t};var Ue=(t,...o)=>{if(t instanceof URL)return H(new URL(t),...o);let e=0,r=0,n=0,s=!1,a=t.split(/(https?:\/\/\S+)/g).map(d=>{if(!/^https?:\/\/\S+$/.test(d))return d;e++;try{let u=H(new URL(d),...o);return g(u)?(r++,u):(s=!0,u,d)}catch(u){return console.error(u),n++,d}});if(!s)return a.join("");let{render:i,dispose:c}=re(),m=()=>i({type:"spinner"},{type:"text",text:URL: ${r}/${e} converted, ${n} failed});return m(),Promise.all(a.map(async d=>{if(g(d))return d;try{let u=await d0;return r++,u}catch(u){return console.error(u),n++,d1}finally{m()}})).then(d=>(i({type:"check-circle"},{type:"text",text:URL: ${r}/${e} converted, ${n} failed}),d.join(""))).finally(()=>{setTimeout(c,1e3)})};var tt=W,Re,W,Pe,Me,B(),Se,ke(),ve,Ne();scrapbox.PopupMenu.addButton({title:t=>/https?:\/\/\S+/.test(t)?"URL":"",onClick:t=>{let o=Ue(t,...tt);if(typeof o=="string")return t===o?void 0:o;o.then(e=>{if(t!==e)return te(e)})}});