2018-01-12 17:09:30 +01:00
|
|
|
import { BrowserApi } from '../browser/browserApi';
|
2017-12-07 21:06:37 +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 { PasswordGenerationService } from 'jslib/abstractions/passwordGeneration.service';
|
|
|
|
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
|
2020-04-06 17:40:16 +02:00
|
|
|
import { VaultTimeoutService } from 'jslib/abstractions/vaultTimeout.service';
|
2018-01-07 04:13:48 +01:00
|
|
|
|
2017-12-07 21:06:37 +01:00
|
|
|
export default class CommandsBackground {
|
2018-01-17 15:12:16 +01:00
|
|
|
private isSafari: boolean;
|
2018-01-17 19:38:32 +01:00
|
|
|
private isVivaldi: boolean;
|
2017-12-07 21:06:37 +01:00
|
|
|
|
2018-01-17 15:12:16 +01:00
|
|
|
constructor(private main: MainBackground, private passwordGenerationService: PasswordGenerationService,
|
2019-03-06 22:50:04 +01:00
|
|
|
private platformUtilsService: PlatformUtilsService, private analytics: Analytics,
|
2020-04-06 17:40:16 +02:00
|
|
|
private vaultTimeoutService: VaultTimeoutService) {
|
2018-01-17 15:12:16 +01:00
|
|
|
this.isSafari = this.platformUtilsService.isSafari();
|
2018-01-17 19:38:32 +01:00
|
|
|
this.isVivaldi = this.platformUtilsService.isVivaldi();
|
2017-12-07 21:06:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async init() {
|
2021-02-03 20:36:05 +01:00
|
|
|
if (this.isVivaldi) {
|
2019-08-15 22:36:49 +02:00
|
|
|
BrowserApi.messageListener('commands.background', async (msg: any, sender: any, sendResponse: any) => {
|
2018-01-17 16:11:09 +01:00
|
|
|
if (msg.command === 'keyboardShortcutTriggered' && msg.shortcut) {
|
2018-01-17 19:38:32 +01:00
|
|
|
await this.processCommand(msg.shortcut, sender);
|
2018-01-17 15:12:16 +01:00
|
|
|
}
|
2018-01-17 16:11:09 +01:00
|
|
|
});
|
2020-09-15 16:50:45 +02:00
|
|
|
} else if (chrome && chrome.commands) {
|
2019-08-13 21:47:03 +02:00
|
|
|
chrome.commands.onCommand.addListener(async (command: any) => {
|
2018-01-17 15:12:16 +01:00
|
|
|
await this.processCommand(command);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-17 16:11:09 +01:00
|
|
|
private async processCommand(command: string, sender?: any) {
|
2018-01-17 15:12:16 +01:00
|
|
|
switch (command) {
|
|
|
|
case 'generate_password':
|
|
|
|
await this.generatePasswordToClipboard();
|
|
|
|
break;
|
|
|
|
case 'autofill_login':
|
2018-01-17 16:11:09 +01:00
|
|
|
await this.autoFillLogin(sender ? sender.tab : null);
|
2018-01-17 15:12:16 +01:00
|
|
|
break;
|
|
|
|
case 'open_popup':
|
|
|
|
await this.openPopup();
|
|
|
|
break;
|
2020-11-06 16:43:06 +01:00
|
|
|
case 'lock_vault':
|
|
|
|
await this.vaultTimeoutService.lock(true);
|
|
|
|
break;
|
2018-01-17 15:12:16 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-12-07 21:06:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private async generatePasswordToClipboard() {
|
2020-02-28 18:43:27 +01:00
|
|
|
const options = (await this.passwordGenerationService.getOptions())[0];
|
2018-01-09 23:55:28 +01: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 21:06:37 +01:00
|
|
|
this.passwordGenerationService.addHistory(password);
|
|
|
|
|
2018-01-19 22:19:24 +01:00
|
|
|
this.analytics.ga('send', {
|
2017-12-07 21:06:37 +01:00
|
|
|
hitType: 'event',
|
|
|
|
eventAction: 'Generated Password From Command',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-17 16:11:09 +01:00
|
|
|
private async autoFillLogin(tab?: any) {
|
2020-04-06 17:40:16 +02:00
|
|
|
if (await this.vaultTimeoutService.isLocked()) {
|
2019-03-06 22:50:04 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-17 16:11:09 +01:00
|
|
|
if (!tab) {
|
|
|
|
tab = await BrowserApi.getTabFromCurrentWindowId();
|
|
|
|
}
|
|
|
|
|
2017-12-07 21:06:37 +01:00
|
|
|
if (tab == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-06 22:50:04 +01:00
|
|
|
await this.main.collectPageDetailsForContentScript(tab, 'autofill_cmd');
|
2017-12-07 21:06:37 +01:00
|
|
|
|
2018-01-19 22:19:24 +01:00
|
|
|
this.analytics.ga('send', {
|
2017-12-07 21:06:37 +01:00
|
|
|
hitType: 'event',
|
|
|
|
eventAction: 'Autofilled From Command',
|
|
|
|
});
|
|
|
|
}
|
2018-01-17 15:12:16 +01:00
|
|
|
|
|
|
|
private async openPopup() {
|
2018-01-17 19:38:32 +01:00
|
|
|
// Chrome APIs cannot open popup
|
2018-01-18 22:17:58 +01:00
|
|
|
if (!this.isSafari) {
|
2018-01-17 15:12:16 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-18 22:17:58 +01:00
|
|
|
this.main.openPopup();
|
2018-01-19 22:19:24 +01:00
|
|
|
this.analytics.ga('send', {
|
2018-01-17 16:23:11 +01:00
|
|
|
hitType: 'event',
|
|
|
|
eventAction: 'Opened Popup From Command',
|
|
|
|
});
|
2018-01-17 15:12:16 +01:00
|
|
|
}
|
2017-12-07 21:06:37 +01:00
|
|
|
}
|