2018-01-12 17:09:30 +01:00
|
|
|
import { BrowserApi } from '../browser/browserApi';
|
2017-12-07 22:02:15 +01:00
|
|
|
|
|
|
|
import MainBackground from './main.background';
|
|
|
|
|
2018-01-09 23:55:28 +01:00
|
|
|
import {
|
2018-01-10 05:05:46 +01:00
|
|
|
CipherService,
|
2018-01-09 23:55:28 +01:00
|
|
|
PasswordGenerationService,
|
|
|
|
} from 'jslib/abstractions';
|
2018-01-07 04:13:48 +01:00
|
|
|
|
2018-01-09 20:26:20 +01:00
|
|
|
import { UtilsService } from 'jslib/services/utils.service';
|
2017-12-07 22:02:15 +01:00
|
|
|
|
|
|
|
export default class ContextMenusBackground {
|
|
|
|
private contextMenus: any;
|
|
|
|
|
|
|
|
constructor(private main: MainBackground, private cipherService: CipherService,
|
|
|
|
private passwordGenerationService: PasswordGenerationService) {
|
|
|
|
this.contextMenus = chrome.contextMenus;
|
|
|
|
}
|
|
|
|
|
|
|
|
async init() {
|
|
|
|
if (!this.contextMenus) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.contextMenus.onClicked.addListener(async (info: any, tab: any) => {
|
|
|
|
if (info.menuItemId === 'generate-password') {
|
|
|
|
await this.generatePasswordToClipboard();
|
|
|
|
} else if (info.parentMenuItemId === 'autofill' || info.parentMenuItemId === 'copy-username' ||
|
|
|
|
info.parentMenuItemId === 'copy-password') {
|
|
|
|
await this.cipherAction(info);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private async generatePasswordToClipboard() {
|
|
|
|
const options = await this.passwordGenerationService.getOptions();
|
2018-01-09 23:55:28 +01:00
|
|
|
const password = this.passwordGenerationService.generatePassword(options);
|
2018-01-09 20:26:20 +01:00
|
|
|
UtilsService.copyToClipboard(password);
|
2017-12-07 22:02:15 +01:00
|
|
|
this.passwordGenerationService.addHistory(password);
|
|
|
|
|
|
|
|
(window as any).ga('send', {
|
|
|
|
hitType: 'event',
|
|
|
|
eventAction: 'Generated Password From Context Menu',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private async cipherAction(info: any) {
|
|
|
|
const id = info.menuItemId.split('_')[1];
|
|
|
|
if (id === 'noop') {
|
|
|
|
if (chrome.browserAction && chrome.browserAction.openPopup) {
|
|
|
|
chrome.browserAction.openPopup();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ciphers = await this.cipherService.getAllDecrypted();
|
|
|
|
for (let i = 0; i < ciphers.length; i++) {
|
|
|
|
const cipher = ciphers[i];
|
|
|
|
if (cipher.id !== id) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info.parentMenuItemId === 'autofill') {
|
|
|
|
(window as any).ga('send', {
|
|
|
|
hitType: 'event',
|
|
|
|
eventAction: 'Autofilled From Context Menu',
|
|
|
|
});
|
|
|
|
await this.startAutofillPage(cipher);
|
|
|
|
} else if (info.parentMenuItemId === 'copy-username') {
|
|
|
|
(window as any).ga('send', {
|
|
|
|
hitType: 'event',
|
|
|
|
eventAction: 'Copied Username From Context Menu',
|
|
|
|
});
|
2018-01-09 20:26:20 +01:00
|
|
|
UtilsService.copyToClipboard(cipher.login.username);
|
2017-12-07 22:02:15 +01:00
|
|
|
} else if (info.parentMenuItemId === 'copy-password') {
|
|
|
|
(window as any).ga('send', {
|
|
|
|
hitType: 'event',
|
|
|
|
eventAction: 'Copied Password From Context Menu',
|
|
|
|
});
|
2018-01-09 20:26:20 +01:00
|
|
|
UtilsService.copyToClipboard(cipher.login.password);
|
2017-12-07 22:02:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async startAutofillPage(cipher: any) {
|
|
|
|
this.main.loginToAutoFill = cipher;
|
|
|
|
const tab = await BrowserApi.getTabFromCurrentWindow();
|
|
|
|
if (tab == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-12 20:44:44 +01:00
|
|
|
BrowserApi.tabSendMessage(tab, {
|
2017-12-07 22:02:15 +01:00
|
|
|
command: 'collectPageDetails',
|
|
|
|
tab: tab,
|
|
|
|
sender: 'contextMenu',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|