2 lines
13 KiB
JavaScript
2 lines
13 KiB
JavaScript
import{b as p,g as B}from"./settings-CgBxHrrF.js";import{i as w,n as W}from"./downloadIntent-Dv31jC2S.js";import{n as g,a as q}from"./clipTypes-C_ha5Ash.js";function M(){const e=document.body||document.documentElement;if(!e)return[];const t=document.createTreeWalker(e,NodeFilter.SHOW_TEXT),n=[];let r=0,o=t.nextNode();for(;o;){const i=o,l=(i.nodeValue||"").length;l>0&&(n.push({node:i,start:r,end:r+l}),r+=l),o=t.nextNode()}return n}function H(e){return e.map(t=>t.node.nodeValue||"").join("")}function I(e){const t=document.body||document.documentElement;if(!t)return"";if(e===t)return"/body";const n=[];let r=e.nodeType===Node.TEXT_NODE?e.parentNode:e;for(;r&&r!==t&&r.nodeType===Node.ELEMENT_NODE;){const o=r,i=o.tagName.toLowerCase();let s=1,l=o.previousElementSibling;for(;l;)l.tagName.toLowerCase()===i&&(s+=1),l=l.previousElementSibling;n.unshift(`${i}[${s}]`),r=o.parentElement}return`/body/${n.join("/")}`}function P(e){if(!e)return null;try{return document.evaluate(e,document,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue}catch{return null}}function V(e){const t=e.commonAncestorContainer,n=t.nodeType===Node.ELEMENT_NODE?t:t.parentElement;return!(!n||n.closest("input, textarea, select, button, script, style")||n.closest('[contenteditable="true"]'))}function Y(e,t,n){const r=e.slice(Math.max(0,t-40),t).trim(),o=e.slice(n,Math.min(e.length,n+40)).trim();return{prefix:r,suffix:o}}function Q(e,t){const n=e.startContainer,r=e.endContainer;if(n.nodeType!==Node.TEXT_NODE||r.nodeType!==Node.TEXT_NODE)return null;const o=t.find(s=>s.node===n),i=t.find(s=>s.node===r);return!o||!i?null:{start:o.start+e.startOffset,end:i.start+e.endOffset}}function R(e,t){if(!Number.isFinite(e)||!Number.isFinite(t)||e>=t)return null;const n=M(),r=n.find(s=>e>=s.start&&e<s.end),o=n.find(s=>t>s.start&&t<=s.end);if(!r||!o)return null;const i=document.createRange();return i.setStart(r.node,Math.max(0,Math.min(r.node.length,e-r.start))),i.setEnd(o.node,Math.max(0,Math.min(o.node.length,t-o.start))),i}function j(e){if(!e.xpathStart||!e.xpathEnd)return null;const t=P(e.xpathStart),n=P(e.xpathEnd);if(!t||!n||t.nodeType!==Node.ELEMENT_NODE||n.nodeType!==Node.ELEMENT_NODE)return null;const r=l=>{const b=document.createTreeWalker(l,NodeFilter.SHOW_TEXT).nextNode();return b&&b.nodeType===Node.TEXT_NODE?b:null},o=r(t),i=r(n);if(!o||!i||o.nodeType!==Node.TEXT_NODE||i.nodeType!==Node.TEXT_NODE||!Number.isInteger(e.startOffset)||!Number.isInteger(e.endOffset))return null;const s=document.createRange();return s.setStart(o,Math.min((o.nodeValue||"").length,Math.max(0,e.startOffset||0))),s.setEnd(i,Math.min((i.nodeValue||"").length,Math.max(0,e.endOffset||0))),s}function G(e){const t=String(e.exact||"");if(!t)return null;const n=M(),r=H(n);let o=r.indexOf(t);if(o<0)return null;const i=String(e.prefix||""),s=String(e.suffix||"");if(!i&&!s)return{start:o,end:o+t.length};for(;o>=0;){const l=r.slice(Math.max(0,o-i.length),o).trim(),S=r.slice(o+t.length,o+t.length+s.length).trim();if((!i||l===i)&&(!s||S===s))return{start:o,end:o+t.length};o=r.indexOf(t,o+t.length)}return null}function J(e){if(!e.rangeCount)return null;const t=e.getRangeAt(0);if(t.collapsed||!V(t))return null;const n=e.toString();if(!n.trim())return null;const r=M(),o=Q(t,r),i={exact:n};if(t.startContainer.nodeType===Node.TEXT_NODE&&t.endContainer.nodeType===Node.TEXT_NODE){const s=t.startContainer,l=t.endContainer;i.xpathStart=I(s),i.xpathEnd=I(l),i.startOffset=t.startOffset,i.endOffset=t.endOffset}if(o){i.startTextOffset=o.start,i.endTextOffset=o.end;const s=H(r),l=Y(s,o.start,o.end);l.prefix&&(i.prefix=l.prefix),l.suffix&&(i.suffix=l.suffix)}return{anchor:i,quote:n}}function Z(e){if(Number.isInteger(e.startTextOffset)&&Number.isInteger(e.endTextOffset)){const r=R(e.startTextOffset||0,e.endTextOffset||0);if(r&&!r.collapsed)return r}const t=G(e);if(t){const r=R(t.start,t.end);if(r&&!r.collapsed)return r}const n=j(e);return n&&!n.collapsed?n:null}const ee=8e3,m=new Map;let a=!1,A=null,T=g(window.location.href);function te(){const e=Date.now();for(const[t,n]of m.entries())n<=e&&m.delete(t)}async function x(e,t){if(!a)return!1;const n=W(e,window.location.href);if(!n)return!1;if(te(),m.has(n))return!0;m.set(n,Date.now()+ee);try{if((await p.runtime.sendMessage({type:"capture-link-download",url:n,referer:t||document.referrer||window.location.href}))?.ok)return!0}catch{}return m.delete(n),!1}function L(e){return e?e instanceof HTMLAnchorElement?e:e instanceof Element?e.closest("a[href]"):null:null}function D(e){return!!(e.metaKey||e.ctrlKey||e.shiftKey||e.altKey)}async function U(e){if(!a||e.defaultPrevented||D(e))return;const t=L(e.target);if(!t)return;const n=t.href||"";!n||!w(n,window.location.href)||(e.preventDefault(),e.stopImmediatePropagation(),e.stopPropagation(),await x(n,document.referrer||window.location.href))}function X(e){if(!a)return;const t=L(e.target);if(!t)return;const n=t.href||"";!n||!w(n,window.location.href)||D(e)||(e.preventDefault(),e.stopImmediatePropagation(),e.stopPropagation(),x(n,document.referrer||window.location.href))}document.addEventListener("pointerdown",e=>{e.button===0&&X(e)},!0);document.addEventListener("mousedown",e=>{e.button===0&&X(e)},!0);document.addEventListener("click",e=>{e.button===0&&U(e)},!0);document.addEventListener("keydown",e=>{if(!a||e.key!=="Enter"||e.defaultPrevented||D(e))return;const t=L(e.target);if(!t)return;const n=t.href||"";!n||!w(n,window.location.href)||(e.preventDefault(),e.stopImmediatePropagation(),e.stopPropagation(),x(n,document.referrer||window.location.href))},!0);document.addEventListener("keydown",e=>{a&&(e.defaultPrevented||e.altKey&&e.shiftKey&&(e.metaKey||e.ctrlKey||e.key.toLowerCase()==="c"&&(e.preventDefault(),e.stopImmediatePropagation(),e.stopPropagation(),$())))},!0);document.addEventListener("auxclick",e=>{e.button===1&&U(e)},!0);function ne(){try{const e=window.open.bind(window);window.open=function(n,r,o){const i=String(n||"").trim();return a&&i&&w(i,window.location.href)?(x(i,window.location.href),null):e(n,r,o)}}catch{}try{const e=HTMLAnchorElement.prototype.click;HTMLAnchorElement.prototype.click=function(){const n=this.href||this.getAttribute("href")||"";if(a&&n&&w(n,window.location.href)){x(n,document.referrer||window.location.href);return}e.call(this)}}catch{}}ne();let y=null,f=null,v=!1,F=null,E=window.location.href;function re(e){try{const t=new URL(e);return t.hostname!=="www.youtube.com"&&t.hostname!=="youtube.com"?!1:t.pathname==="/watch"&&t.searchParams.has("v")}catch{return!1}}function k(e,t="idle"){f&&(f.textContent=e,t==="ok"?f.style.color="#8ff0a4":t==="error"?f.style.color="#ff9b9b":f.style.color="#aeb7d8")}async function oe(){if(!v){v=!0,k("gdown으로 전송 중...");try{const e=await p.runtime.sendMessage({type:"page:enqueue-ytdlp-url",url:window.location.href,referer:window.location.href});e?.ok?k("다운로드 모달로 전송됨","ok"):k(`전송 실패: ${e?.error||"unknown error"}`,"error")}catch(e){k(`전송 실패: ${String(e)}`,"error")}finally{v=!1}}}function C(){y&&(y.remove(),y=null,f=null)}function c(){if(!a){C();return}if(window.top!==window.self)return;if(!re(window.location.href)){C();return}if(y)return;const e=document.createElement("div");e.id="gomdown-youtube-overlay",e.style.position="fixed",e.style.right="20px",e.style.bottom="24px",e.style.zIndex="2147483647",e.style.background="rgba(17, 21, 32, 0.94)",e.style.border="1px solid rgba(133, 148, 195, 0.35)",e.style.borderRadius="12px",e.style.padding="10px",e.style.boxShadow="0 8px 24px rgba(0, 0, 0, 0.28)",e.style.backdropFilter="blur(6px)",e.style.width="220px",e.style.fontFamily="ui-sans-serif, -apple-system, BlinkMacSystemFont, Segoe UI, sans-serif",e.style.color="#e8edff";const t=document.createElement("div");t.textContent="Gdown Helper",t.style.fontSize="12px",t.style.fontWeight="700",t.style.marginBottom="8px";const n=document.createElement("button");n.type="button",n.textContent="이 영상 다운로드",n.style.width="100%",n.style.height="34px",n.style.border="1px solid #5a69f0",n.style.borderRadius="8px",n.style.background="#5a69f0",n.style.color="#ffffff",n.style.fontSize="12px",n.style.fontWeight="700",n.style.cursor="pointer",n.addEventListener("click",()=>{oe()});const r=document.createElement("div");r.textContent="클릭 시 gdown 다운로드 모달로 연결",r.style.fontSize="11px",r.style.marginTop="8px",r.style.lineHeight="1.35",r.style.color="#aeb7d8",e.appendChild(t),e.appendChild(n),e.appendChild(r),document.documentElement.appendChild(e),y=e,f=r}function ie(){F===null&&(F=window.setInterval(()=>{const e=window.location.href;e!==E&&(E=e,c())},800),window.addEventListener("popstate",()=>{E=window.location.href,c()}),document.addEventListener("yt-navigate-finish",()=>{E=window.location.href,c()}))}c();ie();function se(e){return e.color==="yellow","rgba(255, 235, 87, 0.58)"}function N(){const e=Array.from(document.querySelectorAll("span[data-gomdown-clip]"));for(const t of e){const n=t.parentNode;if(n){for(;t.firstChild;)n.insertBefore(t.firstChild,t);n.removeChild(t)}}}function O(e,t="ok"){const n=z();n.textContent=e,n.style.display="block",n.style.borderColor=t==="ok"?"rgba(123, 190, 124, 0.55)":"rgba(200, 113, 113, 0.55)",n.style.color=t==="ok"?"#dffbe4":"#ffe4e4",u!==null&&window.clearTimeout(u),u=window.setTimeout(()=>{n.style.display="none",u=null,n.style.borderColor="rgba(128, 140, 180, 0.42)",n.style.color="#dce4fa"},1800)}function le(e){const t=Z(e.anchor);if(!t||t.collapsed)return!1;const n=document.createElement("span");n.dataset.gomdownClip=e.id,n.style.background=se(e),n.style.padding="0.04em 0.03em",n.style.borderRadius="0.12em",n.style.boxDecorationBreak="clone",n.style.setProperty("-webkit-box-decoration-break","clone"),n.style.cursor="pointer",n.title=e.quote||"clip",n.addEventListener("click",r=>{(r.altKey||r.metaKey||r.ctrlKey)&&(r.stopPropagation(),r.preventDefault())});try{const r=t.extractContents();return n.appendChild(r),t.insertNode(n),!0}catch{return!1}}function _(e){const t=document.querySelector(`span[data-gomdown-clip="${CSS.escape(e)}"]`);return t?(t.scrollIntoView({behavior:"smooth",block:"center",inline:"nearest"}),t.animate([{boxShadow:"0 0 0 0 rgba(255, 250, 164, 0.2)"},{boxShadow:"0 0 0 8px rgba(255, 250, 164, 0.65)"},{boxShadow:"0 0 0 0 rgba(255, 250, 164, 0.2)"}],{duration:760,easing:"ease-out"}),!0):!1}async function d(e=!1){if(!a)return;const t=g(window.location.href),n=await p.runtime.sendMessage({type:"clip:list",pageUrl:t});if(!n?.ok||!Array.isArray(n.items))return;N();const r=[...n.items].sort((i,s)=>{const l=Number(i.anchor.startTextOffset||0);return Number(s.anchor.startTextOffset||0)-l});let o=0;for(const i of r)le(i)&&(o+=1);e&&O(`클립 복원: ${o}/${r.length}`,o>0?"ok":"error")}async function $(){if(!a)return{ok:!1,error:"extension disabled"};const e=window.getSelection();if(!e)return{ok:!1,error:"selection unavailable"};const t=J(e);if(!t)return O("선택된 텍스트가 없습니다.","error"),{ok:!1,error:"empty selection"};const n=await p.runtime.sendMessage({type:"clip:create",pageUrl:g(window.location.href),pageTitle:document.title||window.location.href,quote:q(t.quote),anchor:t.anchor});return!n?.ok||!n.item?(O(`클립 저장 실패: ${n?.error||"unknown error"}`,"error"),{ok:!1,error:n?.error||"create failed"}):(N(),await d(),O("클립 저장 완료 (Alt+Shift+C)","ok"),{ok:!0})}function ae(){A===null&&(A=window.setInterval(()=>{const e=g(window.location.href);e!==T&&(T=e,d())},900),window.addEventListener("popstate",()=>{const e=g(window.location.href);e!==T&&(T=e,d())}))}ae();let h=null,u=null;function K(){h&&(h.style.display="none",u!==null&&(window.clearTimeout(u),u=null))}function z(){if(h)return h;const e=document.createElement("div");return e.id="gomdown-media-toast",e.style.position="fixed",e.style.left="18px",e.style.bottom="18px",e.style.zIndex="2147483647",e.style.maxWidth="360px",e.style.padding="10px 12px",e.style.borderRadius="10px",e.style.border="1px solid rgba(128, 140, 180, 0.42)",e.style.background="rgba(18, 21, 31, 0.95)",e.style.color="#dce4fa",e.style.fontSize="12px",e.style.lineHeight="1.35",e.style.fontFamily="ui-sans-serif, -apple-system, BlinkMacSystemFont, Segoe UI, sans-serif",e.style.boxShadow="0 10px 24px rgba(0, 0, 0, 0.28)",e.style.display="none",document.documentElement.appendChild(e),h=e,e}function ue(e){if(!a)return;const t=z(),n=String(e?.kind||"media").toUpperCase(),r=String(e?.suggestedOut||"").trim(),o=String(e?.url||"").trim().slice(0,96);t.textContent=r?`캡처됨 [${n}] ${r}`:`캡처됨 [${n}] ${o}${o.length>=96?"…":""}`,t.style.display="block",u!==null&&window.clearTimeout(u),u=window.setTimeout(()=>{t.style.display="none",u=null},2200)}p.runtime.onMessage.addListener(e=>{if(e?.type==="clip:create-from-selection")return $();if(e?.type==="media:captured"){ue({kind:e?.kind,url:e?.url,suggestedOut:e?.suggestedOut});return}if(e?.type==="clip:reveal"){const t=String(e?.id||"").trim();return t?(_(t)||d(!0).then(()=>{_(t)}),{ok:!0}):void 0}});async function fe(){try{a=!!(await B()).extensionStatus}catch{a=!1}a?(c(),await d()):(C(),K(),N())}fe();p.storage.onChanged.addListener((e,t)=>{if(t==="sync"&&e.extensionStatus){if(a=!!e.extensionStatus.newValue,!a){C(),K(),N();return}c(),d()}});
|