generated at
mobileでURLを外部リンク記法に変換するPage menu

mod.js
var l=(e,t)=>{if(!(e instanceof HTMLDivElement))throw new TypeError(`"${t}" must be HTMLDivElememt but actual is "${e}"`)};var m=()=>h(document.getElementsByClassName("status-bar")?.[0],"div.status-bar"),h=(e,t)=>{if(!!e)return l(e,t),e};function p(){let e=m();if(!e)throw new Error("div.status-bar can't be found");let t=document.createElement("div");return e.append(t),{render:(...r)=>{t.textContent="";let n=g(...r);n&&t.append(n)},dispose:()=>t.remove()}}function g(...e){let t=e.flatMap(n=>{switch(n.type){case"spinner":return[b()];case"check-circle":return[M()];case"exclamation-triangle":return[H()];case"text":return[s(n.text)];case"group":{let o=g(...n.items);return o?[o]:[]}}});if(t.length===0)return;if(t.length===1)return t[0];let r=document.createElement("span");return r.classList.add("item-group"),r.append(...t),r}function s(e){let t=document.createElement("span");return t.classList.add("item"),t.append(e),t}function b(){let e=document.createElement("i");return e.classList.add("fa","fa-spinner"),s(e)}function M(){let e=document.createElement("i");return e.classList.add("kamon","kamon-check-circle"),s(e)}function H(){let e=document.createElement("i");return e.classList.add("fas","fa-exclamation-triangle"),s(e)}var Je={title:"convert URL",onClick:e=>k(e)};async function k(e){let{render:t,dispose:r}=p();t({type:"spinner"},{type:"text",text:"convert URLs..."});let n=0;try{let o=await Promise.all(e.split(/(https?:\/\/\S+)/).map(async i=>{if(!/^https?:\/\/\S+/.test(i))return i;let c;try{c=new URL(i)}catch(a){if(a instanceof TypeError)return i;throw a}let u=await C(c);if(!u.ok)return i;n++;let x=u.title.replace(/\s/g," ").replaceAll("[","[").replaceAll("]","]");return`[${c} ${x}]`}));return t({type:"check-circle"},{type:"text",text:`Converted ${n} URLs.`}),o.join("")}catch(o){t({type:"exclamation-triangle"},{type:"text",text:o instanceof Error?`${o.name} ${o.message}`:"Unknown error! (see developper console)"}),console.error(o)}finally{setTimeout(()=>r(),1e3)}}async function C(e){let t=`https://scrapbox.io/api/embed-text/url?url=${e.toString()}`,r=await fetch(t,{method:"POST",headers:{"X-CSRF-TOKEN":window._csrf},body:JSON.stringify({timeout:3e3})});if(!r.ok)return{ok:!1};let{title:n}=await r.json();return{ok:!0,title:n}}export{Je as urlConverter};