-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
64 lines (55 loc) · 2.26 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
function retrievePgpKeyFromEmail(email) {
// Send a get request to "https://keys.openpgp.org/vks/v1/by-email/" + email
// If the response is 200, return the pgp key
// If the response is 404, return null
// Do request
var request = new XMLHttpRequest();
request.open("GET", "https://keys.openpgp.org/vks/v1/by-email/" + email, false);
request.send(null);
console.log(request);
// Check response
if (request.status == 200) {
// Return the pgp key
return request.responseText;
} else if (request.status == 404) {
// Return null
return null;
}
return null;
}
function addPgpIcon(parent, email, pgpKey) {
// Create a DOM element to display an icon (🔒) and copy the pgp key to the clipboard when clicked
var pgpIcon = document.createElement("a");
pgpIcon.setAttribute("href", "#");
// Set class to "pgp-email-lookup-icon"
pgpIcon.setAttribute("class", "pgp-email-lookup-icon");
pgpIcon.setAttribute("data-key", pgpKey);
pgpIcon.innerHTML = "🔒";
// Add the DOM element after the parent
parent.parentNode.insertBefore(pgpIcon, parent.nextSibling);
// Add an event listener to the DOM element
pgpIcon.addEventListener("click", function() {
// Copy the pgp key to the clipboard
navigator.clipboard.writeText(this.getAttribute("data-key"));
// Send message to background.js
chrome.runtime.sendMessage({id: "pgpKeyCopied", email: email, pgpKey: pgpKey});
});
}
// Remove all DOM elements with the class "pgp-email-lookup-icon"
var listOfPgpIcons = document.querySelectorAll(".pgp-email-lookup-icon");
for (var i = 0; i < listOfPgpIcons.length; i++) {
listOfPgpIcons[i].parentNode.removeChild(listOfPgpIcons[i]);
}
// Retrieve a list of all DOM elements that contain an email address
var listOfEmails = document.querySelectorAll("[href^='mailto:']");
// Loop through the list of email addresses
for (var i = 0; i < listOfEmails.length; i++) {
// Retrieve the email address
var email = listOfEmails[i].href.substring(7);
// Retrieve the pgp key
var pgpKey = retrievePgpKeyFromEmail(email);
// If the pgp key is not null, display it
if (pgpKey != null) {
addPgpIcon(listOfEmails[i], email, pgpKey);
}
}