mirror of
https://github.com/bitwarden/browser
synced 2025-12-19 09:43:23 +00:00
mousetrap keyboard shortcuts for safari
This commit is contained in:
@@ -2,15 +2,21 @@ import { BrowserApi } from '../browser/browserApi';
|
||||
|
||||
import MainBackground from './main.background';
|
||||
|
||||
import { PasswordGenerationService } from 'jslib/abstractions';
|
||||
import {
|
||||
PasswordGenerationService,
|
||||
PlatformUtilsService,
|
||||
} from 'jslib/abstractions';
|
||||
|
||||
import { UtilsService } from 'jslib/services/utils.service';
|
||||
|
||||
export default class CommandsBackground {
|
||||
private commands: any;
|
||||
private isSafari: boolean;
|
||||
|
||||
constructor(private main: MainBackground, private passwordGenerationService: PasswordGenerationService) {
|
||||
this.commands = chrome.commands;
|
||||
constructor(private main: MainBackground, private passwordGenerationService: PasswordGenerationService,
|
||||
private platformUtilsService: PlatformUtilsService) {
|
||||
this.isSafari = this.platformUtilsService.isSafari();
|
||||
this.commands = this.isSafari ? safari.application : chrome.commands;
|
||||
}
|
||||
|
||||
async init() {
|
||||
@@ -18,18 +24,34 @@ export default class CommandsBackground {
|
||||
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;
|
||||
}
|
||||
});
|
||||
if (this.isSafari) {
|
||||
this.commands.addEventListener('message', async (msgEvent: any) => {
|
||||
const msg = msgEvent.message;
|
||||
if (msg.command === 'keyboardShortcutTriggered' && msg.command.shortcut) {
|
||||
await this.processCommand(msg.command.shortcut);
|
||||
}
|
||||
}, false);
|
||||
} else {
|
||||
this.commands.onCommand.addListener(async (command: any) => {
|
||||
await this.processCommand(command);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private async processCommand(command: string) {
|
||||
switch (command) {
|
||||
case 'generate_password':
|
||||
await this.generatePasswordToClipboard();
|
||||
break;
|
||||
case 'autofill_login':
|
||||
await this.autoFillLogin();
|
||||
break;
|
||||
case 'open_popup':
|
||||
await this.openPopup();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private async generatePasswordToClipboard() {
|
||||
@@ -57,4 +79,12 @@ export default class CommandsBackground {
|
||||
eventAction: 'Autofilled From Command',
|
||||
});
|
||||
}
|
||||
|
||||
private async openPopup() {
|
||||
if (!this.isSafari || !safari.extension.toolbarItems || !safari.extension.toolbarItems.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
safari.extension.toolbarItems[0].showPopover();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,9 +149,10 @@ export default class MainBackground {
|
||||
this.runtimeBackground = new RuntimeBackground(this, this.autofillService, this.cipherService,
|
||||
this.platformUtilsService, this.storageService, this.i18nService);
|
||||
this.tabsBackground = new TabsBackground(this, this.platformUtilsService);
|
||||
this.commandsBackground = new CommandsBackground(this, this.passwordGenerationService,
|
||||
this.platformUtilsService);
|
||||
|
||||
if (!this.isSafari) {
|
||||
this.commandsBackground = new CommandsBackground(this, this.passwordGenerationService);
|
||||
this.contextMenusBackground = new ContextMenusBackground(this, this.cipherService,
|
||||
this.passwordGenerationService);
|
||||
this.idleBackground = new IdleBackground(this, this.lockService, this.storageService);
|
||||
@@ -166,9 +167,9 @@ export default class MainBackground {
|
||||
|
||||
await this.runtimeBackground.init();
|
||||
await this.tabsBackground.init();
|
||||
await this.commandsBackground.init();
|
||||
|
||||
if (!this.isSafari) {
|
||||
await this.commandsBackground.init();
|
||||
await this.contextMenusBackground.init();
|
||||
await this.idleBackground.init();
|
||||
await this.webRequestBackground.init();
|
||||
|
||||
Reference in New Issue
Block a user