-
Notifications
You must be signed in to change notification settings - Fork 1
/
content.js
191 lines (168 loc) · 5.74 KB
/
content.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
class SVGDetector {
constructor() {
this.setupMessageListener();
}
setupMessageListener() {
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "getSVGs") {
// Get inline SVGs and filter out invalid ones
const inlineSVGs = Array.from(document.getElementsByTagName("svg"))
.filter(svg => {
// Check if SVG has valid content
return svg.innerHTML.trim() !== '' &&
svg.getBoundingClientRect().width > 0 &&
svg.getBoundingClientRect().height > 0;
})
.map(svg => svg.outerHTML);
// Get external SVGs and filter out invalid ones
const externalSVGs = Array.from(document.querySelectorAll('img[src$=".svg"], object[data$=".svg"]'))
.filter(element => {
// Check if element has valid source
const source = element.src || element.data;
return source && source.trim() !== '';
})
.map(element => ({
type: 'external',
url: element.src || element.data,
element: element.outerHTML
}));
// Combine both types and filter out any undefined entries
const allSVGs = [
...inlineSVGs.map(svg => ({ type: 'inline', svg })),
...externalSVGs
].filter(Boolean);
sendResponse(allSVGs);
} else if (request.action === "copySVGFromContext") {
this.handleContextMenuCopy(request.target, request.srcUrl);
}
return true;
});
}
async handleContextMenuCopy(targetId, srcUrl) {
try {
let svgString = '';
// Handle SVG image elements
if (srcUrl && srcUrl.toLowerCase().endsWith('.svg')) {
const response = await fetch(srcUrl);
svgString = await response.text();
} else {
// Handle inline SVGs
const element = targetId ? document.querySelector(`[data-contextmenu-element-id="${targetId}"]`) : null;
const svgElement = element?.closest('svg') || element?.querySelector('svg');
if (svgElement) {
svgString = svgElement.outerHTML;
} else {
// Fallback: try to find SVG in img elements
const imgElement = element?.closest('img[src$=".svg"]') || document.querySelector('img[src$=".svg"]');
if (imgElement) {
const response = await fetch(imgElement.src);
svgString = await response.text();
}
}
}
if (svgString && svgString.includes('<svg')) {
await navigator.clipboard.writeText(svgString);
this.showCopyFeedback();
}
} catch (error) {
console.error('Failed to copy SVG:', error);
this.showErrorFeedback();
}
}
showCopyFeedback() {
// Create and show a temporary feedback tooltip
const feedback = document.createElement('div');
feedback.textContent = 'SVG Copied!';
feedback.style.cssText = `
position: fixed;
bottom: 20px;
right: 20px;
background: #4CAF50;
color: white;
padding: 8px 16px;
border-radius: 16px;
z-index: 999999;
font-family: system-ui;
box-shadow: 0 2px 4px rgba(0,0,0,0.2);
animation: fadeInOut 2s ease-in-out forwards;
`;
// Add animation keyframes
const style = document.createElement('style');
style.textContent = `
@keyframes fadeInOut {
0% { opacity: 0; transform: translateY(20px); }
20% { opacity: 1; transform: translateY(0); }
80% { opacity: 1; transform: translateY(0); }
100% { opacity: 0; transform: translateY(-20px); }
}
`;
document.head.appendChild(style);
document.body.appendChild(feedback);
// Remove feedback after animation
setTimeout(() => {
feedback.remove();
style.remove();
}, 2000);
}
showErrorFeedback() {
const feedback = document.createElement('div');
feedback.textContent = 'Failed to copy SVG';
feedback.style.cssText = `
position: fixed;
bottom: 20px;
right: 20px;
background: #f44336;
color: white;
padding: 8px 16px;
border-radius: 4px;
z-index: 999999;
font-family: system-ui;
box-shadow: 0 2px 4px rgba(0,0,0,0.2);
animation: fadeInOut 2s ease-in-out forwards;
`;
document.body.appendChild(feedback);
setTimeout(() => feedback.remove(), 2000);
}
findSVGs() {
const svgs = [];
// Find inline SVGs
document.querySelectorAll("svg").forEach((svg) => {
svgs.push(svg.outerHTML);
});
// Find SVGs in use elements
document.querySelectorAll("use").forEach((use) => {
const href = use.getAttribute("href") || use.getAttribute("xlink:href");
if (href && href.startsWith("#")) {
const id = href.slice(1);
const referencedSvg = document.getElementById(id);
if (referencedSvg && referencedSvg.tagName.toLowerCase() === "svg") {
svgs.push(referencedSvg.outerHTML);
}
}
});
// Find background SVGs
this.findBackgroundSVGs(document.body, svgs);
return [...new Set(svgs)]; // Remove duplicates
}
findBackgroundSVGs(element, svgs) {
const style = window.getComputedStyle(element);
const backgroundImage = style.backgroundImage;
if (backgroundImage.includes("svg")) {
const url = backgroundImage.match(/url\(['"]?(.*?)['"]?\)/)?.[1];
if (url) {
fetch(url)
.then((response) => response.text())
.then((svgContent) => {
if (svgContent.includes("<svg")) {
svgs.push(svgContent);
}
})
.catch(() => {});
}
}
Array.from(element.children).forEach((child) => {
this.findBackgroundSVGs(child, svgs);
});
}
}
new SVGDetector();