bitwarden-estensione-browser/src/background/commands.background.ts

109 lines
3.3 KiB
TypeScript
Raw Normal View History

2018-01-12 17:09:30 +01:00
import { BrowserApi } from '../browser/browserApi';
import MainBackground from './main.background';
2018-01-26 16:48:32 +01:00
import { Analytics } from 'jslib/misc';
import {
PasswordGenerationService,
PlatformUtilsService,
} from 'jslib/abstractions';
2018-01-09 20:26:20 +01:00
import { UtilsService } from 'jslib/services/utils.service';
export default class CommandsBackground {
private commands: any;
private isSafari: boolean;
private isEdge: boolean;
private isVivaldi: boolean;
constructor(private main: MainBackground, private passwordGenerationService: PasswordGenerationService,
2018-01-19 22:19:24 +01:00
private platformUtilsService: PlatformUtilsService, private analytics: Analytics) {
this.isSafari = this.platformUtilsService.isSafari();
this.isEdge = this.platformUtilsService.isEdge();
this.isVivaldi = this.platformUtilsService.isVivaldi();
this.commands = this.isSafari ? safari.application : chrome.commands;
}
async init() {
if (!this.commands && !this.isEdge) {
return;
}
if (this.isSafari || this.isEdge || this.isVivaldi) {
BrowserApi.messageListener(async (msg: any, sender: any, sendResponse: any) => {
if (msg.command === 'keyboardShortcutTriggered' && msg.shortcut) {
await this.processCommand(msg.shortcut, sender);
}
});
} else {
this.commands.onCommand.addListener(async (command: any) => {
await this.processCommand(command);
});
}
}
private async processCommand(command: string, sender?: any) {
switch (command) {
case 'generate_password':
await this.generatePasswordToClipboard();
break;
case 'autofill_login':
await this.autoFillLogin(sender ? sender.tab : null);
break;
case 'open_popup':
await this.openPopup();
break;
default:
break;
}
}
private async generatePasswordToClipboard() {
if (this.isSafari || this.isEdge) {
// Safari does not support access to clipboard from background
return;
}
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);
this.passwordGenerationService.addHistory(password);
2018-01-19 22:19:24 +01:00
this.analytics.ga('send', {
hitType: 'event',
eventAction: 'Generated Password From Command',
});
}
private async autoFillLogin(tab?: any) {
if (!tab) {
tab = await BrowserApi.getTabFromCurrentWindowId();
}
if (tab == null) {
return;
}
this.main.collectPageDetailsForContentScript(tab, 'autofill_cmd');
2018-01-19 22:19:24 +01:00
this.analytics.ga('send', {
hitType: 'event',
eventAction: 'Autofilled From Command',
});
}
private async openPopup() {
// Chrome APIs cannot open popup
2018-01-18 22:17:58 +01:00
if (!this.isSafari) {
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',
});
}
}