2016-09-28 04:44:42 +02:00
|
|
|
var isBackground = true;
|
2016-12-30 08:09:54 +01:00
|
|
|
var loginsToAdd = [];
|
2016-10-19 00:56:40 +02:00
|
|
|
var i18nService = new i18nService();
|
2016-10-25 04:16:47 +02:00
|
|
|
var constantsService = new ConstantsService();
|
2016-09-21 07:17:46 +02:00
|
|
|
var utilsService = new UtilsService();
|
2016-10-25 04:16:47 +02:00
|
|
|
var cryptoService = new CryptoService(constantsService);
|
2016-09-03 06:03:13 +02:00
|
|
|
var tokenService = new TokenService();
|
2017-01-26 04:57:32 +01:00
|
|
|
var appIdService = new AppIdService();
|
|
|
|
var apiService = new ApiService(tokenService, appIdService, utilsService, logout);
|
2016-09-21 05:30:16 +02:00
|
|
|
var userService = new UserService(tokenService, apiService, cryptoService);
|
2017-01-14 17:20:44 +01:00
|
|
|
var settingsService = new SettingsService(userService);
|
2017-01-14 19:43:59 +01:00
|
|
|
var loginService = new LoginService(cryptoService, userService, apiService, settingsService);
|
|
|
|
var folderService = new FolderService(cryptoService, userService, apiService);
|
2017-01-14 17:20:44 +01:00
|
|
|
var syncService = new SyncService(loginService, folderService, userService, apiService, settingsService);
|
2016-09-17 06:00:17 +02:00
|
|
|
var autofillService = new AutofillService();
|
2016-09-18 04:57:51 +02:00
|
|
|
var passwordGenerationService = new PasswordGenerationService();
|
2016-09-16 05:24:45 +02:00
|
|
|
|
2016-10-08 22:37:50 +02:00
|
|
|
chrome.commands.onCommand.addListener(function (command) {
|
|
|
|
if (command === 'generate_password') {
|
2016-10-09 00:09:38 +02:00
|
|
|
ga('send', {
|
|
|
|
hitType: 'event',
|
|
|
|
eventAction: 'Generated Password From Command'
|
|
|
|
});
|
2016-10-08 22:37:50 +02:00
|
|
|
passwordGenerationService.getOptions().then(function (options) {
|
|
|
|
var password = passwordGenerationService.generatePassword(options);
|
|
|
|
copyToClipboard(password);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-01-21 18:39:37 +01:00
|
|
|
var loginToAutoFill = null,
|
2016-11-26 04:01:46 +01:00
|
|
|
pageDetailsToAutoFill = [],
|
2017-01-21 05:20:09 +01:00
|
|
|
autofillTimeout = null,
|
|
|
|
menuOptionsLoaded = [];
|
2016-11-26 04:01:46 +01:00
|
|
|
|
2016-09-29 01:12:14 +02:00
|
|
|
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
|
2017-01-19 06:21:20 +01:00
|
|
|
if (msg.command === 'loggedIn' || msg.command === 'unlocked' || msg.command === 'locked') {
|
2016-10-26 05:17:46 +02:00
|
|
|
setIcon();
|
2016-09-29 01:12:14 +02:00
|
|
|
refreshBadgeAndMenu();
|
|
|
|
}
|
2017-01-19 06:21:20 +01:00
|
|
|
else if (msg.command === 'logout') {
|
2017-01-22 02:00:25 +01:00
|
|
|
logout(msg.expired, function () { });
|
2017-01-19 06:21:20 +01:00
|
|
|
}
|
2016-09-29 01:12:14 +02:00
|
|
|
else if (msg.command === 'syncCompleted' && msg.successfully) {
|
|
|
|
setTimeout(refreshBadgeAndMenu, 2000);
|
|
|
|
}
|
2016-09-29 06:14:15 +02:00
|
|
|
else if (msg.command === 'bgOpenOverlayPopup') {
|
|
|
|
messageCurrentTab('openOverlayPopup', msg.data);
|
|
|
|
}
|
|
|
|
else if (msg.command === 'bgCloseOverlayPopup') {
|
|
|
|
messageCurrentTab('closeOverlayPopup');
|
|
|
|
}
|
2016-12-30 00:35:41 +01:00
|
|
|
else if (msg.command === 'bgOpenNotificationBar') {
|
2016-12-30 20:25:57 +01:00
|
|
|
messageTab(sender.tab.id, 'openNotificationBar', msg.data);
|
2016-12-30 00:35:41 +01:00
|
|
|
}
|
|
|
|
else if (msg.command === 'bgCloseNotificationBar') {
|
2016-12-30 20:25:57 +01:00
|
|
|
messageTab(sender.tab.id, 'closeNotificationBar');
|
2016-12-30 00:35:41 +01:00
|
|
|
}
|
2016-12-30 08:09:54 +01:00
|
|
|
else if (msg.command === 'bgCollectPageDetails') {
|
|
|
|
collectPageDetailsForContentScript(sender.tab);
|
|
|
|
}
|
|
|
|
else if (msg.command === 'bgAddLogin') {
|
|
|
|
addLogin(msg.login, sender.tab);
|
|
|
|
}
|
2016-12-30 20:25:57 +01:00
|
|
|
else if (msg.command === 'bgAddClose') {
|
|
|
|
removeAddLogin(sender.tab);
|
|
|
|
}
|
|
|
|
else if (msg.command === 'bgAddSave') {
|
|
|
|
saveAddLogin(sender.tab);
|
|
|
|
}
|
2016-11-26 04:01:46 +01:00
|
|
|
else if (msg.command === 'collectPageDetailsResponse') {
|
2016-12-30 08:09:54 +01:00
|
|
|
if (msg.contentScript) {
|
|
|
|
var forms = autofillService.getFormsWithPasswordFields(msg.details);
|
|
|
|
messageTab(msg.tabId, 'pageDetails', { details: msg.details, forms: forms });
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
clearTimeout(autofillTimeout);
|
|
|
|
pageDetailsToAutoFill.push({ frameId: sender.frameId, tabId: msg.tabId, details: msg.details });
|
|
|
|
autofillTimeout = setTimeout(autofillPage, 300);
|
|
|
|
}
|
2016-11-26 04:01:46 +01:00
|
|
|
}
|
2016-09-28 05:36:55 +02:00
|
|
|
});
|
|
|
|
|
2016-10-26 05:17:46 +02:00
|
|
|
setIcon();
|
|
|
|
function setIcon() {
|
|
|
|
userService.isAuthenticated(function (isAuthenticated) {
|
|
|
|
cryptoService.getKey(false, function (key) {
|
|
|
|
var suffix = '';
|
|
|
|
if (!isAuthenticated) {
|
|
|
|
suffix = '_gray';
|
|
|
|
}
|
|
|
|
else if (!key) {
|
|
|
|
suffix = '_locked';
|
|
|
|
}
|
2016-09-29 01:12:14 +02:00
|
|
|
|
2016-10-26 05:17:46 +02:00
|
|
|
chrome.browserAction.setIcon({
|
|
|
|
path: {
|
|
|
|
'19': 'images/icon19' + suffix + '.png',
|
|
|
|
'38': 'images/icon38' + suffix + '.png',
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2016-09-29 01:12:14 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chrome.runtime.onInstalled) {
|
|
|
|
chrome.runtime.onInstalled.addListener(function (details) {
|
|
|
|
ga('send', {
|
|
|
|
hitType: 'event',
|
|
|
|
eventAction: 'onInstalled ' + details.reason
|
|
|
|
});
|
2016-11-17 06:42:20 +01:00
|
|
|
|
|
|
|
if (details.reason === 'install') {
|
|
|
|
chrome.tabs.create({ url: 'https://bitwarden.com/browser-start/' }, function (tab) { });
|
|
|
|
}
|
2016-09-29 01:12:14 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-01-21 04:32:21 +01:00
|
|
|
var buildingContextMenu = false;
|
2016-10-26 05:03:21 +02:00
|
|
|
function buildContextMenu(callback) {
|
2017-01-21 04:32:21 +01:00
|
|
|
if (buildingContextMenu) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
buildingContextMenu = true;
|
|
|
|
|
2016-10-26 05:03:21 +02:00
|
|
|
chrome.contextMenus.removeAll(function () {
|
|
|
|
chrome.contextMenus.create({
|
|
|
|
type: 'normal',
|
2016-11-25 05:54:22 +01:00
|
|
|
id: 'root',
|
2016-10-26 05:03:21 +02:00
|
|
|
contexts: ['all'],
|
2016-11-25 05:54:22 +01:00
|
|
|
title: 'bitwarden'
|
2016-10-26 05:03:21 +02:00
|
|
|
}, function () {
|
|
|
|
chrome.contextMenus.create({
|
|
|
|
type: 'normal',
|
2016-11-25 05:54:22 +01:00
|
|
|
id: 'autofill',
|
|
|
|
parentId: 'root',
|
2016-10-26 05:03:21 +02:00
|
|
|
contexts: ['all'],
|
2016-11-25 05:54:22 +01:00
|
|
|
title: i18nService.autoFill
|
2016-10-26 05:03:21 +02:00
|
|
|
}, function () {
|
2017-01-21 05:20:09 +01:00
|
|
|
if (utilsService.isFirefox()) {
|
|
|
|
// Firefox does not support writing to the clipboard from background
|
|
|
|
buildingContextMenu = false;
|
|
|
|
if (callback) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-26 05:03:21 +02:00
|
|
|
chrome.contextMenus.create({
|
|
|
|
type: 'normal',
|
2016-11-25 05:54:22 +01:00
|
|
|
id: 'copy-username',
|
|
|
|
parentId: 'root',
|
2016-10-26 05:03:21 +02:00
|
|
|
contexts: ['all'],
|
2016-11-25 05:54:22 +01:00
|
|
|
title: i18nService.copyUsername
|
2016-10-26 05:03:21 +02:00
|
|
|
}, function () {
|
|
|
|
chrome.contextMenus.create({
|
|
|
|
type: 'normal',
|
2016-11-25 05:54:22 +01:00
|
|
|
id: 'copy-password',
|
|
|
|
parentId: 'root',
|
2016-10-26 05:03:21 +02:00
|
|
|
contexts: ['all'],
|
2016-11-25 05:54:22 +01:00
|
|
|
title: i18nService.copyPassword
|
2016-10-26 05:03:21 +02:00
|
|
|
}, function () {
|
2016-11-25 05:54:22 +01:00
|
|
|
chrome.contextMenus.create({
|
|
|
|
type: 'separator',
|
|
|
|
parentId: 'root'
|
|
|
|
});
|
|
|
|
|
|
|
|
chrome.contextMenus.create({
|
|
|
|
type: 'normal',
|
|
|
|
id: 'generate-password',
|
|
|
|
parentId: 'root',
|
|
|
|
contexts: ['all'],
|
|
|
|
title: i18nService.generatePasswordCopied
|
|
|
|
}, function () {
|
2017-01-21 04:32:21 +01:00
|
|
|
buildingContextMenu = false;
|
2016-11-25 05:54:22 +01:00
|
|
|
if (callback) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
});
|
2016-10-26 05:03:21 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-10-09 00:09:38 +02:00
|
|
|
});
|
2016-09-17 02:20:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
chrome.tabs.onActivated.addListener(function (activeInfo) {
|
2016-12-30 08:09:54 +01:00
|
|
|
checkLoginsToAdd();
|
2016-09-29 01:12:14 +02:00
|
|
|
refreshBadgeAndMenu();
|
2016-09-17 02:20:02 +02:00
|
|
|
});
|
|
|
|
|
2016-10-26 05:03:21 +02:00
|
|
|
var onReplacedRan = false;
|
2016-09-23 04:04:50 +02:00
|
|
|
chrome.tabs.onReplaced.addListener(function (addedTabId, removedTabId) {
|
2016-10-26 05:03:21 +02:00
|
|
|
if (onReplacedRan) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
onReplacedRan = true;
|
2016-12-30 08:09:54 +01:00
|
|
|
checkLoginsToAdd();
|
2016-10-26 05:03:21 +02:00
|
|
|
refreshBadgeAndMenu();
|
|
|
|
});
|
|
|
|
|
|
|
|
var onUpdatedRan = false;
|
|
|
|
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
|
|
|
|
if (onUpdatedRan) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
onUpdatedRan = true;
|
2016-12-30 08:09:54 +01:00
|
|
|
checkLoginsToAdd();
|
2016-09-29 01:12:14 +02:00
|
|
|
refreshBadgeAndMenu();
|
|
|
|
});
|
|
|
|
|
2017-01-21 04:32:21 +01:00
|
|
|
chrome.windows.onFocusChanged.addListener(function (windowId) {
|
2017-01-21 05:20:09 +01:00
|
|
|
if (windowId === null || windowId < 0) {
|
2017-01-21 04:32:21 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
refreshBadgeAndMenu();
|
|
|
|
});
|
|
|
|
|
2016-09-29 01:12:14 +02:00
|
|
|
function refreshBadgeAndMenu() {
|
2017-01-21 04:32:21 +01:00
|
|
|
chrome.tabs.query({ active: true, windowId: chrome.windows.WINDOW_ID_CURRENT }, function (tabs) {
|
2016-09-23 04:04:50 +02:00
|
|
|
var tab = null;
|
|
|
|
if (tabs.length > 0) {
|
|
|
|
tab = tabs[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!tab) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-26 05:03:21 +02:00
|
|
|
buildContextMenu(function () {
|
|
|
|
loadMenuAndUpdateBadge(tab.url, tab.id, true);
|
2017-01-21 18:39:37 +01:00
|
|
|
onUpdatedRan = onReplacedRan = false;
|
2016-10-26 05:03:21 +02:00
|
|
|
});
|
2016-09-23 04:04:50 +02:00
|
|
|
});
|
2016-09-29 01:12:14 +02:00
|
|
|
}
|
2016-09-23 04:04:50 +02:00
|
|
|
|
|
|
|
function loadMenuAndUpdateBadge(url, tabId, loadContextMenuOptions) {
|
|
|
|
if (!url) {
|
2016-09-16 05:24:45 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-23 04:04:50 +02:00
|
|
|
var tabDomain = tldjs.getDomain(url);
|
2016-09-16 05:24:45 +02:00
|
|
|
if (!tabDomain) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
chrome.browserAction.setBadgeBackgroundColor({ color: '#294e5f' });
|
|
|
|
|
2017-01-21 05:20:09 +01:00
|
|
|
menuOptionsLoaded = [];
|
2017-01-04 01:02:51 +01:00
|
|
|
loginService.getAllDecryptedForDomain(tabDomain).then(function (logins) {
|
|
|
|
sortLogins(logins);
|
2017-01-21 05:20:09 +01:00
|
|
|
|
|
|
|
if (loadContextMenuOptions) {
|
|
|
|
for (var i = 0; i < logins.length; i++) {
|
2017-01-04 01:02:51 +01:00
|
|
|
loadLoginContextMenuOptions(logins[i]);
|
2016-09-16 05:24:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-04 01:02:51 +01:00
|
|
|
if (logins.length > 0 && logins.length < 9) {
|
2016-09-16 05:24:45 +02:00
|
|
|
chrome.browserAction.setBadgeText({
|
2017-01-04 01:02:51 +01:00
|
|
|
text: logins.length.toString(),
|
2016-09-16 05:24:45 +02:00
|
|
|
tabId: tabId
|
|
|
|
});
|
|
|
|
}
|
2017-01-04 01:02:51 +01:00
|
|
|
else if (logins.length > 0) {
|
2016-09-16 05:24:45 +02:00
|
|
|
chrome.browserAction.setBadgeText({
|
|
|
|
text: '9+',
|
|
|
|
tabId: tabId
|
|
|
|
});
|
|
|
|
}
|
2016-09-21 06:04:59 +02:00
|
|
|
else {
|
2017-01-04 01:02:51 +01:00
|
|
|
loadNoLoginsContextMenuOptions(i18nService.noMatchingLogins);
|
2016-09-21 06:04:59 +02:00
|
|
|
chrome.browserAction.setBadgeText({
|
2016-09-29 01:12:14 +02:00
|
|
|
text: '',
|
2016-09-21 06:04:59 +02:00
|
|
|
tabId: tabId
|
|
|
|
});
|
|
|
|
}
|
2016-10-25 06:23:21 +02:00
|
|
|
}, function () {
|
2017-01-04 01:02:51 +01:00
|
|
|
loadNoLoginsContextMenuOptions(i18nService.vaultLocked);
|
2016-10-25 06:23:21 +02:00
|
|
|
chrome.browserAction.setBadgeText({
|
|
|
|
text: '',
|
|
|
|
tabId: tabId
|
|
|
|
});
|
2016-09-16 05:24:45 +02:00
|
|
|
});
|
2016-09-23 04:04:50 +02:00
|
|
|
}
|
2016-09-17 02:20:02 +02:00
|
|
|
|
|
|
|
chrome.contextMenus.onClicked.addListener(function (info, tab) {
|
2016-10-09 00:09:38 +02:00
|
|
|
if (info.menuItemId === 'generate-password') {
|
|
|
|
ga('send', {
|
|
|
|
hitType: 'event',
|
|
|
|
eventAction: 'Generated Password From Context Menu'
|
|
|
|
});
|
|
|
|
passwordGenerationService.getOptions().then(function (options) {
|
|
|
|
var password = passwordGenerationService.generatePassword(options);
|
|
|
|
copyToClipboard(password);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else if (info.parentMenuItemId === 'autofill' || info.parentMenuItemId === 'copy-username' ||
|
2016-09-17 06:13:12 +02:00
|
|
|
info.parentMenuItemId === 'copy-password') {
|
2016-09-17 02:20:02 +02:00
|
|
|
var id = info.menuItemId.split('_')[1];
|
2016-09-22 05:59:53 +02:00
|
|
|
if (id === 'noop') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-04 01:02:51 +01:00
|
|
|
loginService.getAllDecrypted().then(function (logins) {
|
|
|
|
for (var i = 0; i < logins.length; i++) {
|
|
|
|
if (logins[i].id === id) {
|
2016-09-17 06:13:12 +02:00
|
|
|
if (info.parentMenuItemId === 'autofill') {
|
2016-09-28 05:19:33 +02:00
|
|
|
ga('send', {
|
|
|
|
hitType: 'event',
|
|
|
|
eventAction: 'Autofilled From Context Menu'
|
|
|
|
});
|
2017-01-04 01:02:51 +01:00
|
|
|
startAutofillPage(logins[i]);
|
2016-09-17 06:13:12 +02:00
|
|
|
}
|
|
|
|
else if (info.parentMenuItemId === 'copy-username') {
|
2016-09-28 05:19:33 +02:00
|
|
|
ga('send', {
|
|
|
|
hitType: 'event',
|
|
|
|
eventAction: 'Copied Username From Context Menu'
|
|
|
|
});
|
2017-01-04 01:02:51 +01:00
|
|
|
copyToClipboard(logins[i].username);
|
2016-09-17 02:20:02 +02:00
|
|
|
}
|
|
|
|
else if (info.parentMenuItemId === 'copy-password') {
|
2016-09-28 05:19:33 +02:00
|
|
|
ga('send', {
|
|
|
|
hitType: 'event',
|
|
|
|
eventAction: 'Copied Password From Context Menu'
|
|
|
|
});
|
2017-01-04 01:02:51 +01:00
|
|
|
copyToClipboard(logins[i].password);
|
2016-09-17 02:20:02 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-10-25 06:23:21 +02:00
|
|
|
}, function () {
|
|
|
|
|
2016-09-17 02:20:02 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-09-22 05:41:53 +02:00
|
|
|
function messageCurrentTab(command, data) {
|
|
|
|
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
|
|
|
|
var tabId = null;
|
|
|
|
if (tabs.length > 0) {
|
|
|
|
tabId = tabs[0].id;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-30 08:09:54 +01:00
|
|
|
messageTab(tabId, command, data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function messageTab(tabId, command, data) {
|
|
|
|
if (!tabId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var obj = {
|
|
|
|
command: command
|
|
|
|
};
|
|
|
|
|
|
|
|
if (data) {
|
|
|
|
obj['data'] = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
chrome.tabs.sendMessage(tabId, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
function collectPageDetailsForContentScript(tab) {
|
|
|
|
chrome.tabs.sendMessage(tab.id, { command: 'collectPageDetails', tabId: tab.id, contentScript: true }, function () { });
|
|
|
|
}
|
|
|
|
|
|
|
|
function addLogin(login, tab) {
|
|
|
|
var loginDomain = tldjs.getDomain(login.url);
|
|
|
|
if (!loginDomain) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-04 01:02:51 +01:00
|
|
|
loginService.getAllDecryptedForDomain(loginDomain).then(function (logins) {
|
2016-12-30 20:25:57 +01:00
|
|
|
var match = false;
|
2017-01-04 01:02:51 +01:00
|
|
|
for (var i = 0; i < logins.length; i++) {
|
|
|
|
if (logins[i].username === login.username) {
|
2016-12-30 20:25:57 +01:00
|
|
|
match = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!match) {
|
|
|
|
// remove any old logins for this tab
|
|
|
|
removeAddLogin(tab);
|
|
|
|
|
|
|
|
loginsToAdd.push({
|
|
|
|
username: login.username,
|
|
|
|
password: login.password,
|
|
|
|
name: loginDomain,
|
|
|
|
uri: login.url,
|
|
|
|
tabId: tab.id,
|
|
|
|
expires: new Date((new Date()).getTime() + 30 * 60000) // 30 minutes
|
|
|
|
});
|
2016-12-30 20:50:16 +01:00
|
|
|
checkLoginsToAdd(tab);
|
2016-12-30 20:25:57 +01:00
|
|
|
}
|
2016-12-30 08:09:54 +01:00
|
|
|
});
|
2016-12-30 20:25:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanupLoginsToAdd();
|
|
|
|
setInterval(cleanupLoginsToAdd, 2 * 60 * 1000); // check every 2 minutes
|
|
|
|
function cleanupLoginsToAdd() {
|
|
|
|
var now = new Date();
|
|
|
|
for (var i = loginsToAdd.length - 1; i >= 0 ; i--) {
|
|
|
|
if (loginsToAdd[i].expires < now) {
|
|
|
|
loginsToAdd.splice(i, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeAddLogin(tab) {
|
|
|
|
for (var i = loginsToAdd.length - 1; i >= 0 ; i--) {
|
|
|
|
if (loginsToAdd[i].tabId === tab.id) {
|
|
|
|
loginsToAdd.splice(i, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function saveAddLogin(tab) {
|
|
|
|
for (var i = loginsToAdd.length - 1; i >= 0 ; i--) {
|
|
|
|
if (loginsToAdd[i].tabId === tab.id) {
|
|
|
|
var loginToAdd = loginsToAdd[i];
|
|
|
|
loginsToAdd.splice(i, 1);
|
2017-01-04 01:02:51 +01:00
|
|
|
loginService.encrypt({
|
2016-12-30 20:25:57 +01:00
|
|
|
id: null,
|
|
|
|
folderId: null,
|
|
|
|
favorite: false,
|
|
|
|
name: loginToAdd.name,
|
|
|
|
uri: loginToAdd.uri,
|
|
|
|
username: loginToAdd.username,
|
|
|
|
password: loginToAdd.password,
|
|
|
|
notes: null
|
2017-01-04 01:02:51 +01:00
|
|
|
}).then(function (loginModel) {
|
|
|
|
var login = new Login(loginModel, true);
|
|
|
|
loginService.saveWithServer(login).then(function (login) {
|
2016-12-30 20:25:57 +01:00
|
|
|
ga('send', {
|
|
|
|
hitType: 'event',
|
2017-01-04 01:02:51 +01:00
|
|
|
eventAction: 'Added Login from Notification Bar'
|
2016-12-30 20:25:57 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
messageTab(tab.id, 'closeNotificationBar');
|
|
|
|
}
|
|
|
|
}
|
2016-12-30 08:09:54 +01:00
|
|
|
}
|
|
|
|
|
2016-12-30 20:50:16 +01:00
|
|
|
function checkLoginsToAdd(tab) {
|
2016-12-30 08:09:54 +01:00
|
|
|
if (!loginsToAdd.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-30 20:50:16 +01:00
|
|
|
if (tab) {
|
|
|
|
check();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-30 08:09:54 +01:00
|
|
|
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
|
|
|
|
if (tabs.length > 0) {
|
2016-12-30 20:50:16 +01:00
|
|
|
tab = tabs[0];
|
|
|
|
check();
|
2016-09-22 05:41:53 +02:00
|
|
|
}
|
2016-12-30 20:50:16 +01:00
|
|
|
});
|
2016-09-22 05:41:53 +02:00
|
|
|
|
2016-12-30 20:50:16 +01:00
|
|
|
function check() {
|
|
|
|
if (!tab) {
|
2016-12-30 08:09:54 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-09-22 05:41:53 +02:00
|
|
|
|
2016-12-30 20:50:16 +01:00
|
|
|
var tabDomain = tldjs.getDomain(tab.url);
|
2016-12-30 08:09:54 +01:00
|
|
|
if (!tabDomain) {
|
|
|
|
return;
|
2016-09-22 05:41:53 +02:00
|
|
|
}
|
|
|
|
|
2016-12-30 08:09:54 +01:00
|
|
|
for (var i = 0; i < loginsToAdd.length; i++) {
|
2016-12-30 20:50:16 +01:00
|
|
|
// loginsToAdd[x].name is the domain here
|
|
|
|
if (loginsToAdd[i].tabId === tab.id && loginsToAdd[i].name === tabDomain) {
|
|
|
|
messageTab(tab.id, 'openNotificationBar', { type: 'add' });
|
2016-12-30 08:09:54 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-12-30 20:50:16 +01:00
|
|
|
}
|
2016-09-22 05:41:53 +02:00
|
|
|
}
|
|
|
|
|
2017-01-04 01:02:51 +01:00
|
|
|
function startAutofillPage(login) {
|
|
|
|
loginToAutoFill = login;
|
2016-09-17 06:13:12 +02:00
|
|
|
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
|
|
|
|
var tabId = null;
|
|
|
|
if (tabs.length > 0) {
|
|
|
|
tabId = tabs[0].id;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!tabId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-26 04:01:46 +01:00
|
|
|
chrome.tabs.sendMessage(tabId, { command: 'collectPageDetails', tabId: tabId }, function () { });
|
|
|
|
});
|
|
|
|
}
|
2016-09-17 06:13:12 +02:00
|
|
|
|
2016-11-26 04:01:46 +01:00
|
|
|
function autofillPage() {
|
|
|
|
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
|
|
|
|
var tabId = null;
|
|
|
|
if (tabs.length > 0) {
|
|
|
|
tabId = tabs[0].id;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!tabId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-04 01:02:51 +01:00
|
|
|
if (loginToAutoFill && pageDetailsToAutoFill && pageDetailsToAutoFill.length) {
|
2016-11-26 04:01:46 +01:00
|
|
|
for (var i = 0; i < pageDetailsToAutoFill.length; i++) {
|
|
|
|
// make sure we're still on correct tab
|
2016-11-26 05:53:46 +01:00
|
|
|
if (pageDetailsToAutoFill[i].tabId !== tabId) {
|
2016-11-26 04:01:46 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-01-04 01:02:51 +01:00
|
|
|
var fillScript = autofillService.generateFillScript(pageDetailsToAutoFill[i].details, loginToAutoFill.username,
|
|
|
|
loginToAutoFill.password);
|
2016-11-26 04:01:46 +01:00
|
|
|
if (tabId && fillScript && fillScript.script && fillScript.script.length) {
|
|
|
|
chrome.tabs.sendMessage(tabId, {
|
|
|
|
command: 'fillForm',
|
|
|
|
fillScript: fillScript
|
|
|
|
}, {
|
|
|
|
frameId: pageDetailsToAutoFill[i].frameId
|
|
|
|
});
|
|
|
|
}
|
2016-09-17 06:13:12 +02:00
|
|
|
}
|
2016-11-26 04:01:46 +01:00
|
|
|
}
|
2016-09-17 06:13:12 +02:00
|
|
|
|
2016-11-26 04:01:46 +01:00
|
|
|
// reset
|
2017-01-04 01:02:51 +01:00
|
|
|
loginToAutoFill = null;
|
2016-11-26 04:01:46 +01:00
|
|
|
pageDetailsToAutoFill = [];
|
|
|
|
});
|
2016-09-17 06:13:12 +02:00
|
|
|
}
|
|
|
|
|
2017-01-04 01:02:51 +01:00
|
|
|
function sortLogins(logins) {
|
|
|
|
logins.sort(function (a, b) {
|
2016-09-17 02:20:02 +02:00
|
|
|
var nameA = (a.name + '_' + a.username).toUpperCase();
|
|
|
|
var nameB = (b.name + '_' + b.username).toUpperCase();
|
|
|
|
|
|
|
|
if (nameA < nameB) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (nameA > nameB) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-01-04 01:02:51 +01:00
|
|
|
function loadLoginContextMenuOptions(login) {
|
|
|
|
var title = login.name + (login.username && login.username !== '' ? ' (' + login.username + ')' : '');
|
|
|
|
loadContextMenuOptions(title, login.id, login);
|
2016-09-22 05:59:53 +02:00
|
|
|
}
|
|
|
|
|
2017-01-04 01:02:51 +01:00
|
|
|
function loadNoLoginsContextMenuOptions(noLoginsMessage) {
|
|
|
|
loadContextMenuOptions(noLoginsMessage, 'noop', null);
|
2016-09-22 05:59:53 +02:00
|
|
|
}
|
|
|
|
|
2017-01-04 01:02:51 +01:00
|
|
|
function loadContextMenuOptions(title, idSuffix, login) {
|
2017-01-21 05:20:09 +01:00
|
|
|
if (menuOptionsLoaded.indexOf(idSuffix) > -1) {
|
2017-01-21 04:32:21 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-01-21 05:20:09 +01:00
|
|
|
menuOptionsLoaded.push(idSuffix);
|
2017-01-21 04:32:21 +01:00
|
|
|
|
2017-01-04 01:02:51 +01:00
|
|
|
if (!login || (login.password && login.password !== '')) {
|
2016-10-14 05:42:08 +02:00
|
|
|
chrome.contextMenus.create({
|
|
|
|
type: 'normal',
|
|
|
|
id: 'autofill_' + idSuffix,
|
|
|
|
parentId: 'autofill',
|
|
|
|
contexts: ['all'],
|
|
|
|
title: title
|
|
|
|
});
|
|
|
|
}
|
2016-09-17 02:20:02 +02:00
|
|
|
|
2017-01-21 05:20:09 +01:00
|
|
|
if (utilsService.isFirefox()) {
|
|
|
|
// Firefox does not support writing to the clipboard from background
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-04 01:02:51 +01:00
|
|
|
if (!login || (login.username && login.username !== '')) {
|
2016-10-14 05:42:08 +02:00
|
|
|
chrome.contextMenus.create({
|
|
|
|
type: 'normal',
|
|
|
|
id: 'copy-username_' + idSuffix,
|
|
|
|
parentId: 'copy-username',
|
|
|
|
contexts: ['all'],
|
|
|
|
title: title
|
|
|
|
});
|
|
|
|
}
|
2016-09-17 02:20:02 +02:00
|
|
|
|
2017-01-04 01:02:51 +01:00
|
|
|
if (!login || (login.password && login.password !== '')) {
|
2016-10-14 05:42:08 +02:00
|
|
|
chrome.contextMenus.create({
|
|
|
|
type: 'normal',
|
|
|
|
id: 'copy-password_' + idSuffix,
|
|
|
|
parentId: 'copy-password',
|
|
|
|
contexts: ['all'],
|
|
|
|
title: title
|
|
|
|
});
|
|
|
|
}
|
2016-09-17 02:20:02 +02:00
|
|
|
}
|
|
|
|
|
2017-01-19 06:21:20 +01:00
|
|
|
// TODO: Fix callback hell by moving to promises
|
|
|
|
function logout(expired, callback) {
|
|
|
|
userService.getUserId(function (userId) {
|
|
|
|
syncService.setLastSync(new Date(0), function () {
|
|
|
|
settingsService.clear(function () {
|
|
|
|
tokenService.clearToken(function () {
|
|
|
|
cryptoService.clearKey(function () {
|
|
|
|
cryptoService.clearKeyHash(function () {
|
|
|
|
userService.clearUserIdAndEmail(function () {
|
|
|
|
loginService.clear(userId, function () {
|
|
|
|
folderService.clear(userId, function () {
|
|
|
|
chrome.runtime.sendMessage({ command: 'doneLoggingOut', expired: expired });
|
2017-01-22 02:00:25 +01:00
|
|
|
setIcon();
|
|
|
|
refreshBadgeAndMenu();
|
2017-01-19 06:21:20 +01:00
|
|
|
callback();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-17 02:20:02 +02:00
|
|
|
function copyToClipboard(text) {
|
|
|
|
if (window.clipboardData && window.clipboardData.setData) {
|
|
|
|
// IE specific code path to prevent textarea being shown while dialog is visible.
|
|
|
|
return clipboardData.setData('Text', text);
|
|
|
|
}
|
|
|
|
else if (document.queryCommandSupported && document.queryCommandSupported('copy')) {
|
|
|
|
var textarea = document.createElement('textarea');
|
|
|
|
textarea.textContent = text;
|
|
|
|
// Prevent scrolling to bottom of page in MS Edge.
|
|
|
|
textarea.style.position = 'fixed';
|
|
|
|
document.body.appendChild(textarea);
|
|
|
|
textarea.select();
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Security exception may be thrown by some browsers.
|
|
|
|
return document.execCommand('copy');
|
|
|
|
}
|
|
|
|
catch (ex) {
|
|
|
|
console.warn('Copy to clipboard failed.', ex);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
document.body.removeChild(textarea);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-21 06:26:23 +02:00
|
|
|
|
2016-09-22 02:13:52 +02:00
|
|
|
// Sync polling
|
2016-09-21 06:26:23 +02:00
|
|
|
|
2016-09-22 02:13:52 +02:00
|
|
|
fullSync(true);
|
|
|
|
setInterval(fullSync, 5 * 60 * 1000); // check every 5 minutes
|
|
|
|
var syncInternal = 6 * 60 * 60 * 1000; // 6 hours
|
|
|
|
|
|
|
|
function fullSync(override) {
|
2017-01-14 18:47:11 +01:00
|
|
|
override = override || false;
|
2016-09-22 02:13:52 +02:00
|
|
|
syncService.getLastSync(function (lastSync) {
|
|
|
|
var now = new Date();
|
|
|
|
if (override || !lastSync || (now - lastSync) >= syncInternal) {
|
2017-01-18 03:43:26 +01:00
|
|
|
syncService.fullSync(override || false, function () { });
|
2016-09-22 02:13:52 +02:00
|
|
|
}
|
|
|
|
});
|
2016-09-21 06:26:23 +02:00
|
|
|
}
|
2016-10-25 06:23:21 +02:00
|
|
|
|
|
|
|
// Locking
|
|
|
|
|
|
|
|
checkLock();
|
|
|
|
setInterval(checkLock, 10 * 1000); // check every 10 seconds
|
|
|
|
|
|
|
|
function checkLock() {
|
|
|
|
if (chrome.extension.getViews({ type: 'popup' }).length > 0) {
|
|
|
|
// popup is open, do not lock
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cryptoService.getKey(false, function (key) {
|
|
|
|
if (!key) {
|
|
|
|
// no key so no need to lock
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
chrome.storage.local.get(constantsService.lockOptionKey, function (obj) {
|
2016-10-26 05:37:24 +02:00
|
|
|
if (obj && ((!obj[constantsService.lockOptionKey] && obj[constantsService.lockOptionKey] !== 0) ||
|
|
|
|
obj[constantsService.lockOptionKey] === -1)) {
|
2016-10-25 06:23:21 +02:00
|
|
|
// no lock option set
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
chrome.storage.local.get(constantsService.lastActiveKey, function (obj2) {
|
|
|
|
if (obj2 && obj2[constantsService.lastActiveKey]) {
|
|
|
|
var lastActive = obj2[constantsService.lastActiveKey];
|
|
|
|
var diffSeconds = ((new Date()).getTime() - lastActive) / 1000;
|
|
|
|
var lockOptionSeconds = parseInt(obj[constantsService.lockOptionKey]) * 60;
|
|
|
|
|
|
|
|
if (diffSeconds >= lockOptionSeconds) {
|
|
|
|
// need to lock now
|
|
|
|
cryptoService.clearKey(function () {
|
2016-10-26 05:17:46 +02:00
|
|
|
setIcon();
|
2016-10-25 06:23:21 +02:00
|
|
|
folderService.clearCache();
|
2017-01-04 01:02:51 +01:00
|
|
|
loginService.clearCache();
|
2016-10-25 06:23:21 +02:00
|
|
|
refreshBadgeAndMenu();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|