mirror of
https://github.com/bitwarden/browser
synced 2025-12-16 00:03:56 +00:00
separating background apis to their own classes
This commit is contained in:
59
src/background/commands.background.ts
Normal file
59
src/background/commands.background.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import BrowserApi from '../browser/browserApi';
|
||||
|
||||
import MainBackground from './main.background';
|
||||
|
||||
import PasswordGenerationService from '../services/passwordGeneration.service';
|
||||
import UtilsService from '../services/utils.service';
|
||||
|
||||
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();
|
||||
const password = PasswordGenerationService.generatePassword(options);
|
||||
UtilsService.copyToClipboard(password);
|
||||
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',
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user