-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
32 lines (30 loc) · 1.09 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
"use strict";
/**
* Convert an ArrayBuffer to a base64-encoded string.
*
* @param {ArrayBuffer} buffer ArrayBuffer containing data to encode.
* @return {String} Base64-encoded string.
*/
function bufToBase64(buffer) {
return btoa(String.fromCharCode(...new Uint8Array(buffer)));
}
// Although the documentation has a warning not to use an async function, it's
// perfectly fine for our use case since we always want to send a response in
// this script.
browser.runtime.onMessage.addListener(async (message, sender) => {
try {
const url = new URL(message.url, sender.url);
// For security, make sure the requested domain is valid
if (!url.hostname.endsWith('.thumbs.redditmedia.com')) {
throw new Error("Invalid domain");
}
// Also make sure to use HTTPS
url.protocol = 'https:';
const hash = await fetchImageAndGetHash(
url.href, message.hashFunction);
return {hash: bufToBase64(hash)};
} catch (error) {
console.warn(message, error);
return {error: "Failed to get image hash"};
}
});