From c942dd34105911f4a4132469eba7c7715426102f Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Wed, 17 Jan 2018 13:38:32 -0500 Subject: [PATCH] add support for shortcuts to edge and vivaldi --- src/background/commands.background.ts | 13 ++++++--- src/content/shortcuts.js | 28 +++++++++++--------- src/manifest.json | 38 ++++++++++----------------- 3 files changed, 39 insertions(+), 40 deletions(-) diff --git a/src/background/commands.background.ts b/src/background/commands.background.ts index ce206442ef..9fbf977726 100644 --- a/src/background/commands.background.ts +++ b/src/background/commands.background.ts @@ -12,22 +12,26 @@ 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, private platformUtilsService: PlatformUtilsService) { 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) { + if (!this.commands && !this.isEdge) { return; } - if (this.isSafari) { + 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); + await this.processCommand(msg.shortcut, sender); } }); } else { @@ -54,7 +58,7 @@ export default class CommandsBackground { } private async generatePasswordToClipboard() { - if (this.isSafari) { + if (this.isSafari || this.isEdge) { // Safari does not support access to clipboard from background return; } @@ -88,6 +92,7 @@ export default class CommandsBackground { } private async openPopup() { + // Chrome APIs cannot open popup if (!this.isSafari || !safari.extension.toolbarItems || !safari.extension.toolbarItems.length) { return; } diff --git a/src/content/shortcuts.js b/src/content/shortcuts.js index 7e3f73af60..7d7ef3f825 100644 --- a/src/content/shortcuts.js +++ b/src/content/shortcuts.js @@ -1,8 +1,10 @@ document.addEventListener('DOMContentLoaded', (event) => { const isSafari = (typeof safari !== 'undefined') && navigator.userAgent.indexOf(' Safari/') !== -1 && navigator.userAgent.indexOf('Chrome') === -1; + const isEdge = !isSafari && navigator.userAgent.indexOf(' Edge/') !== -1; + const isVivaldi = !isSafari && navigator.userAgent.indexOf(' Vivaldi/') !== -1; - if (!isSafari) { + if (!isSafari && !isEdge && !isVivaldi) { return; } @@ -23,29 +25,31 @@ document.addEventListener('DOMContentLoaded', (event) => { return false; }; - const autofillCommand = isSafari ? 'mod+\\' : 'mod+shift+l'; + const autofillCommand = isSafari || isEdge ? 'mod+\\' : 'mod+shift+l'; Mousetrap.bind(autofillCommand, () => { sendMessage('autofill_login'); }); - Mousetrap.bind('mod+shift+y', () => { - sendMessage('open_popup'); - }); - - if (!isSafari) { + if (isSafari) { + Mousetrap.bind('mod+shift+y', () => { + sendMessage('open_popup'); + }); + } else if (!this.isEdge) { Mousetrap.bind('mod+shift+9', () => { sendMessage('generate_password'); }); } function sendMessage(shortcut) { + const msg = { + command: 'keyboardShortcutTriggered', + shortcut: shortcut + }; + if (isSafari) { - safari.self.tab.dispatchMessage('bitwarden', { - command: 'keyboardShortcutTriggered', - shortcut: shortcut - }); + safari.self.tab.dispatchMessage('bitwarden', msg); } else { - // other browsers not supported at this time. + chrome.runtime.sendMessage(msg); } } }); diff --git a/src/manifest.json b/src/manifest.json index 14b53561e0..15411aff66 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -18,30 +18,8 @@ { "all_frames": true, "js": [ - "content/autofill.js" - ], - "matches": [ - "http://*/*", - "https://*/*", - "file:///*" - ], - "run_at": "document_start" - }, - { - "all_frames": true, - "js": [ - "content/autofiller.js" - ], - "matches": [ - "http://*/*", - "https://*/*", - "file:///*" - ], - "run_at": "document_start" - }, - { - "all_frames": true, - "js": [ + "content/autofill.js", + "content/autofiller.js", "content/notificationBar.js" ], "matches": [ @@ -51,6 +29,18 @@ ], "run_at": "document_start" }, + { + "all_frames": false, + "js": [ + "content/shortcuts.js" + ], + "matches": [ + "http://*/*", + "https://*/*", + "file:///*" + ], + "run_at": "document_start" + }, { "all_frames": true, "css": [