diff --git a/package-lock.json b/package-lock.json index 75b86535e6..b228112e1d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -205,6 +205,12 @@ "integrity": "sha512-esk3CG25hRtHsVHm+LOjiSFYdw8be3uIY653WUwR43Bro914HSimPgPpqgajkhTJ0awK3RQfaIxP7zvbtCpcyg==", "dev": true }, + "@types/mousetrap": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/@types/mousetrap/-/mousetrap-1.6.0.tgz", + "integrity": "sha512-Jn2cF8X6RAMiSmJaATGjf2r3GzIfpZQpvnQhKprQ5sAbMaNXc7hc9sA2XHdMl3bEMEQhTV79JVW7n4Pgg7sjtg==", + "dev": true + }, "@types/node": { "version": "8.0.19", "resolved": "https://registry.npmjs.org/@types/node/-/node-8.0.19.tgz", @@ -7205,6 +7211,11 @@ "minimist": "0.0.8" } }, + "mousetrap": { + "version": "1.6.1", + "resolved": "https://registry.npmjs.org/mousetrap/-/mousetrap-1.6.1.tgz", + "integrity": "sha1-KghfXHUSlMdefoH27CVFspy/Qtk=" + }, "move-concurrently": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/move-concurrently/-/move-concurrently-1.0.1.tgz", diff --git a/package.json b/package.json index cf60bc3f90..c4122c49f5 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "@types/jasmine": "^2.8.2", "@types/jquery": "^3.2.16", "@types/lunr": "2.1.5", + "@types/mousetrap": "^1.6.0", "@types/node": "8.0.19", "@types/node-forge": "0.6.10", "@types/papaparse": "4.1.33", @@ -86,6 +87,7 @@ "core-js": "2.4.1", "font-awesome": "4.7.0", "lunr": "2.1.6", + "mousetrap": "1.6.1", "ngx-infinite-scroll": "0.8.4", "node-forge": "0.7.1", "papaparse": "4.3.5", diff --git a/src/content/shortcuts.js b/src/content/shortcuts.js deleted file mode 100644 index 7d7ef3f825..0000000000 --- a/src/content/shortcuts.js +++ /dev/null @@ -1,55 +0,0 @@ -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 && !isEdge && !isVivaldi) { - return; - } - - /* mousetrap v1.6.1 craig.is/killing/mice */ - (function(r,v,f){function w(a,b,g){a.addEventListener?a.addEventListener(b,g,!1):a.attachEvent("on"+b,g)}function A(a){if("keypress"==a.type){var b=String.fromCharCode(a.which);a.shiftKey||(b=b.toLowerCase());return b}return p[a.which]?p[a.which]:t[a.which]?t[a.which]:String.fromCharCode(a.which).toLowerCase()}function F(a){var b=[];a.shiftKey&&b.push("shift");a.altKey&&b.push("alt");a.ctrlKey&&b.push("ctrl");a.metaKey&&b.push("meta");return b}function x(a){return"shift"==a||"ctrl"==a||"alt"==a|| - "meta"==a}function B(a,b){var g,c,d,f=[];g=a;"+"===g?g=["+"]:(g=g.replace(/\+{2}/g,"+plus"),g=g.split("+"));for(d=0;dq||p.hasOwnProperty(q)&&(n[p[q]]=q)}d=n[g]?"keydown":"keypress"}"keypress"==d&&f.length&&(d="keydown");return{key:c,modifiers:f,action:d}}function E(a,b){return null===a||a===v?!1:a===b?!0:E(a.parentNode,b)}function c(a){function b(a){a= - a||{};var b=!1,l;for(l in n)a[l]?b=!0:n[l]=0;b||(y=!1)}function g(a,b,u,e,c,g){var l,m,k=[],f=u.type;if(!h._callbacks[a])return[];"keyup"==f&&x(a)&&(b=[a]);for(l=0;l":".","?":"/","|":"\\"},C={option:"alt",command:"meta","return":"enter", - escape:"esc",plus:"+",mod:/Mac|iPod|iPhone|iPad/.test(navigator.platform)?"meta":"ctrl"},n;for(f=1;20>f;++f)p[111+f]="f"+f;for(f=0;9>=f;++f)p[f+96]=f.toString();c.prototype.bind=function(a,b,c){a=a instanceof Array?a:[a];this._bindMultiple.call(this,a,b,c);return this};c.prototype.unbind=function(a,b){return this.bind.call(this,a,function(){},b)};c.prototype.trigger=function(a,b){if(this._directMap[a+":"+b])this._directMap[a+":"+b]({},a);return this};c.prototype.reset=function(){this._callbacks={}; - this._directMap={};return this};c.prototype.stopCallback=function(a,b){return-1<(" "+b.className+" ").indexOf(" mousetrap ")||E(b,this.target)?!1:"INPUT"==b.tagName||"SELECT"==b.tagName||"TEXTAREA"==b.tagName||b.isContentEditable};c.prototype.handleKey=function(){return this._handleKey.apply(this,arguments)};c.addKeycodes=function(a){for(var b in a)a.hasOwnProperty(b)&&(p[b]=a[b]);n=null};c.init=function(){var a=c(v),b;for(b in a)"_"!==b.charAt(0)&&(c[b]=function(b){return function(){return a[b].apply(a, - arguments)}}(b))};c.init();r.Mousetrap=c;"undefined"!==typeof module&&module.exports&&(module.exports=c);"function"===typeof define&&define.amd&&define(function(){return c})}})("undefined"!==typeof window?window:null,"undefined"!==typeof window?document:null); - /* mousetrap v1.6.1 craig.is/killing/mice */ - - Mousetrap.prototype.stopCallback = () => { - return false; - }; - - const autofillCommand = isSafari || isEdge ? 'mod+\\' : 'mod+shift+l'; - Mousetrap.bind(autofillCommand, () => { - sendMessage('autofill_login'); - }); - - 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', msg); - } else { - chrome.runtime.sendMessage(msg); - } - } -}); diff --git a/src/content/shortcuts.ts b/src/content/shortcuts.ts new file mode 100644 index 0000000000..20d210b248 --- /dev/null +++ b/src/content/shortcuts.ts @@ -0,0 +1,44 @@ +import * as Mousetrap from 'mousetrap'; + +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 && !isEdge && !isVivaldi) { + return; + } + + Mousetrap.prototype.stopCallback = () => { + return false; + }; + + const autofillCommand = isSafari || isEdge ? 'mod+\\' : 'mod+shift+l'; + Mousetrap.bind(autofillCommand, () => { + sendMessage('autofill_login'); + }); + + 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: string) { + const msg = { + command: 'keyboardShortcutTriggered', + shortcut: shortcut, + }; + + if (isSafari) { + safari.self.tab.dispatchMessage('bitwarden', msg); + } else { + chrome.runtime.sendMessage(msg); + } + } +}); diff --git a/webpack.config.js b/webpack.config.js index c5a1bc8799..71b2aba976 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -162,7 +162,7 @@ const config = { 'content/autofill': './src/content/autofill.js', 'content/autofiller': './src/content/autofiller.ts', 'content/notificationBar': './src/content/notificationBar.ts', - 'content/shortcuts': './src/content/shortcuts.js', + 'content/shortcuts': './src/content/shortcuts.ts', 'notification/bar': './src/notification/bar.js', 'downloader/downloader': './src/downloader/downloader.ts', '2fa/2fa': './src/2fa/2fa.ts',