-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
138 lines (119 loc) · 3.79 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
const proxies = [
"https://corsproxy.io/?url=",
"https://cors-anywhere.herokuapp.com/",
"https://api.allorigins.win/get?url=",
"https://thingproxy.freeboard.io/fetch/"
];
let currentProxyIndex = 0;
let observer;
function startObserving() {
console.log('Starting to observe media changes');
observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
mutation.addedNodes.forEach(node => {
if (node.tagName === 'IMG') {
handleImage(node);
}
});
});
});
observer.observe(document.body, { childList: true, subtree: true });
console.log('Observer initialized and listening for DOM changes');
}
function stopObserving() {
if (observer) {
observer.disconnect();
console.log('Observer stopped');
}
}
function handleImage(img) {
console.log(`New media element detected: IMG`);
console.log(`Processing media element: IMG`);
if (!img.src.endsWith('.avif')) {
console.log(`Image is not an AVIF format: ${img.src}`);
return;
}
img.onload = () => {
console.log(`Image loaded: ${img.src}`);
clickAndConvert(img.src);
};
img.onerror = () => {
console.error(`Image load failed: ${img.src}`);
};
if (img.complete) {
img.onload();
} else {
img.src = img.src;
}
}
function clickAndConvert(imageUrl) {
console.log(`Clicking image to trigger full resolution: ${imageUrl}`);
tryFetching(imageUrl);
}
function tryFetching(url) {
if (currentProxyIndex >= proxies.length) {
console.error('All proxies failed');
return;
}
const proxy = proxies[currentProxyIndex];
const proxiedUrl = proxy + encodeURIComponent(url);
fetch(proxiedUrl)
.then(response => {
if (!response.ok) {
throw new Error(`Failed to fetch: ${response.statusText}`);
}
return response.blob();
})
.then(blob => {
const img = new Image();
img.onload = () => {
console.log('Image loaded for conversion');
convertAvifToJpeg(img);
};
img.onerror = () => {
console.error('Image load failed. Trying next proxy...');
currentProxyIndex++;
tryFetching(url);
};
img.src = URL.createObjectURL(blob);
})
.catch(error => {
console.error('Fetching failed:', error);
currentProxyIndex++;
tryFetching(url);
});
}
function convertAvifToJpeg(img) {
console.log('Converting AVIF image to JPEG...');
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.width = img.naturalWidth;
canvas.height = img.naturalHeight;
try {
ctx.drawImage(img, 0, 0);
const jpegUrl = canvas.toDataURL('image/jpeg');
console.log('Downloaded JPEG image:', jpegUrl);
const link = document.createElement('a');
link.href = jpegUrl;
link.download = `${generateRandomFilename()}.jpg`;
link.click();
} catch (error) {
console.error('Failed to convert image:', error);
}
}
function generateRandomFilename(length = 10) {
const characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
let result = '';
const charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'startObserving') {
startObserving();
} else if (message.action === 'stopObserving') {
stopObserving();
}
});