Files
chrome-extenstions/svg-gubbler/assets/background.ts-C1MWaM9c.js
2026-01-20 21:53:59 +03:30

2 lines
7.6 KiB
JavaScript

import"./index-BSHrD-on.js";import{e as g,l as C}from"./extension-utilities-DU05fpr-.js";async function H(u){const l=u??globalThis.document,i=l.location,a=e=>{try{const t=new Image;return t.src=e,t.outerHTML}catch(t){return console.warn(`Failed to create image from source: ${e}`,t),""}},m=e=>e?e.includes(".svg")||e.includes("data:image/svg+xml")||e.includes("image/svg+xml")||e.includes("data:")&&e.includes("<svg")||e.includes("http://www.w3.org/2000/svg")||/<(?:svg|path|circle|rect|g)\s/i.test(e)||/viewBox\s*=\s*["']/i.test(e):!1,p=()=>{const e=[];try{const t=l.querySelectorAll('img[src*=".svg"], img[src*="data:image/svg+xml"]'),n=l.querySelectorAll('object[type="image/svg+xml"]'),r=l.querySelectorAll('embed[type="image/svg+xml"], embed[src*=".svg"]'),o=l.querySelectorAll('iframe[src*=".svg"]');for(const c of t)c instanceof HTMLImageElement&&m(c.src)&&e.push(a(c.src));for(const c of n)c instanceof HTMLObjectElement&&e.push(a(c.data));for(const c of r)c instanceof HTMLEmbedElement&&e.push(a(c.src));for(const c of o)c instanceof HTMLIFrameElement&&e.push(a(c.src));const s=l.querySelectorAll("*");for(const c of s)if(c instanceof HTMLElement)try{const h=globalThis.getComputedStyle(c).backgroundImage;if(h&&m(h)){const d=h.match(/url\(['"]?([^'"()]+)['"]?\)/);d&&d[1]&&e.push(a(d[1]))}}catch(h){console.warn("Error accessing background image:",h)}}catch(t){console.warn("Error processing elements for SVG content:",t)}return e.filter(Boolean)},y=()=>{try{return[...l.querySelectorAll("svg")].filter(e=>!e.querySelector("use, symbol")).map(e=>e.outerHTML).filter(Boolean)}catch(e){return console.warn("Error gathering inline SVG elements:",e),[]}},S=()=>{try{return[...l.querySelectorAll("g")].map(e=>e.outerHTML).filter(Boolean)}catch(e){return console.warn("Error gathering G elements:",e),[]}},b=()=>{try{return[...l.querySelectorAll("symbol")].map(e=>e.outerHTML).filter(Boolean)}catch(e){return console.warn("Error gathering symbol elements:",e),[]}},v=()=>{const e=[];try{const t=l.querySelectorAll("use");for(const n of t){const r=n.getAttribute("href");r&&m(r)&&e.push(a(r));const o=n.getAttribute("xlink:href");o&&m(o)&&e.push(a(o))}}catch(t){console.warn("Error gathering use elements:",t)}return e.filter(Boolean)},E=()=>{const e=[];try{const t=l.querySelectorAll("*"),n=r=>{const o=r.shadowRoot;if(o){for(const s of o.querySelectorAll("svg"))e.push(s.outerHTML);for(const s of o.querySelectorAll('img[src*=".svg"], object[type="image/svg+xml"]'))s instanceof HTMLImageElement?e.push(a(s.src)):s instanceof HTMLObjectElement&&e.push(a(s.data));for(const s of o.querySelectorAll("*"))n(s)}};for(const r of t)n(r)}catch(t){console.warn("Error gathering SVGs from Shadow DOM:",t)}return e.filter(Boolean)},w=()=>{const e=[];try{for(const t of l.styleSheets)try{const n=[...t.cssRules||[]];for(const r of n)if(r instanceof CSSStyleRule){const o=r.style;for(let s=0;s<o.length;s++){const c=o[s];if(c.startsWith("--")){const h=o.getPropertyValue(c);if(m(h)){const d=h.match(/url\(['"]?([^'"()]+)['"]?\)/);d&&d[1]&&e.push(a(d[1]))}}}}}catch(n){console.warn("Could not access stylesheet due to CORS:",n)}}catch(t){console.warn("Error gathering SVGs from CSS custom properties:",t)}return e.filter(Boolean)},T=()=>{const e=[];try{const t=[...l.querySelectorAll("*")].filter(n=>n.tagName.includes("-"));for(const n of t){const r=n.querySelectorAll("slot");for(const o of r){const s=o.assignedNodes();for(const c of s)c instanceof SVGElement&&e.push(c.outerHTML)}}}catch(t){console.warn("Error gathering SVGs from Web Components:",t)}return e.filter(Boolean)},A=()=>{const e=[];try{const t=l.querySelectorAll("*");for(const n of t)try{const o=globalThis.getComputedStyle(n).getPropertyValue("content");if(o&&m(o)&&o.includes("url(")){const s=o.match(/url\(['"]?([^'"()]+)['"]?\)/);s&&s[1]&&e.push(a(s[1]))}}catch(r){console.warn("Error accessing CSS content property:",r)}}catch(t){console.warn("Error gathering SVGs from CSS content property:",t)}return e.filter(Boolean)},L=()=>{const e=[];try{for(const t of l.querySelectorAll("template")){const n=t.content;for(const r of n.querySelectorAll("svg"))e.push(r.outerHTML);for(const r of n.querySelectorAll('img[src*=".svg"], object[type="image/svg+xml"]'))r instanceof HTMLImageElement?e.push(a(r.src)):r instanceof HTMLObjectElement&&e.push(a(r.data))}}catch(t){console.warn("Error gathering SVGs from HTML templates:",t)}return e.filter(Boolean)},x=()=>{const e=[];try{const t=l.querySelectorAll('[class*="icon"], [class*="sprite"]');for(const n of t)try{const r=globalThis.getComputedStyle(n),o=r.backgroundImage,s=r.backgroundPosition;if(o&&s&&m(o)){const c=o.match(/url\(['"]?([^'"()]+)['"]?\)/);c&&c[1]&&e.push(a(c[1]))}}catch(r){console.warn("Error accessing sprite sheet styles:",r)}}catch(t){console.warn("Error parsing CSS sprite sheets:",t)}return e.filter(Boolean)},q=()=>{const e=[];try{for(const t of l.querySelectorAll("picture")){for(const r of t.querySelectorAll('source[type="image/svg+xml"]')){const o=r.getAttribute("srcset");if(o)for(const s of o.split(",")){const c=s.trim().split(" ")[0];e.push(a(c))}}const n=t.querySelector("img");n&&m(n.src)&&e.push(a(n.src))}}catch(t){console.warn("Error gathering SVGs from picture elements:",t)}return e.filter(Boolean)},M=(e,t,n)=>{const r=e.match(/\/([^/"']+)\.svg/i);if(r!=null&&r[1])return r[1];const o=e.match(/id=["']([^"']+)["']/i);if(o!=null&&o[1])return o[1];const s=e.match(/<title>([^<]+)<\/title>/i);return s!=null&&s[1]?s[1]:`${(t==null?void 0:t.host)||"unknown"}-${n+1}`},I=[...p(),...y(),...S(),...b(),...v(),...E(),...w(),...T(),...A(),...L(),...x(),...q()];return{data:[...new Set(I)].filter(e=>e.trim().length>0).map((e,t)=>{try{return{corsRestricted:!1,id:crypto.randomUUID(),lastEdited:new Date().toISOString(),name:M(e,i,t),svg:e}}catch(n){return console.warn("Error creating SVG data object:",n),{corsRestricted:!1,id:`svg-fallback-${t}`,lastEdited:new Date().toISOString(),name:`unknown-${t}`,svg:e}}}),host:(i==null?void 0:i.host)||"",href:(i==null?void 0:i.href)||"",origin:(i==null?void 0:i.origin)||""}}const f={createContextMenu(){chrome.contextMenus.create({contexts:["all"],id:"svg-gobbler",title:"Search page for SVGs"}),chrome.contextMenus.onClicked.addListener(u=>{u.menuItemId==="svg-gobbler"&&f.launchSvgGobbler()})},handleUninstall(){chrome.runtime.setUninstallURL("https://svggobbler.com/uninstall")},init(){f.setExtensionIcons(),f.launchOnboardingExperience(),f.launchExtensionFromOnboarding(),f.launchExtensionFromIcon(),f.handleUninstall(),f.createContextMenu()},launchExtensionFromIcon(){chrome.action.onClicked.addListener(f.launchSvgGobbler)},launchExtensionFromOnboarding(){const u=async function(l){const{data:i,type:a}=l;if(a==="launch-svg-gobbler-from-onboarding"){const m=function(p,y,S){p==="gobble"&&(S({data:i}),chrome.runtime.onMessage.removeListener(m))};chrome.runtime.onMessage.addListener(m),await g.createNewTab()}};chrome.runtime.onMessage.addListener(u)},launchOnboardingExperience(){chrome.runtime.onInstalled.addListener(async u=>{u.reason==="install"&&await g.createNewTab("onboarding.html")})},async launchSvgGobbler(){try{let u={data:[],host:"Collection",href:"",origin:""};const l=await g.getActiveTab();if(g.isExtensionTab(l))return;if(g.isNewTabPage(l)){g.createNewTab();return}u=await g.executeScript(l.id,H),chrome.runtime.onMessage.addListener(function i(a,m,p){a==="gobble"&&(p({data:u}),chrome.runtime.onMessage.removeListener(i))}),g.createNewTab()}catch(u){C.error("Error while trying to gobble SVGs",u)}},async setExtensionIcons(){g.isFirefox||"update_url"in chrome.runtime.getManifest()||chrome.action.setIcon({path:{16:"assets/dev/16.png",24:"assets/dev/24.png",32:"assets/dev/32.png",48:"assets/dev/48.png",64:"assets/dev/64.png"}})}};f.init();