-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
148 lines (123 loc) · 4.55 KB
/
popup.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
document.getElementById("detectImages").addEventListener("click", async () => {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
chrome.scripting.executeScript(
{
target: { tabId: tab.id },
func: detectImages,
},
(results) => {
const images = results[0].result;
const imageList = document.getElementById("imageList");
imageList.innerHTML = "";
images.forEach((image) => {
const listItem = document.createElement("li");
// Preview image
// Önizleme resmi
const preview = document.createElement("img");
preview.src = image;
// Extract file name from URL
// URL'den dosya adını çıkar
const fileName = image.split("/").pop();
// Create a link for the URL
// URL için bir bağlantı oluştur
const link = document.createElement("a");
link.href = image;
link.textContent = fileName;
link.target = "_blank";
// Create a download button
// İndirme butonu oluştur
const downloadButton = document.createElement("button");
downloadButton.textContent = "Original";
downloadButton.addEventListener("click", () => downloadImage(image));
// Add an SVG to PNG conversion button if the file is SVG
// Dosya SVG ise, SVG'den PNG'ye dönüştürme butonu ekle
if (image.endsWith(".svg")) {
const convertButton = document.createElement("button");
convertButton.textContent = "Convert to PNG";
convertButton.classList.add("convert-to-svg");
convertButton.addEventListener("click", () => convertSvgToPng(image));
listItem.appendChild(convertButton);
}
// Add a PNG to SVG conversion button if the file is PNG
// Dosya PNG ise, PNG'den SVG'ye dönüştürme butonu ekle
if (image.endsWith(".png")) {
const convertButton = document.createElement("button");
convertButton.textContent = "Convert to SVG";
convertButton.classList.add("convert-to-png");
convertButton.addEventListener("click", () => convertToSVG(image));
listItem.appendChild(convertButton);
}
// Append elements to the list item
// Liste öğesine elemanları ekle
listItem.appendChild(preview);
listItem.appendChild(link);
listItem.appendChild(downloadButton);
imageList.appendChild(listItem);
});
}
);
});
// Function to detect all images on the current page
// Mevcut sayfadaki tüm resimleri algılayan işlev
function detectImages() {
const images = Array.from(document.images).map((img) => img.src);
return images;
}
// Function to download an image
// Bir resmi indirme işlevi
function downloadImage(url) {
chrome.downloads.download({
url: url,
filename: url.split("/").pop(),
});
}
// Function to convert PNG to SVG
// PNG'den SVG'ye dönüştürme işlevi
async function convertToSVG(imageUrl) {
const response = await fetch(imageUrl);
const blob = await response.blob();
const img = new Image();
img.onload = () => {
const canvas = document.createElement("canvas");
const context = canvas.getContext("2d");
canvas.width = img.width;
canvas.height = img.height;
context.drawImage(img, 0, 0);
const svgData = `
<svg xmlns="http://www.w3.org/2000/svg" width="${img.width}" height="${img.height}">
<image href="${canvas.toDataURL()}" width="${img.width}" height="${img.height}" />
</svg>
`;
const blob = new Blob([svgData], { type: "image/svg+xml;charset=utf-8" });
const svgUrl = URL.createObjectURL(blob);
chrome.downloads.download({
url: svgUrl,
filename: "converted_image.svg",
});
};
img.src = URL.createObjectURL(blob);
}
// Function to convert SVG to PNG
// SVG'den PNG'ye dönüştürme işlevi
async function convertSvgToPng(imageUrl) {
const response = await fetch(imageUrl);
const svgText = await response.text();
const img = new Image();
const svgBlob = new Blob([svgText], { type: "image/svg+xml;charset=utf-8" });
const svgUrl = URL.createObjectURL(svgBlob);
img.onload = () => {
const canvas = document.createElement("canvas");
const context = canvas.getContext("2d");
canvas.width = img.width;
canvas.height = img.height;
context.drawImage(img, 0, 0);
canvas.toBlob((blob) => {
const pngUrl = URL.createObjectURL(blob);
chrome.downloads.download({
url: pngUrl,
filename: "converted_image.png",
});
});
};
img.src = svgUrl;
}