diff --git a/src/background.js b/src/background.js index b702068..3d04926 100644 --- a/src/background.js +++ b/src/background.js @@ -37,6 +37,27 @@ chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) { }); //----------------------------------- Function definitions ----------------------------------// + +/** + * Copy text to clipboard + * + * @since 3.0.0 + * + * @param string text Text to copy + * @return void + */ +function copyToClipboard(text) { + document.addEventListener( + "copy", + function(e) { + e.clipboardData.setData("text/plain", text); + e.preventDefault(); + }, + { once: true } + ); + document.execCommand("copy"); +} + /** * Get Local settings from the extension * @@ -162,6 +183,29 @@ async function handleMessage(settings, message, sendResponse) { }); } break; + case "copyPassword": + try { + copyToClipboard(message.login.fields.secret); + sendResponse({ status: "ok" }); + } catch (e) { + sendResponse({ + status: "error", + message: "Unable to copy password" + }); + } + break; + case "copyUsername": + try { + copyToClipboard(message.login.fields.login); + sendResponse({ status: "ok" }); + } catch (e) { + sendResponse({ + status: "error", + message: "Unable to copy username" + }); + } + break; + case "launch": try { var tab = (await chrome.tabs.query({ active: true, currentWindow: true }))[0]; diff --git a/src/popup/interface.js b/src/popup/interface.js index c1774d2..5d313cd 100644 --- a/src/popup/interface.js +++ b/src/popup/interface.js @@ -88,6 +88,13 @@ function view(ctl, params) { case "Enter": result.doAction("fill"); break; + case "KeyC": + if (e.ctrlKey) { + result.doAction( + e.shiftKey ? "copyUsername" : "copyPassword" + ); + } + break; case "KeyG": result.doAction("launch"); break; diff --git a/src/popup/searchinterface.js b/src/popup/searchinterface.js index e7da0e1..fb5ed06 100644 --- a/src/popup/searchinterface.js +++ b/src/popup/searchinterface.js @@ -90,6 +90,14 @@ function view(ctl, params) { } } break; + case "KeyC": + if (e.ctrlKey && e.target.selectionStart == e.target.selectionEnd) { + e.preventDefault(); + self.popup.results[0].doAction( + e.shiftKey ? "copyUsername" : "copyPassword" + ); + } + break; } } })