-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
60 lines (53 loc) · 1.63 KB
/
background.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
const handledUrls = new Set();
async function fetchAndBase64(url) {
const res = await fetch(url);
const blob = await res.blob();
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result);
reader.onerror = reject;
reader.readAsDataURL(blob);
});
}
chrome.webRequest.onBeforeRequest.addListener(
function (details) {
if (
details.url.includes("/rtig/image") &&
!handledUrls.has(details.url)
) {
handledUrls.add(details.url);
fetchAndBase64(details.url)
.then(base64 => {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
if (tabs[0]) {
chrome.tabs.sendMessage(tabs[0].id, {
type: "rtig-image",
url: details.url,
base64: base64
}, { frameId: details.frameId });
return true;
}
});
})
.catch(err => {
console.error("[ProCap] Error base64ing:", err);
});
}
},
{ urls: ["<all_urls>"], types: ["image", "xmlhttprequest", "other"] },
[]
);
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.type === "get-api-key") {
chrome.storage.local.get("apikey", (result) => {
sendResponse({ apikey: result.apikey });
});
return true;
}
if (request.type === "gfct-response") {
gfctResponse = request.data;
}
if (request.type === "get-gfct-response") {
sendResponse({ gfctResponse });
}
});