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';
|
|
|
|
|
2021-06-07 19:25:37 +02:00
|
|
|
import { CipherService } from 'jslib-common/abstractions/cipher.service';
|
|
|
|
import { EventService } from 'jslib-common/abstractions/event.service';
|
|
|
|
import { PasswordGenerationService } from 'jslib-common/abstractions/passwordGeneration.service';
|
|
|
|
import { PlatformUtilsService } from 'jslib-common/abstractions/platformUtils.service';
|
|
|
|
import { TotpService } from 'jslib-common/abstractions/totp.service';
|
|
|
|
import { VaultTimeoutService } from 'jslib-common/abstractions/vaultTimeout.service';
|
2020-03-03 16:42:49 +01:00
|
|
|
|
2021-06-07 19:25:37 +02:00
|
|
|
import { EventType } from 'jslib-common/enums/eventType';
|
2021-10-18 15:32:38 +02:00
|
|
|
import { CipherView } from 'jslib-common/models/view/cipherView';
|
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,
|
2021-04-14 23:43:09 +02:00
|
|
|
private passwordGenerationService: PasswordGenerationService,
|
2020-04-06 17:40:16 +02:00
|
|
|
private platformUtilsService: PlatformUtilsService, private vaultTimeoutService: VaultTimeoutService,
|
2020-03-03 16:40:06 +01:00
|
|
|
private eventService: EventService, private totpService: TotpService) {
|
2017-12-07 22:02:15 +01:00
|
|
|
this.contextMenus = chrome.contextMenus;
|
|
|
|
}
|
|
|
|
|
|
|
|
async init() {
|
|
|
|
if (!this.contextMenus) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-19 16:00:38 +02:00
|
|
|
this.contextMenus.onClicked.addListener(async (info: chrome.contextMenus.OnClickData, tab: chrome.tabs.Tab) => {
|
2017-12-07 22:02:15 +01:00
|
|
|
if (info.menuItemId === 'generate-password') {
|
|
|
|
await this.generatePasswordToClipboard();
|
2021-09-01 23:51:43 +02:00
|
|
|
} else if (info.menuItemId === 'copy-identifier') {
|
2021-10-07 01:52:33 +02:00
|
|
|
await this.getClickedElement(info.frameId);
|
2020-03-03 16:40:06 +01:00
|
|
|
} else if (info.parentMenuItemId === 'autofill' ||
|
2020-03-03 16:42:49 +01:00
|
|
|
info.parentMenuItemId === 'copy-username' ||
|
|
|
|
info.parentMenuItemId === 'copy-password' ||
|
|
|
|
info.parentMenuItemId === 'copy-totp') {
|
2021-10-19 16:03:39 +02:00
|
|
|
await this.cipherAction(tab, info);
|
2017-12-07 22:02:15 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private async generatePasswordToClipboard() {
|
2020-02-28 18:43:27 +01:00
|
|
|
const options = (await this.passwordGenerationService.getOptions())[0];
|
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);
|
|
|
|
}
|
|
|
|
|
2021-10-07 01:52:33 +02:00
|
|
|
private async getClickedElement(frameId: number) {
|
2021-09-01 23:51:43 +02:00
|
|
|
const tab = await BrowserApi.getTabFromCurrentWindow();
|
|
|
|
if (tab == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-07 01:52:33 +02:00
|
|
|
BrowserApi.tabSendMessage(tab, { command: 'getClickedElement' }, { frameId: frameId });
|
2021-09-01 23:51:43 +02:00
|
|
|
}
|
|
|
|
|
2021-10-19 16:03:39 +02:00
|
|
|
private async cipherAction(tab: chrome.tabs.Tab, info: chrome.contextMenus.OnClickData) {
|
2017-12-07 22:02:15 +01:00
|
|
|
const id = info.menuItemId.split('_')[1];
|
|
|
|
if (id === 'noop') {
|
2020-10-09 17:16:15 +02:00
|
|
|
if (chrome.browserAction && (chrome.browserAction as any).openPopup) {
|
|
|
|
(chrome.browserAction as any).openPopup();
|
2017-12-07 22:02:15 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-06 17:40:16 +02:00
|
|
|
if (await this.vaultTimeoutService.isLocked()) {
|
2019-03-06 22:50:04 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-12-07 22:02:15 +01:00
|
|
|
|
2019-03-06 22:50:04 +01:00
|
|
|
const ciphers = await this.cipherService.getAllDecrypted();
|
2021-02-10 16:40:15 +01:00
|
|
|
const cipher = ciphers.find(c => c.id === id);
|
2019-03-06 22:50:04 +01:00
|
|
|
if (cipher == null) {
|
|
|
|
return;
|
|
|
|
}
|
2017-12-07 22:02:15 +01:00
|
|
|
|
2019-03-06 22:50:04 +01:00
|
|
|
if (info.parentMenuItemId === 'autofill') {
|
2021-10-19 16:03:39 +02:00
|
|
|
await this.startAutofillPage(tab, cipher);
|
2019-03-06 22:50:04 +01:00
|
|
|
} else if (info.parentMenuItemId === 'copy-username') {
|
|
|
|
this.platformUtilsService.copyToClipboard(cipher.login.username, { window: window });
|
|
|
|
} else if (info.parentMenuItemId === 'copy-password') {
|
|
|
|
this.platformUtilsService.copyToClipboard(cipher.login.password, { window: window });
|
2019-07-12 21:16:06 +02:00
|
|
|
this.eventService.collect(EventType.Cipher_ClientCopiedPassword, cipher.id);
|
2020-03-03 16:40:06 +01:00
|
|
|
} else if (info.parentMenuItemId === 'copy-totp') {
|
|
|
|
const totpValue = await this.totpService.getCode(cipher.login.totp);
|
|
|
|
this.platformUtilsService.copyToClipboard(totpValue, { window: window });
|
2017-12-07 22:02:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 16:03:39 +02:00
|
|
|
private async startAutofillPage(tab: chrome.tabs.Tab, cipher: CipherView) {
|
2017-12-07 22:02:15 +01:00
|
|
|
this.main.loginToAutoFill = cipher;
|
|
|
|
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',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|