Skip to content

Commit

Permalink
Add copy actions (browserpass#8)
Browse files Browse the repository at this point in the history
* Add copy actions
* Add hotkeys to search box
  • Loading branch information
erayd authored Apr 21, 2018
1 parent a2d92e9 commit fb37519
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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];
Expand Down
7 changes: 7 additions & 0 deletions src/popup/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions src/popup/searchinterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
})
Expand Down

0 comments on commit fb37519

Please sign in to comment.