-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
193 lines (167 loc) · 6.43 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"use strict";
// Open welcome screen when installing addon.
browser.runtime.onInstalled.addListener(function (details) {
if (details.reason == "install" || details.reason == "update") {
browser.storage.sync.get("username").then(function (storage) {
if (!("username" in storage) || !storage["username"]) {
var options = {"url": "options/welcome.html",
"width": 750, "height": 360, "type": "popup"};
browser.windows.create(options).then(function (welcomeWindow) {
browser.windows.onRemoved.addListener(function handler(id) {
if (id == welcomeWindow.id) {
browser.windows.onRemoved.removeListener(handler);
browser.runtime.openOptionsPage();
}
});
});
}
});
}
});
browser.menus.create({
id: "paste-email",
contexts: ["editable"],
title: browser.i18n.getMessage("menuPasteAddress")
});
function openCreateAddress(parent_tab, frameId) {
var options = {"url": "../create-address/create-address.html",
"type": "popup", "width": 750, "height": 490};
browser.windows.create(options).then(function (window) {
// (FF 56) Security policy blocks running code until tab has completed loading.
browser.tabs.onUpdated.addListener(function handler(tabId, changeInfo, tab) {
if (tabId == window.tabs[0].id && changeInfo.status == "complete") {
browser.tabs.onUpdated.removeListener(handler);
// Send the parent url and window ID through to the new window.
browser.tabs.sendMessage(
tab.id, [parent_tab.url, parent_tab.windowId, parent_tab.id, frameId]);
}
});
});
}
browser.menus.onClicked.addListener(function(info, parent_tab) {
if (info.menuItemId == "paste-email") {
openCreateAddress(parent_tab, info.frameId);
} else {
// Paste previous email.
browser.tabs.sendMessage(parent_tab.id, info.menuItemId,
{"frameId": info.frameId});
}
});
// Open create address window on keyboard shortcut.
browser.commands.onCommand.addListener(function (command) {
browser.tabs.query({
currentWindow: true,
active: true
}).then(function (tabs) {
// This won't work if user is inside an iframe (as we don't have the frame ID).
browser.tabs.sendMessage(tabs[0].id, "check_editable").then(function (is_editable) {
if (is_editable)
openCreateAddress(tabs[0], 0);
});
});
});
/**
* Paste previous address context menus.
*/
// Track current status of previous address menus.
var current_domain = "";
var previous_address_menus = [];
var previous_addresses = {};
browser.storage.local.get("previous_addresses").then(function (addresses) {
previous_addresses = addresses;
});
browser.storage.onChanged.addListener(function (changes, area) {
if ("previous_addresses" in changes) {
previous_addresses = changes["previous_addresses"].newValue;
current_domain = "*invalid*"; // Force context menu to reload.
}
});
// Update the currently displayed previous addresses context menu items.
browser.menus.onShown.addListener(function (info, tab) {
if (!info.editable)
return;
let domain = (new URL(tab.url)).hostname;
if (domain == current_domain)
return;
current_domain = domain;
// Remove previous menu items.
for (const id of previous_address_menus)
browser.menus.remove(id);
previous_address_menus = [];
if (!current_domain) {
// Refresh in case any menu items have been removed.
browser.menus.refresh();
return;
}
// Add any new ones for this domain.
let addresses = [];
let p = current_domain.length;
while (p >= 0) {
p = current_domain.lastIndexOf(".", p - 1);
let domain = current_domain.slice(p + 1);
if (domain in previous_addresses) {
addresses = previous_addresses[domain];
break;
}
}
for (let [email, url] of addresses) {
url = new URL(url);
let url_detail = current_domain == url.hostname ? url.pathname : url.hostname;
let id = browser.menus.create({
id: email,
contexts: ["editable"],
title: browser.i18n.getMessage("menuPastePrevious", email) + " (" + url_detail + ")"
});
previous_address_menus.push(id);
}
browser.menus.refresh();
});
// Update some settings each time the addon is loaded.
browser.storage.sync.get(["username", "password"]).then(function (storage) {
let data = {
"cmd": "login",
"fe-login-user": storage["username"],
"fe-login-pass": storage["password"]
};
return callAPI(data);
}).then(function (login) {
// Update list of domains and real emails.
browser.storage.local.set({
"domains": login["domain_name_list"],
"real_emails": Object.keys(login["real_email_list"])
});
let data = {
"cmd": "read_dea",
"session_id": login["session_id"]
};
// Load public suffix data from file as needed.
let suffixes = fetch(browser.runtime.getURL("public_suffix.json")).then(function (response) {
if (response.ok)
return response.json();
});
return Promise.all([callAPI(data), suffixes]);
}).then(function (values) {
// Update local storage of existing disposable addresses.
let current_prev_addresses = {};
let [addresses, [rules, exceptions]] = values;
for (const address of addresses) {
if (address["website"]) {
try {
var domain = new URL(address["website"]);
} catch (e) {
if (e instanceof TypeError)
continue; // Not a valid URL.
throw e;
}
domain = org_domain(domain, rules, exceptions);
let email = [address["disposable_name"] + "@" + address["disposable_domain"],
address["website"]];
if (domain in current_prev_addresses)
current_prev_addresses[domain].push(email);
else
current_prev_addresses[domain] = [email];
}
}
previous_addresses = current_prev_addresses;
browser.storage.local.set({"previous_addresses": current_prev_addresses});
});