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-26 16:48:32 +01:00
|
|
|
import { Analytics } from 'jslib/misc';
|
|
|
|
|
2019-03-06 22:50:04 +01:00
|
|
|
import { CipherService } from 'jslib/abstractions/cipher.service';
|
2019-07-12 21:16:06 +02:00
|
|
|
import { EventService } from 'jslib/abstractions/event.service';
|
2019-03-06 22:50:04 +01:00
|
|
|
import { LockService } from 'jslib/abstractions/lock.service';
|
|
|
|
import { PasswordGenerationService } from 'jslib/abstractions/passwordGeneration.service';
|
|
|
|
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
|
2019-07-12 21:16:06 +02:00
|
|
|
import { EventType } from 'jslib/enums/eventType';
|
2018-01-07 04:13:48 +01:00
|
|
|
|
2017-12-07 22:02:15 +01:00
|
|
|
export default class ContextMenusBackground {
|
|
|
|
private contextMenus: any;
|
|
|
|
|
|
|
|
constructor(private main: MainBackground, private cipherService: CipherService,
|
2018-04-23 19:04:11 +02:00
|
|
|
private passwordGenerationService: PasswordGenerationService, private analytics: Analytics,
|
2019-07-12 21:16:06 +02:00
|
|
|
private platformUtilsService: PlatformUtilsService, private lockService: LockService,
|
|
|
|
private eventService: EventService) {
|
2017-12-07 22:02:15 +01:00
|
|
|
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-04-23 19:04:11 +02:00
|
|
|
const password = await this.passwordGenerationService.generatePassword(options);
|
2018-08-13 15:44:59 +02:00
|
|
|
this.platformUtilsService.copyToClipboard(password, { window: window });
|
2017-12-07 22:02:15 +01:00
|
|
|
this.passwordGenerationService.addHistory(password);
|
|
|
|
|
2018-01-19 22:19:24 +01:00
|
|
|
this.analytics.ga('send', {
|
2017-12-07 22:02:15 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-03-06 22:50:04 +01:00
|
|
|
if (await this.lockService.isLocked()) {
|
|
|
|
return;
|
|
|
|
}
|
2017-12-07 22:02:15 +01:00
|
|
|
|
2019-03-06 22:50:04 +01:00
|
|
|
const ciphers = await this.cipherService.getAllDecrypted();
|
|
|
|
const cipher = ciphers.find((c) => c.id === id);
|
|
|
|
if (cipher == null) {
|
|
|
|
return;
|
|
|
|
}
|
2017-12-07 22:02:15 +01:00
|
|
|
|
2019-03-06 22:50:04 +01:00
|
|
|
if (info.parentMenuItemId === 'autofill') {
|
|
|
|
this.analytics.ga('send', {
|
|
|
|
hitType: 'event',
|
|
|
|
eventAction: 'Autofilled From Context Menu',
|
|
|
|
});
|
|
|
|
await this.startAutofillPage(cipher);
|
|
|
|
} else if (info.parentMenuItemId === 'copy-username') {
|
|
|
|
this.analytics.ga('send', {
|
|
|
|
hitType: 'event',
|
|
|
|
eventAction: 'Copied Username From Context Menu',
|
|
|
|
});
|
|
|
|
this.platformUtilsService.copyToClipboard(cipher.login.username, { window: window });
|
|
|
|
} else if (info.parentMenuItemId === 'copy-password') {
|
|
|
|
this.analytics.ga('send', {
|
|
|
|
hitType: 'event',
|
|
|
|
eventAction: 'Copied Password From Context Menu',
|
|
|
|
});
|
|
|
|
this.platformUtilsService.copyToClipboard(cipher.login.password, { window: window });
|
2019-07-12 21:16:06 +02:00
|
|
|
this.eventService.collect(EventType.Cipher_ClientCopiedPassword, cipher.id);
|
2017-12-07 22:02:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|