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-17 15:12:16 +01:00
|
|
|
import {
|
|
|
|
PasswordGenerationService,
|
|
|
|
PlatformUtilsService,
|
|
|
|
} 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 21:06:37 +01:00
|
|
|
|
|
|
|
export default class CommandsBackground {
|
|
|
|
private commands: any;
|
2018-01-17 15:12:16 +01:00
|
|
|
private isSafari: boolean;
|
2017-12-07 21:06:37 +01:00
|
|
|
|
2018-01-17 15:12:16 +01:00
|
|
|
constructor(private main: MainBackground, private passwordGenerationService: PasswordGenerationService,
|
|
|
|
private platformUtilsService: PlatformUtilsService) {
|
|
|
|
this.isSafari = this.platformUtilsService.isSafari();
|
|
|
|
this.commands = this.isSafari ? safari.application : chrome.commands;
|
2017-12-07 21:06:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async init() {
|
|
|
|
if (!this.commands) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-17 15:12:16 +01:00
|
|
|
if (this.isSafari) {
|
2018-01-17 16:11:09 +01:00
|
|
|
BrowserApi.messageListener(async (msg: any, sender: any, sendResponse: any) => {
|
|
|
|
if (msg.command === 'keyboardShortcutTriggered' && msg.shortcut) {
|
|
|
|
await this.processCommand(msg.shortcut);
|
2018-01-17 15:12:16 +01:00
|
|
|
}
|
2018-01-17 16:11:09 +01:00
|
|
|
});
|
2018-01-17 15:12:16 +01:00
|
|
|
} else {
|
|
|
|
this.commands.onCommand.addListener(async (command: any) => {
|
|
|
|
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;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-12-07 21:06:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private async generatePasswordToClipboard() {
|
2018-01-17 16:11:09 +01:00
|
|
|
if (this.isSafari) {
|
|
|
|
// Safari does not support access to clipboard from background
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-07 21:06:37 +01:00
|
|
|
const options = await this.passwordGenerationService.getOptions();
|
2018-01-09 23:55:28 +01:00
|
|
|
const password = await this.passwordGenerationService.generatePassword(options);
|
2018-01-09 20:26:20 +01:00
|
|
|
UtilsService.copyToClipboard(password);
|
2017-12-07 21:06:37 +01:00
|
|
|
this.passwordGenerationService.addHistory(password);
|
|
|
|
|
|
|
|
(window as any).ga('send', {
|
|
|
|
hitType: 'event',
|
|
|
|
eventAction: 'Generated Password From Command',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-17 16:11:09 +01:00
|
|
|
private async autoFillLogin(tab?: any) {
|
|
|
|
if (!tab) {
|
|
|
|
tab = await BrowserApi.getTabFromCurrentWindowId();
|
|
|
|
}
|
|
|
|
|
2017-12-07 21:06:37 +01:00
|
|
|
if (tab == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.main.collectPageDetailsForContentScript(tab, 'autofill_cmd');
|
|
|
|
|
|
|
|
(window as any).ga('send', {
|
|
|
|
hitType: 'event',
|
|
|
|
eventAction: 'Autofilled From Command',
|
|
|
|
});
|
|
|
|
}
|
2018-01-17 15:12:16 +01:00
|
|
|
|
|
|
|
private async openPopup() {
|
|
|
|
if (!this.isSafari || !safari.extension.toolbarItems || !safari.extension.toolbarItems.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
safari.extension.toolbarItems[0].showPopover();
|
2018-01-17 16:23:11 +01:00
|
|
|
|
|
|
|
(window as any).ga('send', {
|
|
|
|
hitType: 'event',
|
|
|
|
eventAction: 'Opened Popup From Command',
|
|
|
|
});
|
2018-01-17 15:12:16 +01:00
|
|
|
}
|
2017-12-07 21:06:37 +01:00
|
|
|
}
|