Files
gdown-helper/packages/edge/assets/index.ts-D64R0PS1.js

8 lines
29 KiB
JavaScript
Raw Permalink Normal View History

import{b as c,g as k}from"./settings-mco8QK8Y.js";import{n as mt,a as $t}from"./downloadIntent-Dv31jC2S.js";import{n as x,a as qt}from"./clipTypes-C_ha5Ash.js";const yt="org.gdown.nativehost";async function At(t){return c.runtime.sendNativeMessage(yt,{action:"addUri",...t})}async function Ot(){return c.runtime.sendNativeMessage(yt,{action:"focus"})}const G="history";async function Nt(){const e=(await c.storage.local.get([G]))[G];return Array.isArray(e)?e:[]}async function Rt(t){await c.storage.local.set({[G]:t.slice(0,300)})}async function Et(t){const e=await Nt(),n=e.findIndex(r=>r.gid===t.gid);n>=0?e[n]=t:e.unshift(t),await Rt(e)}function ht(t,e){const n=Array.isArray(t?.responseHeaders)?t.responseHeaders:[],r=e.toLowerCase(),o=n.find(i=>String(i?.name||"").toLowerCase()===r);return String(o?.value||"")}function Lt(t){const e=t.toLowerCase();return e?e.includes("application/vnd.apple.mpegurl")||e.includes("application/x-mpegurl")||e.includes("audio/mpegurl")?"m3u8":e.includes("video/mp4")?"mp4":e.includes("application/octet-stream")&&e.includes("m3u8")?"m3u8":e.includes("hls")?"hls":"unknown":"unknown"}function Dt(t){const e=String(t||"").toLowerCase();return e.includes(".m3u8")?"m3u8":e.includes(".m3u")?"m3u":e.includes(".mp4")?"mp4":e.includes("m3u8")?"m3u8":e.includes("hls")?"hls":"unknown"}function wt(t,e){const n=Lt(e);return n!=="unknown"?n:Dt(t)}function Ht(t){if(!t?.url)return!1;const e=String(t?.method||"").toUpperCase();if(e&&e!=="GET")return!1;const n=Number(t?.statusCode||0);if(n>0&&(n<200||n>299))return!1;const r=String(t?.type||"");if(!["xmlhttprequest","media","other","main_frame","sub_frame","fetch"].includes(r))return!1;const o=ht(t,"content-type");return wt(t.url,o)!=="unknown"}function Bt(t,e=""){const n=ht(t,"content-type"),r=String(t?.url||""),o=wt(r,n),i=Number.isInteger(t?.tabId)?Number(t.tabId):-1,a=Date.now();return{id:`${a}:${i}:${o}:${r}`,url:r,kind:o,tabId:i,pageUrl:String(t?.documentUrl||t?.initiator||""),referer:String(e||t?.documentUrl||t?.initiator||""),contentType:n,detectedAt:a}}function rt(t){try{const e=new URL(t);return`${e.protocol}//${e.host}${e.pathname}`.toLowerCase()}catch{return String(t||"").toLowerCase()}}const F="media_candidates",_t=200;async function St(){const e=(await c.storage.local.get([F]))[F];return Array.isArray(e)?e:[]}async function Ft(t){const e=[...t].sort((n,r)=>r.detectedAt-n.detectedAt);await c.storage.local.set({[F]:e.slice(0,_t)})}async function Pt(t,e){const n=await St(),r=n.findIndex(o=>{try{const i=new URL(o.url);return`${i.protocol}//${i.host}${i.pathname}`.toLowerCase()===e}catch{return o.url.toLowerCase()===e}});r>=0?n[r]={...n[r],...t,detectedAt:Date.now()}:n.unshift(t),await Ft(n)}async function zt(){await c.storage.local.set({[F]:[]})}const V="clips",jt=500;function xt(t,e){return Date.parse(e.createdAt||"")-Date.parse(t.createdAt||"")}function q(t){return`${x(t.pageUrl)}::${qt(t.quote).toLowerCase()}`}async function b(){const e=(await c.storage.local.get([V]))[V];return Array.isArray(e)?e:[]}async function O(t){const e=[...t].sort(xt).slice(0,jt);await c.storage.local.set({[V]:e})}async function ot(t){const e=x(t);return(await b()).filter(r=>x(r.pageUrl)===e).sort(xt)}async function Wt(t){return(await b()).find(r=>r.id===t)||null}async function bt(t){const e=await b(),n=q(t),r=e.find(a=>q(a)===n);if(r){const a=e.findIndex(s=>s.id===r.id);return a>=0?(e[a]={...e[a],tabId:t.tabId??e[a].tabId,pageTitle:t.pageTitle||e[a].pageTitle,anchor:t.anchor||e[a].anchor,resolveStatus:t.resolveStatus??e[a].resolveStatus,resolveUpdatedAt:t.resolveUpdatedAt??e[a].resolveUpdatedAt},await O(e),e[a]):r}const o=Date.now(),i=e.find(a=>{const s=Date.parse(a.createdAt||"");return!Number.isFinite(s)||Math.abs(o-s)>8e3?!1:q(a)===n});return i||(e.unshift(t),await O(e),t)}async function Gt(t){const e=await b(),n=e.filter(r=>r.id!==t);return n.length===e.length?!1:(await O(n),!0)}async function B(t,e){const n=await b(),r=n.findIndex(a=>a.id===t);if(r<0)return null;const o=n[r];if(o.resolveStatus===e)return o;const i={...o,resolveStatus:e,resolveUpdatedAt:new Date().toISOStri
`).trim(),o=(n.length>4e3?`${n.slice(0,4e3)}
...(truncated)`:n).split(`
`).map(a=>a.trimEnd()).filter((a,s,u)=>!(a===""&&u[s-1]==="")),i=o.length===0?"> ":o.map(a=>`> ${a}`).join(`
`);return[`### ${e+1}. Clip`,i,"",`- created: ${t.createdAt}`,`- status: ${t.resolveStatus||"ok"}`].join(`
`)}function Mt(t,e,n=""){const o=[`# ${n||t[0]?.pageTitle||e||"Untitled"}`,`- source: ${e}`,`- exportedAt: ${new Date().toISOString()}`,`- clips: ${t.length}`,"","---",""];for(let i=0;i<t.length;i+=1)o.push(me(t[i],i)),o.push("");return o.join(`
`)}function et(t){return String(t||"").replace(/\s*[|\-–—]\s*(qiita|medium|youtube|x|twitter|tistory|velog|github)\s*$/i,"").replace(/\s*[-–—|]\s*(edge|chrome|firefox)\s*$/i,"").replace(/\[[^\]]{1,30}\]\s*$/g,"").trim().replace(/[\\/:*?"<>|]/g,"_").replace(/[(){}[\]]/g,"").replace(/\s+/g," ").replace(/[._-]{2,}/g,"-").replace(/^[._\-\s]+|[._\-\s]+$/g,"").slice(0,90)||"clips"}function ye(t,e){let n=String(t||"").trim(),r=String(e||"").trim();if(n.includes("/")||n.includes("\\")){const o=n.split(/[\\/]+/).filter(Boolean);n=o[o.length-1]||""}return(/^myvault$/i.test(n)||/^example$/i.test(n))&&(n=""),r=r.replace(/^\/+|\/+$/g,""),{vault:n,folder:r}}async function he(){try{const t=await c.storage.local.get(Y);return String(t?.[Y]||"").trim()}catch{return""}}async function we(t){const e=String(t||"").trim();if(e)try{await c.storage.local.set({[Y]:e})}catch{}}async function nt(t,e,n){if(!t.trim())return{ok:!1,error:"filename is empty"};const r=`data:${e},${encodeURIComponent(n)}`;try{return{ok:!0,downloadId:await c.downloads.download({url:r,filename:t.trim(),saveAs:!0})}}catch(o){return{ok:!1,error:String(o)}}}async function gt(t,e,n){const r=x(String(t||"").trim());if(!r)return{ok:!1,error:"pageUrl is empty"};const o=await ot(r);if(o.length===0)return{ok:!1,error:"no clips for current page"};const i=String(e||o[0]?.pageTitle||r).trim();if(n==="md"){const f=Mt(o,r,i),d=`${et(i)}-clips.md`,m=await nt(d,"text/markdown;charset=utf-8",f);return{ok:m.ok,error:m.error,count:o.length,downloadId:m.downloadId}}const a={exportedAt:new Date().toISOString(),version:1,count:o.length,clips:o},s=`${et(i)}-clips.json`,u=await nt(s,"application/json;charset=utf-8",JSON.stringify(a,null,2));return{ok:u.ok,error:u.error,count:o.length,downloadId:u.downloadId}}async function Se(t,e){const n=x(String(t||"").trim());if(!n)return{ok:!1,error:"pageUrl is empty"};const r=await ot(n);if(r.length===0)return{ok:!1,error:"no clips for current page"};const o=await k(),i=ye(o.obsidianVault,o.obsidianFolder);i.vault&&await we(i.vault);const a=i.vault||await he(),s=String(e||r[0]?.pageTitle||n).trim(),u=Mt(r,n,s),f=i.folder,d=`${et(s)}-${new Date().toISOString().slice(0,19).replace(/[:T]/g,"-")}`,m=f?`${f}/${d}`:d,h=`file=${encodeURIComponent(m)}&content=${encodeURIComponent(u)}`;return{ok:!0,uri:a?`obsidian://new?vault=${encodeURIComponent(a)}&${h}`:`obsidian://new?${h}`,count:r.length}}async function xe(t,e){try{return!!(await c.scripting.executeScript({target:{tabId:t},args:[e.quote||e.anchor?.exact||"",e.anchor?.prefix||"",e.anchor?.suffix||""],func:(r,o,i)=>{const a=String(r||"").trim();if(!a)return{ok:!1,error:"quote is empty"};const s=String(o||"").trim(),u=String(i||"").trim(),f=document.body?.innerText||document.documentElement?.innerText||"";if(!f)return{ok:!1,error:"document text is empty"};let d=f.indexOf(a),m=-1;for(;d>=0;){const M=s?f.slice(Math.max(0,d-s.length),d).trim():"",W=u?f.slice(d+a.length,d+a.length+u.length).trim():"";if((!s||M===s)&&(!u||W===u)){m=d;break}d=f.indexOf(a,d+Math.max(1,Math.floor(a.length/2)))}if(m<0)return{ok:!1,error:"quote not found in page text"};const h=document.createTreeWalker(document.body||document.documentElement,NodeFilter.SHOW_TEXT);let g=0,l=null,p=null,y=0,L=0;const U=m,v=m+a.length;let D=h.nextNode();for(;D;){if(D.nodeType===Node.TEXT_NODE){const M=D,W=M.nodeValue||"",H=g+W.length;!l&&U>=g&&U<=H&&(l=M,y=Math.max(0,U-g)),!p&&v>=g&&v<=H&&(p=M,L=Math.max(0,v-g)),g=H}D=h.nextNode()}if(!l||!p)return{ok:!1,error:"failed to map quote to text nodes"};const j=document.createRange();j.setStart(l,Math.min(l.length,y)),j.setEnd(p,Math.min(p.length,L));const T=j.getBoundingClientRect(),S=document.createElement("span");return S.style.position="absolute",S.style.left=`${window.scrollX+T.left-2}px`,S.style.top=`${window.scrollY+T.top-2}px`,S.style.width=`${Math.max(8,T.width+4)}px`,S.style.height=`${Math.max(14,T.height+4)}px`,S.style.pointerEvents="none",S.style.borderRadius="6px",S.style.background="rgba(255, 240, 130, 0.42)",S.style.border="1px solid rgba(230, 190, 70, 0.72)",S.style.zIndex="2147483647",document