yozbaUserScripts
https://gyazo.com/451b1c93580dccf303c4b9f9af518d38
code:script.js
code:diary-template.js
/* MIT License Copyright (c) 2020 ci7lus */
export const importExternalJs = async (url) =>
new Promise((res, rej) => {
if (document.querySelector(script[src="${url}"])) res()
const script = document.createElement("script")
script.src = url
script.addEventListener("load", res)
script.addEventListener("error", rej)
document.body.appendChild(script)
})
importExternalJs(
);
export function insertText({text}) {
const cursor = document.getElementById('text-input');
cursor.focus();
const start = cursor.selectionStart; // in this case maybe 0
cursor.setRangeText(text);
cursor.selectionStart = cursor.selectionEnd = start + text.length;
const uiEvent = document.createEvent('UIEvent');
uiEvent.initEvent('input', true, false);
cursor.dispatchEvent(uiEvent);
};
scrapbox.PageMenu.addMenu({
title: DailyReport,
onClick: () => {
if (!scrapbox.Page.lines || !scrapbox.Page.lines.length == 1) return
const input = prompt(
"日報テンプレートを展開したい日付を相対(d+)または絶対(2020-1-1)で(入力なしで今日)"
)
if (input === null) return
const diff = parseInt(input.trim() || 0)
const abs = input.split("-").length === 3 && dayjs(input)
if ((Number.isNaN(diff) && !abs) || (abs && !abs.isValid())) return
const today = abs
? abs.startOf("days")
: dayjs().startOf("days").add(diff, "days")
const yesterday = today.clone().subtract(1, "days")
const tomorrow = today.clone().add(1, "days")
console.log(today.format(), yesterday.format(), tomorrow.format())
const conf = confirm(対象の日付は ${today.format("YYYY.MM.DD")} ですか?)
if (!conf) return
insertText({
text: ${today.format("YYYY.MM.DD")}\n<- [${tomorrow.format("YYYY.MM.DD")}] / [${today.format("YYYY.MM")}] / [${yesterday.format("YYYY.MM.DD")}] ->\n\n\n\n<- [${tomorrow.format("YYYY.MM.DD")}] / [${today.format("YYYY.MM")}] / [${yesterday.format("YYYY.MM.DD")}] ->,
})
},
})
code:alt-t-format.js
scrapbox.TimeStamp.removeAllFormats()
scrapbox.TimeStamp.addFormat("HH:mm")
scrapbox.TimeStamp.addFormat("YYYY.MM.DD")
行頭に引用符をつける
code:quote.js
scrapbox.PopupMenu.addButton({
title: 'quote',
onClick: text => text.split(/\n/).map(line => >${line}).join('\n')
})
code:gmfetch.js
var u=n=>new Headers(n.replace(/\r?\n\t +/g," ").split(/\r\n|\r|\n/).flatMap(e=>{lets,o=e.split(":").map(t=>t.trim());return s?s,o:[]})),l=(n,e)=>new Promise((s,o)=>{let t=Object.fromEntries(new Headers(n instanceof Request?n.headers:e?.headers).entries());n instanceof Request&&(t.Referer=n.referrer,t"Referrer-Policy"=n.referrerPolicy),e?.referrer&&(t.Referer=e.referrer),e?.referrerPolicy&&(t"Referrer-Policy"=e.referrerPolicy);let r=new Request(n,e);if(r.signal?.aborted){o(new DOMException("Aborted","AbortError"));return}let{abort:a}=GM_xmlhttpRequest({method:r.method,url:r.url,headers:t,...e?.body?{data:e.body}:{},anonymous:r.credentials==="omit",responseType:"stream",fetch:!0,onerror:()=>{o(new TypeError("Network request failed"))},ontimeout:()=>{o(new TypeError("Network request timeout"))},onabort:()=>{o(new DOMException("Aborted","AbortError"))},onreadystatechange:i=>{switch(i.readyState){case 2:{let d=new Response(i.response,{status:i.status,statusText:i.statusText,headers:u(i.responseHeaders)});Object.defineProperty(d,"url",{value:i.finalUrl}),s(d);break}case 4:r.signal?.removeEventListener?.("abort",a);break;default:break}}});r.signal?.addEventListener?.("abort",a)});unsafeWindow.GM_fetch=l; code:scrapbox-url-customizer.js
var Je=e=>typeof e=="object"&&e!==null,Ze=e=>Je(e)?(e.name===void 0||typeof e.name=="string")&&typeof e.message=="string":!1,te=e=>{try{let n=typeof e=="string"?JSON.parse(e):e;return Ze(n)?n:!1}catch(n){if(n instanceof SyntaxError)return!1;throw n}};var re=!1;scrapbox.addListener("lines:changed",()=>re=!1);scrapbox.addListener("layout:changed",()=>re=!1);var ne=(e,n)=>{if(!(e instanceof HTMLDivElement))throw new TypeError("${n}" must be HTMLDivElememt but actual is "${e}")};var oe=(e,n)=>{if(!(e instanceof HTMLTextAreaElement))throw new TypeError("${n}" must be HTMLTextAreaElement but actual is "${e}")};var k=()=>{let e=document.getElementById("text-input");if(!!e)return oe(e,"textarea#text-input"),e};var se=()=>et(document.getElementsByClassName("status-bar")?.0,"div.status-bar"),et=(e,n)=>{if(!!e)return ne(e,n),e};var W=e=>new Promise(n=>setTimeout(()=>n(),e));var ae=async e=>{let n=k();if(!n)throw Error("#text-input is not ditected.");n.focus(),n.value=e;let t=new InputEvent("input",{bubbles:!0});n.dispatchEvent(t),await W(1)};var ce=()=>{let e=se();if(!e)throw new Error("div.status-bar can't be found");let n=document.createElement("div");return e.append(n),{render:(...t)=>{n.textContent="";let r=pe(...t);r&&n.append(r)},dispose:()=>n.remove()}},pe=(...e)=>{let n=e.flatMap(r=>{switch(r.type){case"spinner":returnit();case"check-circle":returnat();case"exclamation-triangle":returnct();case"text":return$(r.text);case"group":{let o=pe(...r.items);return o?o:[]}}});if(n.length===0)return;if(n.length===1)return n0;let t=document.createElement("span");return t.classList.add("item-group"),t.append(...n),t},$=e=>{let n=document.createElement("span");return n.classList.add("item"),n.append(e),n},it=()=>{let e=document.createElement("i");return e.classList.add("fa","fa-spinner"),$(e)},at=()=>{let e=document.createElement("i");return e.classList.add("kamon","kamon-check-circle"),$(e)},ct=()=>{let e=document.createElement("i");return e.classList.add("fas","fa-exclamation-triangle"),$(e)};var y=e=>...e.map((n,t)=>n===" "?"_":!pt.includes(n)||t===e.length-1&&dt.includes(n)?encodeURIComponent(n):n).join(""),pt='@$&+=:;",',dt=':;",';var de=e=>{if(e===0)return;let n={};return e&1&&(n.capture=!0),e&2&&(n.once=!0),e&4&&(n.passive=!0),n};var lt=new Map,tn=new Map;scrapbox.on("layout:changed",()=>{let e=k();if(!!e)for(letn,tof lt)for(letr,oof t)for(let s of o)e.addEventListener(n,r,de(s))});var E=e=>typeof e=="string";var V=(e,...n)=>{let t=e;for(let r of n){let o=t instanceof Promise?t.then(s=>E(s)?s:r(s)):r(t);if(E(o))return o;t=o instanceof URL?new URL(o):o.then(s=>E(s)?s:new URL(s))}return t instanceof Promise?t.then(r=>${r}):${e}};var R=e=>e.ok?void 0:{name:"HTTPError",message:${e.status} ${e.statusText},response:e};var ue=e=>{if(!window.GM_fetch)return;let n=window.GM_fetch;return(async()=>{let t=await n(https://cdn.syndication.twimg.com/tweet-result?id=${e}&token=x),r=R(t);if(r)return{ok:!1,value:r};let o=await t.json();return{ok:!0,value:o}})()};var j=e=>{let{fetch:n=globalThis.fetch,...t}=e;return{fetch:n,...t}},le=e=>typeof e=="object"&&e!==null;var P=class extends Error{constructor(n){super(${n.status} ${n.statusText} when fetching ${n.path.toString()});this.name="UnexpectedResponseError";this.status=n.status,this.statusText=n.statusText,this.body=n.body,this.path=n.path,Error.captureStackTrace&&Error.captureStackTrace(this,P)}};var F=async e=>{let n=await e.text();if(e.ok)return{ok:!0,value:n};if(e.status===400)return{ok:!1,value:{name:"BadRequestError",message:n}};try{let t=JSON.parse(n);if(!le(t)||typeof t.message!="string")throw new P({status:e.status,statusText:e.statusText,body:n,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 P({status:e.status,statusText:e.statusText,body:n,path:new URL(e.url)})}}catch(t){throw t instanceof SyntaxError?new P({status:e.status,statusText:e.statusText,body:n,path:new URL(e.url)}):t}};var me=async(e,n)=>{let{title:t,description:r,metadataIsPublic:o,collectionId:s,refererURL:a,accessToken:i,created:c,app:m,fetch:p}=j(n),d=new FormData;d.append("imagedata",e),d.append("access_token",i),a&&d.append("referer_url",a.toString()),m!==void 0&&d.append("app",m),t!==void 0&&d.append("title",t),r!=null&&d.append("desc",r),s&&d.append("collection_id",s),o&&d.append("metadata_is_public","true"),c!==void 0&&d.append("created_at",${c});let h=await p("https://upload.gyazo.com/api/upload",{method:"POST",mode:"cors",credentials:"omit",body:d}),g=await F(h);return g.ok?{ok:!0,value:JSON.parse(g.value)}:g};var u=e=>{let{fetch:n=globalThis.fetch,hostName:t="scrapbox.io",...r}=e;return{fetch:n,hostName:t,...r}};var x=class extends Error{constructor(n){super(${n.status} ${n.statusText} when fetching ${n.url});this.response=n;this.name="UnexpectedResponseError";Error.captureStackTrace&&Error.captureStackTrace(this,x)}},f=async e=>{let n=e.clone(),t=await n.text(),r=te(t);if(!r)throw new x(n);return{ok:!1,value:r}};var fe=async e=>{let{sid:n,hostName:t,fetch:r}=u(e??{}),o=new Request(https://${t}/api/users/me,n?{headers:{Cookie:l(n)}}:void 0),s=await r(o);if(!s.ok)throw new x(s);return await s.json()};var l=e=>connect.sid=${e},b=async e=>globalThis._csrf?globalThis._csrf:(await fe(e)).csrfToken;var ge=(e,n,t)=>{let{sid:r,hostName:o,followRename:s,projects:a}=u(t??{}),i=new URLSearchParams;i.append("followRename",${s??!0});for(let m of a??[])i.append("projects",m);let c=https://${o}/api/pages/${e}/${y(n)}?${i.toString()};return new Request(c,r?{headers:{Cookie:l(r)}}:void 0)},he=async e=>{if(!e.ok)return e.status===414?{ok:!1,value:{name:"TooLongURIError",message:"project ids may be too much."}}:f(e);let n=await e.json();return{ok:!0,value:n}},X=async(e,n,t)=>{let{fetch:r}=u(t??{}),o=ge(e,n,t),s=await r(o);return await he(s)};X.toRequest=ge;X.fromResponse=he;var xe=(e,n)=>{let{sid:t,hostName:r,sort:o,limit:s,skip:a}=u(n??{}),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 c=https://${r}/api/pages/${e}?${i.toString()};return new Request(c,t?{headers:{Cookie:l(t)}}:void 0)},Ee=async e=>{if(!e.ok)return f(e);let n=await e.json();return{ok:!0,value:n}},be=async(e,n)=>{let{fetch:t}=u(n??{}),r=await t(xe(e,n));return await Ee(r)};be.toRequest=xe;be.fromResponse=Ee;var ye=(e,n,t,r)=>{let{sid:o,hostName:s}=u(r??{}),a=https://${s}/api/table/${e}/${y(n)}/${encodeURIComponent(t)}.csv;return new Request(a,o?{headers:{Cookie:l(o)}}: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."}}:f(e),Le=async(e,n,t,r)=>{let{fetch:o}=u(r??{}),s=ye(e,n,t,r),a=await o(s);return await we(a)};Le.toRequest=ye;Le.fromResponse=we;var ke=(e,n)=>{let{sid:t,hostName:r}=u(n??{});return new Request(https://${r}/api/projects/${e},t?{headers:{Cookie:l(t)}}:void 0)},Te=async e=>{if(!e.ok)return f(e);let n=await e.json();return{ok:!0,value:n}},H=async(e,n)=>{let{fetch:t}=u(n??{}),r=ke(e,n),o=await t(r);return Te(o)};H.toRequest=ke;H.fromResponse=Te;var Re=(e,n)=>{let{sid:t,hostName:r}=u(n??{}),o=new URLSearchParams;for(let s of e)o.append("ids",s);return new Request(https://${r}/api/projects?${o.toString()},t?{headers:{Cookie:l(t)}}:void 0)},Pe=async e=>{if(!e.ok)return f(e);let n=await e.json();return{ok:!0,value:n}},ve=async(e,n)=>{let{fetch:t}=u(n??{}),r=await t(Re(e,n));return Pe(r)};ve.toRequest=Re;ve.fromResponse=Pe;var Ie=async(e,n)=>{let{sid:t,hostName:r,fetch:o,csrf:s}=u(n??{}),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 b(n),...t?{Cookie:l(t)}:{}},body:JSON.stringify({timeout:3e3})}),i=await o(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 Me=async(e,n)=>{let{sid:t,hostName:r,fetch:o,csrf:s}=u(n??{}),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 b(n),...t?{Cookie:l(t)}:{}},body:JSON.stringify({timeout:3e3})}),i=await o(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 Ne=async e=>{let{sid:n,hostName:t,gyazoTeamsName:r}=u(e??{}),o=new Request(https://${t}/api/login/gyazo/oauth-upload/token${r??gyazoTeamsName=${r}:""},n?{headers:{Cookie:l(n)}}:void 0),s=await fetch(o);if(!s.ok)return f(s);let{token:a}=await s.json();return{ok:!0,value:a}};var Ce=(e,n,t,r)=>{let{sid:o,hostName:s}=u(r??{}),a=https://${s}/api/code/${e}/${y(n)}/${y(t)};return new Request(a,o?{headers:{Cookie:l(o)}}:void 0)},Ue=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"}}:f(e),Se=async(e,n,t,r)=>{let{fetch:o}=u(r??{}),s=Ce(e,n,t,r),a=await o(s);return await Ue(a)};Se.toRequest=Ce;Se.fromResponse=Ue;var _e=new TextEncoder().encode("0123456789abcdef");function Oe(e){let n=new Uint8Array(e.length*2);for(let t=0;t<n.length;t++){let r=et;nt*2=_er>>4,nt*2+1=_er&15}return n}var Be="md5: data is invalid type",T=64,A=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(T),this.#i=0,this.#s=0,this.#a=0}#p(n){let t=this.#s;t+=n,t>4294967295&&(this.#a+=1),this.#s=t>>>0}#c(n){let t=this.#e,r=this.#t,o=this.#r,s=this.#n,a=L=>nL|nL+1<<8|nL+2<<16|nL+3<<24,i=(L,ee)=>L<<ee|L>>>32-ee,c=a(0),m=a(4),p=a(8),d=a(12),h=a(16),g=a(20),w=a(24),I=a(28),M=a(32),N=a(36),C=a(40),U=a(44),S=a(48),_=a(52),O=a(56),B=a(60);t=r+i(((o^s)&r^s)+t+c+3614090360,7),s=t+i(((r^o)&t^o)+s+m+3905402710,12),o=s+i(((t^r)&s^r)+o+p+606105819,17),r=o+i(((s^t)&o^t)+r+d+3250441966,22),t=r+i(((o^s)&r^s)+t+h+4118548399,7),s=t+i(((r^o)&t^o)+s+g+1200080426,12),o=s+i(((t^r)&s^r)+o+w+2821735955,17),r=o+i(((s^t)&o^t)+r+I+4249261313,22),t=r+i(((o^s)&r^s)+t+M+1770035416,7),s=t+i(((r^o)&t^o)+s+N+2336552879,12),o=s+i(((t^r)&s^r)+o+C+4294925233,17),r=o+i(((s^t)&o^t)+r+U+2304563134,22),t=r+i(((o^s)&r^s)+t+S+1804603682,7),s=t+i(((r^o)&t^o)+s+_+4254626195,12),o=s+i(((t^r)&s^r)+o+O+2792965006,17),r=o+i(((s^t)&o^t)+r+B+1236535329,22),t=r+i(((r^o)&s^o)+t+m+4129170786,5),s=t+i(((t^r)&o^r)+s+w+3225465664,9),o=s+i(((s^t)&r^t)+o+U+643717713,14),r=o+i(((o^s)&t^s)+r+c+3921069994,20),t=r+i(((r^o)&s^o)+t+g+3593408605,5),s=t+i(((t^r)&o^r)+s+C+38016083,9),o=s+i(((s^t)&r^t)+o+B+3634488961,14),r=o+i(((o^s)&t^s)+r+h+3889429448,20),t=r+i(((r^o)&s^o)+t+N+568446438,5),s=t+i(((t^r)&o^r)+s+O+3275163606,9),o=s+i(((s^t)&r^t)+o+d+4107603335,14),r=o+i(((o^s)&t^s)+r+M+1163531501,20),t=r+i(((r^o)&s^o)+t+_+2850285829,5),s=t+i(((t^r)&o^r)+s+p+4243563512,9),o=s+i(((s^t)&r^t)+o+I+1735328473,14),r=o+i(((o^s)&t^s)+r+S+2368359562,20),t=r+i((r^o^s)+t+g+4294588738,4),s=t+i((t^r^o)+s+M+2272392833,11),o=s+i((s^t^r)+o+U+1839030562,16),r=o+i((o^s^t)+r+O+4259657740,23),t=r+i((r^o^s)+t+m+2763975236,4),s=t+i((t^r^o)+s+h+1272893353,11),o=s+i((s^t^r)+o+I+4139469664,16),r=o+i((o^s^t)+r+C+3200236656,23),t=r+i((r^o^s)+t+_+681279174,4),s=t+i((t^r^o)+s+c+3936430074,11),o=s+i((s^t^r)+o+d+3572445317,16),r=o+i((o^s^t)+r+w+76029189,23),t=r+i((r^o^s)+t+N+3654602809,4),s=t+i((t^r^o)+s+S+3873151461,11),o=s+i((s^t^r)+o+B+530742520,16),r=o+i((o^s^t)+r+p+3299628645,23),t=r+i((o^(r|~s))+t+c+4096336452,6),s=t+i((r^(t|~o))+s+I+1126891415,10),o=s+i((t^(s|~r))+o+O+2878612391,15),r=o+i((s^(o|~t))+r+g+4237533241,21),t=r+i((o^(r|~s))+t+S+1700485571,6),s=t+i((r^(t|~o))+s+d+2399980690,10),o=s+i((t^(s|~r))+o+C+4293915773,15),r=o+i((s^(o|~t))+r+m+2240044497,21),t=r+i((o^(r|~s))+t+M+1873313359,6),s=t+i((r^(t|~o))+s+B+4264355552,10),o=s+i((t^(s|~r))+o+w+2734768916,15),r=o+i((s^(o|~t))+r+_+1309151649,21),t=r+i((o^(r|~s))+t+h+4149444226,6),s=t+i((r^(t|~o))+s+U+3174756917,10),o=s+i((t^(s|~r))+o+p+718787259,15),r=o+i((s^(o|~t))+r+N+3951481745,21),this.#e=this.#e+t>>>0,this.#t=this.#t+r>>>0,this.#r=this.#r+o>>>0,this.#n=this.#n+s>>>0}update(n){let t;if(typeof n=="string")t=new TextEncoder().encode(n);else if(typeof n=="object")if(n instanceof ArrayBuffer||ArrayBuffer.isView(n))t=new Uint8Array(n);else throw new TypeError(Be);else throw new TypeError(Be);let r=this.#i,o=T-r;if(t.length<o)this.#o.set(t,r),r+=t.length;else{this.#o.set(t.slice(0,o),r),this.#c(this.#o);let s=o;for(;s+T<=t.length;)this.#c(t.slice(s,s+T)),s+=T;this.#o.fill(0).set(t.slice(s),0),r=t.length-s}return this.#i=r,this.#p(t.length),this}digest(){let n=T-this.#i;n<9&&(n+=T);let t=new Uint8Array(n);t0=128;let r=this.#s<<3,o=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=o&255,tt.length-3=o>>>8&255,tt.length-2=o>>>16&255,tt.length-1=o>>>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.#n,!0),s}toString(n="hex"){let t=this.digest();switch(n){case"hex":return new TextDecoder().decode(Oe(new Uint8Array(t)));case"base64":{let r=new Uint8Array(t),o="";for(let s=0;s<r.length;++s)o+=String.fromCharCode(rs);return btoa(o)}default:throw new Error("md5: invalid format")}}};var $e=async(e,n,t)=>{let r=new A().update(await e.arrayBuffer()).toString(),o=await mt(e,n,r,t);if(!o.ok)return o;if("embedUrl"in o.value)return{ok:!0,value:o.value};let s=await ft(o.value.signedUrl,e,t);return s.ok?gt(n,o.value.fileId,r,t):s},mt=async(e,n,t,r)=>{let{sid:o,hostName:s,fetch:a,csrf:i}=u(r??{}),c={md5:t,size:e.size,contentType:e.type,name:e.name},m=i??await b(),p=new Request(https://${s}/api/gcs/${n}/upload-request,{method:"POST",body:JSON.stringify(c),headers:{"Content-Type":"application/json;charset=utf-8","X-CSRF-TOKEN":m,...o?{Cookie:l(o)}:{}}}),d=await a(p);return d.ok?{ok:!0,value:await d.json()}:f(d)},ft=async(e,n,t)=>{let{sid:r,fetch:o}=u(t??{}),s=await o(e,{method:"PUT",body:n,headers:{"Content-Type":n.type,...r?{Cookie:l(r)}:{}}});if(!s.ok){if(s.headers.get("Content-Type")?.includes?.("/xml"))return{ok:!1,value:{name:"GCSError",message:await s.text()}};throw new x(s)}return{ok:!0,value:void 0}},gt=async(e,n,t,r)=>{let{sid:o,hostName:s,fetch:a,csrf:i}=u(r??{}),c=i??await b(),m=new Request(https://${s}/api/gcs/${e}/verify,{method:"POST",body:JSON.stringify({md5:t,fileId:n}),headers:{"Content-Type":"application/json;charset=utf-8","X-CSRF-TOKEN":c,...o?{Cookie:l(o)}:{}}}),p=await a(m);if(!p.ok){try{if(p.status===404)return{ok:!1,value:{name:"NotFoundError",message:(await p.json()).message}}}catch{throw new x(p)}throw new x(p)}let d=await p.json();return{ok:!0,value:d}};var je=(e,n)=>{let t=new FormData;return t.append("data",e),t.append("metadata",JSON.stringify({app:"Gyazo",title:e.name})),GM_fetch(https://gif.gyazo.com/${n?.teams?"teams":"gif"}/upload,{method:"POST",body:t,credentials:"include",headers:{Origin:"https://gyazo.com","sec-fetch-site":"same-site"},referrer:"https://gyazo.com/"})};var D="",Fe=!1,q=new Map,He=async(e,n,t,r)=>{let o=q.get(e.href);if(o)return o;if(e.hostname==="video.twimg.com"||${e}.endsWith(".svg")){let c=await GM_fetch(e);if(!c.ok)return;let m=c.headers.get("content-type")?.split?.(";")?.0??${e}.endsWith(".mp4")?"video/mp4":"video/webm",p=new File(await c.blob(),r,{type:m});if(m==="video/mp4"){let g=await je(p);if(g.ok){let w=new URL(await g.text());return q.set(e.href,w),w}}let d=await $e(p,t);if(!d.ok)throw Error(d.value.name);let h=new URL(d.value.embedUrl);return q.set(e.href,h),h}if(e.hostname!=="pbs.twimg.com"||!e.pathname.startsWith("/media"))return;if(Fe){if(!D)return}else{let c=await Ne();if(Fe=!0,!c.ok){alert("You haven't logged in Gyazo yet, so you can only upload images to scrapbox.io.");return}if(D=c.value||"",!D){alert("You haven't connect Gyazo to scrapbox.io yet.");return}}let s=await GM_fetch(e);if(!s.ok)return;let a=await me(await s.blob(),{accessToken:D,refererURL:n,description:r});if(!a.ok)throw Error(a.value.name);let i=new URL(a.value.permalink_url);return q.set(e.href,i),i};var ht={"<":"<",">":">","&":"&",""":'"',"'":"'","`":""},Y=e=>e.replace(/&(lt|gt|amp|quot|#x27|#x60);/g,n=>ht[n]);var G=e=>{let n={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:c,title:m}=e.card.binding_values,p="STRING";c?.type===p&&(i.description=c.string_value),m?.type===p&&(i.title=m.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?.((c,m)=>(m.bitrate??0)-(c.bitrate??0))?.[0].url??i.media_url_https)}]:[])??[]}))??[]].sort((a,i)=>a.indices[0]-i.indices[0]),o=[];{let a=0,i=e.text;for(let{indices:c,...m}of r){let p=[...i].slice(0,c[0]-a).join("");o.push({type:"plain",text:Y(p)}),o.push(m),i=[...i].slice(c[1]-a).join(""),a=c[1]}i&&o.push({type:"plain",text:Y(i)})}let s={id:e.id_str,content:o,author:n,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=G(e.parent)),e.quoted_tweet&&(s.quote=G(e.quoted_tweet)),s};var xt=["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"],K=(e=scrapbox.Project.name,n=location.host)=>t=>{if(t.host!==n)return t;let[,r,o]=t.pathname.match(/^\/([\w\d][\w\d-]{0,22}[\w\d])(?:\/?|\/(.+))$/)??[];return!r||xt.includes(r)?t:o?r===e?${decodeURIComponent(o)}:/${r}/${decodeURIComponent(o)}:/${r}};var Ae=(e=Et)=>n=>{let[,t]=n.href.match(/^https:\/\/(?:(?:www\.|mobile\.|m\.)?twitter|x)\.com\/[\w\d_]+\/(?:status|statuses)\/(\d+)/)??[];return t?(async()=>{let r=await(ue(t)??Me(n.href));if(!r.ok)throw r.value;return e("images"in r.value?{...r.value,id:t}:r.value,n)})():n},Et=async e=>{if("images"in e)return v(e);let{quote:n,replyTo:t,...r}=G(e);return[...t?[...(await v(t)).split( ).map(o=> > ${o}),...t.quote?(await v(t.quote)).split(
).map(o=> > ${o}):[]]:[],...(await v(r)).split(
).map(o=>> ${o}),...n?(await v(n)).split(
).map(o=>> > ${o}):[]].join(
)?.map?.(o=>> ${J(o)})??["> [/ no description provided]"],...e.images.length>0?[> ${e.images.map(o=>[${o}])}]:[]].join(
);let t=e.content,r=e.author.screenName;return[@${J(r)} ${n},...(await Promise.all(t.map(async o=>{switch(o.type){case"plain":return o.text;case"hashtag":return #${o.text} ;case"symbol":return #$${o.text} ;case"mention":return@${o.screenName};case"media":{let s=[],a=1;for(;a<o.media.length;a+=2)s.push([${await Q(o.mediaa-1,n)}] [${await Q(o.mediaa,n)}]);return a===o.media.length&&s.push([${await Q(o.mediaa-1,n)}]), ${s.join(`
`)}
}case"url":return${K()(o.url)} }}))).join("").split(
)].join(
)},z="",bt=async()=>{if(z)return z;let e=await H(scrapbox.Project.name);if(!e.ok)throw new Error(e.value.name);return z=e.value.id,z},Q=async(e,n)=>await He(e.url,n,await bt(),"")??e.url,J=e=>e.replace(/\b/gm,"").replace(/[\s\r\n\u2028\u2029]+/gm," ").replace(/\s*[[\]]\s*/g," ").trim();var De=e=>{if(!window.GM_fetch)return;let n=window.GM_fetch;return(async()=>{let t=await n(https://t.co/${e}),r=R(t);if(r)return{ok:!1,value:r};let o=new DOMParser().parseFromString(await t.text(),"text/html");return{ok:!0,value:o.title}})()};var qe=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 n=De(e.pathname.slice(1));return n?(async()=>{let t=await n;if(!t.ok)throw t.value;try{return new URL(t.value)}catch(r){if(r instanceof TypeError)return e;throw r}})():e};var Ge=e=>{if(!e.pathname.startsWith("/wiki/"))return e;if(!/^\w+\.wikipedia\.org$/.test(e.hostname)){let,o=e.hostname.match(/^(\w+)\.m\.wikipedia\.org$/)??[];if(!o)return e;e.hostname=${o}.wikipedia.org}let n=decodeURIComponent(e.pathname.slice(6)),t=e.hash?decodeURIComponent(e.hash.slice(1)):"",r=${e.origin}/wiki/${n};return t?[${t} | ${n} - Wikipedia ${r}#${t}]:[${n} - Wikipedia ${r}]};var Ke=e=>{if(e.hostname!=="www.wikiwand.com")return e;let,n,t=e.pathname.match(/^\/(^\/+)\/(^\/+)/)??[];return!n||!t||(e.hostname=${n}.wikipedia.org,e.pathname=/wiki/${t},e.hash=e.hash.startsWith("#/")?#${e.hash.slice(2)}:e.hash),e};var Z=e=>{if(!e.hostname.startsWith("www.google."))return e;let n=e.searchParams.get("url");return n?new URL(decodeURIComponent(n)):e};var ze=/charset=(^;+)/,We=e=>{if(!window.GM_fetch)return;let n=window.GM_fetch;return(async()=>{let t=await n(${e}),r=R(t);if(r)return{ok:!1,value:r};let o=t.headers.get("content-type")?.match?.(ze)?.1??await yt(t.clone()),s=new TextDecoder(o).decode(await t.arrayBuffer());return{ok:!0,value:s}})()},yt=async e=>{let n=new DOMParser().parseFromString(await e.text(),"text/html");return n.querySelector("metacharset")?.getAttribute?.("charset")??n.querySelector('metahttp-equiv="content-type"')?.getAttribute?.("content")?.match?.(ze)?.1??"utf-8"};var Ve=(e=wt)=>async n=>e(await Lt(n),n),wt=(e,n)=>{let t=(E(e)?e:e.title).replace(/\s/g," ").replaceAll("","[").replaceAll("","]");return t?[${n.hash?${decodeURIComponent(n.hash.slice(1))} | :""}${t} ${n}]:${n}},Lt=async e=>{let n=We(e);if(!n){let r=await Ie(e);if(!r.ok)throw r.value;return r.value}let t=await n;if(!t.ok)throw t.value;return new DOMParser().parseFromString(t.value,"text/html")};var Xe=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,n=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 n&&(e.hash="",e.search="",e.pathname=/dp/${n}),e};var Ye=e=>{if(!/(?:0-9a-z-\.)?gyazo\.com/.test(e.hostname))return e;let,n=e.pathname.match(/^\/(0-9a-f{32})(?:\/raw)?$/)??[];return n?[https://gyazo.com/${n}]:e};var Qe=(e,...n)=>{if(e instanceof URL)return V(new URL(e),...n);let t=0,r=0,o=0,s=!1,a=e.split(/(https?:\/\/\S+)/g).map(p=>{if(!/^https?:\/\/\S+$/.test(p))return p;t++;try{let d=V(new URL(p),...n);return E(d)?(r++,d):(s=!0,d,p)}catch(d){return console.error(d),o++,p}});if(!s)return a.join("");let{render:i,dispose:c}=ce(),m=()=>i({type:"spinner"},{type:"text",text:URL: ${r}/${t} converted, ${o} failed});return m(),Promise.all(a.map(async p=>{if(E(p))return p;try{let d=await p0;return r++,d}catch(d){return console.error(d),o++,p1}finally{m()}})).then(p=>(i({type:"check-circle"},{type:"text",text:URL: ${r}/${t} converted, ${o} failed}),p.join(""))).finally(()=>{setTimeout(c,1e3)})};var kt=Z,qe,Z,Ke,Xe,K(),Ye,Ae(),Ge,Ve();scrapbox.PopupMenu.addButton({title:e=>/https?:\/\/\S+/.test(e)?"URL":"",onClick:e=>{let n=Qe(e,...kt);if(typeof n=="string")return e===n?void 0:n;n.then(t=>{if(e!==t)return ae(t)})}});