-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbackground.js
More file actions
335 lines (285 loc) · 9.67 KB
/
Copy pathbackground.js
File metadata and controls
335 lines (285 loc) · 9.67 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Password Hasher Plus
*
* The Initial Developer of the Original Code is Eric Woodruff.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s): (none)
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
var portsByTabId = new Array();
var menuItemLengths = new Object();
chrome.extension.onConnect.addListener (function (port) {
console.assert(port.name == "passhashultimate");
port.onMessage.addListener (function (message) {
// Add the tab port to the collection and send back the config for that site
if (message.type == "init") {
portsByTabId[port.tab.id] = port;
port.siteName = getSiteName(port.tab.url);
port.postMessage({
type: "updateConfig",
config: loadConfig(port.siteName)
});
}
// Create the context menus for the given tab based on its configuration
else if (message.type == "addContextMenus") {
chrome.contextMenus.removeAll();
addContextMenus(loadConfig(port.siteName), message.fieldId, message.show);
}
// Kill all the context menus
else if (message.type == "removeContextMenus")
chrome.contextMenus.removeAll();
});
port.onDisconnect.addListener (function (event) {
portsByTabId.splice(port.tab.id, 1);
console.log("Removed " + port.tab.id);
});
});
/**
* Creates the context menus for the given config.
* @param {Object} config The site configuration that the context menus should be based on.
* @param {String} fieldId The identifier of the field that the user is focused on when they right-clicked
* to bring up the context menu.
* @param {Boolean} show Flag indicating whether or not the user has chosen to show the value of the focused
* password field.
*/
function addContextMenus(config, fieldId, show) {
// Create the various context menu entries based on the configuration values
var topLevelMenu = chrome.contextMenus.create({
title: "Password hashing",
contexts: ["all"]
});
chrome.contextMenus.create({
title: "Enabled",
type: "checkbox",
contexts: ["all"],
checked: config.fields.indexOf(fieldId) != -1,
parentId: topLevelMenu,
onclick: function (info, tab) {
var port = portsByTabId[tab.id];
var config = loadConfig(port.siteName);
if (info.checked && config.fields.indexOf(fieldId) == -1)
config.fields.push(fieldId);
else if (!info.checked && config.fields.indexOf(fieldId) != -1)
config.fields.splice(config.fields.indexOf(fieldId), 1);
saveConfig(port.siteName, config, tab.id);
}
});
chrome.contextMenus.create({
title: "Use compatibility mode",
type: "checkbox",
contexts: ["all"],
checked: config.compatibilityMode,
parentId: topLevelMenu,
onclick: function (info, tab) {
var port = portsByTabId[tab.id];
var config = loadConfig(port.siteName);
config.compatibilityMode = info.checked;
saveConfig(port.siteName, config, tab.id);
}
});
chrome.contextMenus.create({
type: "separator",
contexts: ["all"],
parentId: topLevelMenu
});
chrome.contextMenus.create({
title: "Include digits",
type: "checkbox",
checked: config.includeDigits,
contexts: ["all"],
parentId: topLevelMenu,
onclick : function (info, tab) {
var port = portsByTabId[tab.id];
var config = loadConfig(port.siteName);
config.includeDigits = info.checked;
saveConfig(port.siteName, config, tab.id);
}
});
chrome.contextMenus.create({
title: "Include punctuation",
type: "checkbox",
checked: config.includePunctuation,
contexts: ["all"],
parentId: topLevelMenu,
onclick : function (info, tab) {
var port = portsByTabId[tab.id];
var config = loadConfig(port.siteName);
config.includePunctuation = info.checked;
saveConfig(port.siteName, config, tab.id);
}
});
chrome.contextMenus.create({
title: "Include mixed case",
type: "checkbox",
checked: config.includeMixedCase,
contexts: ["all"],
parentId: topLevelMenu,
onclick : function (info, tab) {
var port = portsByTabId[tab.id];
var config = loadConfig(port.siteName);
config.includeMixedCase = info.checked;
saveConfig(port.siteName, config, tab.id);
}
});
chrome.contextMenus.create({
title: "No special characters",
type: "checkbox",
checked: config.noSpecialCharacters,
contexts: ["all"],
parentId: topLevelMenu,
onclick : function (info, tab) {
var port = portsByTabId[tab.id];
var config = loadConfig(port.siteName);
config.noSpecialCharacters = info.checked;
saveConfig(port.siteName, config, tab.id);
}
});
chrome.contextMenus.create({
title: "Digits only",
type: "checkbox",
contexts: ["all"],
checked: config.digitsOnly,
parentId: topLevelMenu,
onclick : function (info, tab) {
var port = portsByTabId[tab.id];
var config = loadConfig(port.siteName);
config.digitsOnly = info.checked;
saveConfig(port.siteName, config, tab.id);
}
});
chrome.contextMenus.create({
type: "separator",
contexts: ["all"],
parentId: topLevelMenu
});
chrome.contextMenus.create({
title: "Bump version",
contexts: ["all"],
parentId: topLevelMenu,
onclick: function (info, tab) {
var port = portsByTabId[tab.id];
var config = loadConfig(port.siteName);
config.version++;
saveConfig(port.siteName, config, tab.id);
}
});
chrome.contextMenus.create({
title: show ? "Hide value" : "Show value",
contexts: ["all"],
parentId: topLevelMenu,
onclick: function (info, tab) {
var port = portsByTabId[tab.id];
port.postMessage({
type: "showHideValue"
});
}
});
var lengthMenu = chrome.contextMenus.create({
title: "Length",
contexts: ["all"],
parentId: topLevelMenu
});
// Create the submenu items for the size of the generated password in a loop
menuItemLengths = new Object();
for (var i = 4; i <= 26; i += 2) {
var newMenuItemId = chrome.contextMenus.create({
title: i.toString(),
type: "radio",
contexts: ["all"],
checked: config.size == i,
parentId: lengthMenu,
onclick: function (info, tab) {
var port = portsByTabId[tab.id];
var config = loadConfig(port.siteName);
config.size = menuItemLengths[info.menuItemId];
saveConfig(port.siteName, config, tab.id);
}
});
menuItemLengths[newMenuItemId] = i;
}
}
/**
* Loads the configuration for the given site name.
* @param {String} siteName The site name whose configuration we are to look up.
* @returns {Object} Configuration from storage matching the given site name if it exists, the default config otherwise.
*/
function loadConfig(siteName) {
console.log("Loading config for " + siteName);
// Try to load the config from storage
var seed = getSeed();
var config = localStorage.getObject("siteName:" + siteName);
// Use the default config if it doesn't exist
if (config == null)
config = getDefaultConfig();
// Set the private key to the value set by the user in the options screen
config.siteName = siteName;
config.seed = seed;
// Initialize any missing properties
if (typeof config.fields == "undefined")
config.fields = new Array();
if (typeof config.version == "undefined")
config.version = 1;
return config;
}
/**
* Generates the short site name identifier for the given url (i.e. google from https://mail.google.com).
* @param {String} url URL for which we are to generate the site name.
* @returns {String} Site name for the given URL.
*/
function getSiteName(url) {
console.log("Getting site name for " + url);
var regEx = new RegExp ("^https?://([^:/]+\\.)?([^.:/]+)\\.([a-z]{2,5})(:\\d+)?/.*$");
var components = regEx.exec(url);
try {
if (components[2] == "")
throw "chrome";
// Return the component immediately before the TLD
return components[2];
}
// If the URL doesn't match our regex, just return a placeholder value
catch (e) {
return "chrome";
}
}
/**
* Saves the config for the given site to storage.
* @param {String} siteName The name of the site for which we are saving the config.
* @param {Object} config Configuration that is being persisted to storage.
* @param {Integer} tabId Identifier of the tab for which we are saving this config.
*/
function saveConfig(siteName, config, tabId) {
console.log("Saving config for " + siteName);
localStorage.setObject("siteName:" + siteName, config);
// Post a message back to the tab indicating whe have refreshed the config
if (tabId != null)
portsByTabId[tabId].postMessage({
type: "updateConfig",
config: config
});
}