-
Notifications
You must be signed in to change notification settings - Fork 1
/
mailto.js
54 lines (48 loc) · 1.54 KB
/
mailto.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
/**
* @fileoverview Rewrites the mailto links on the current page
* to Gmail Compose links.
*/
var toField = "&to=";
var cachedGmailUrl = "";
function rewriteMailtoToGMailUrl(inUrl) {
var retUrl = inUrl;
retUrl = retUrl.replace("?", "&");
retUrl = retUrl.replace(/subject=/i, "su=");
retUrl = retUrl.replace(/CC=/i, "cc=");
retUrl = retUrl.replace(/BCC=/i, "bcc=");
retUrl = retUrl.replace(/Body=/i, "body=");
var gmailUrl = cachedGmailUrl + toField;
retUrl = retUrl.replace("mailto:", gmailUrl);
return retUrl;
}
// Content Scripts
function rewriteMailtosOnPage() {
// Find all the mailto links.
var result = document.evaluate(
'//a[contains(@href, "mailto:")]',
document, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);
var item;
var nodes = [];
// cannot change the NODE_ITERATOR nodes' attributes in this loop itself
// since iterateNext will invalidate the state; Need to store temporarily.
while (item = result.iterateNext()) {
nodes.push(item);
}
for (var i = 0; i < nodes.length; i++) {
var mailtoStr = nodes[i].getAttribute('href');
mailtoStr = rewriteMailtoToGMailUrl(mailtoStr);
nodes[i].setAttribute('href', mailtoStr);
nodes[i].setAttribute('target', "_blank");
}
}
if (cachedGmailUrl == "") {
var bgPort = chrome.extension.connect({name: "GmailUrlConn"});
bgPort.postMessage({req: "GmailUrlPlease"});
bgPort.onMessage.addListener(
function(msg) {
cachedGmailUrl = msg.gmailDomainUrl;
rewriteMailtosOnPage();
});
} else {
rewriteMailtosOnPage();
}