-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
66 lines (61 loc) · 1.83 KB
/
background.js
File metadata and controls
66 lines (61 loc) · 1.83 KB
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
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: "blockFace",
title: "Block this face forever",
contexts: ["image"],
});
});
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
if (info.menuItemId === "blockFace" && info.srcUrl) {
try {
// Fetch the image as a blob (bypasses CORS)
const response = await fetch(info.srcUrl);
const blob = await response.blob();
// Convert blob to base64 data URL
const reader = new FileReader();
reader.onloadend = () => {
const dataUrl = reader.result;
console.log("Image fetched and converted to data URL");
chrome.tabs.sendMessage(tab.id, {
type: "BLOCK_FACE",
src: dataUrl,
originalSrc: info.srcUrl,
});
};
reader.readAsDataURL(blob);
} catch (e) {
console.error("Failed to fetch image:", e);
// Fallback to original URL
chrome.tabs.sendMessage(tab.id, {
type: "BLOCK_FACE",
src: info.srcUrl,
});
}
}
});
// Handle image fetch requests from content script
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
if (msg.type === "FETCH_IMAGE" && msg.url) {
fetchImageAsDataUrl(msg.url).then(sendResponse);
return true; // Keep channel open for async response
}
});
async function fetchImageAsDataUrl(url) {
try {
const response = await fetch(url);
const blob = await response.blob();
return new Promise((resolve) => {
const reader = new FileReader();
reader.onloadend = () => {
resolve({ dataUrl: reader.result });
};
reader.onerror = () => {
resolve({ dataUrl: null });
};
reader.readAsDataURL(blob);
});
} catch (e) {
console.error("Failed to fetch image:", url, e);
return { dataUrl: null };
}
}