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-09 23:55:28 +01:00
|
|
|
import { 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 21:06:37 +01:00
|
|
|
|
|
|
|
export default class CommandsBackground {
|
|
|
|
private commands: any;
|
|
|
|
|
|
|
|
constructor(private main: MainBackground, private passwordGenerationService: PasswordGenerationService) {
|
|
|
|
this.commands = chrome.commands;
|
|
|
|
}
|
|
|
|
|
|
|
|
async init() {
|
|
|
|
if (!this.commands) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.commands.onCommand.addListener(async (command: any) => {
|
|
|
|
switch (command) {
|
|
|
|
case 'generate_password':
|
|
|
|
await this.generatePasswordToClipboard();
|
|
|
|
break;
|
|
|
|
case 'autofill_login':
|
|
|
|
await this.autoFillLogin();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private async generatePasswordToClipboard() {
|
|
|
|
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',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private async autoFillLogin() {
|
|
|
|
const tab = await BrowserApi.getTabFromCurrentWindowId();
|
|
|
|
if (tab == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.main.collectPageDetailsForContentScript(tab, 'autofill_cmd');
|
|
|
|
|
|
|
|
(window as any).ga('send', {
|
|
|
|
hitType: 'event',
|
|
|
|
eventAction: 'Autofilled From Command',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|