Skip to content

Commit

Permalink
fixes usernames and passwords with backslash are not being inserted p…
Browse files Browse the repository at this point in the history
…roperly.
  • Loading branch information
belaviyo committed Feb 7, 2017
1 parent 6d74586 commit b6623fe
Show file tree
Hide file tree
Showing 9 changed files with 360 additions and 169 deletions.
148 changes: 104 additions & 44 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,56 +37,70 @@ chrome.runtime.onMessage.addListener((request, sender, response) => {
}));
return true;
}
else if (cmd === 'url-is') {
else if (cmd === 'guesses') {
chrome.tabs.sendMessage(id, request);
}
else if (request.cmd.startsWith('insert-')) {
// escape "\"
request.password = (request.password || '').replace(/\\/g, '\\');
request.login = (request.login || '').replace(/\\/g, '\\\\');

chrome.tabs.executeScript(id, {
code: `
(function (success) {
if (success && ${cmd === 'insert-both'}) {
let form = aElement.closest('form');
if (form) {
let password = form.querySelector('[type=password]');
if (password) {
password.focus();
document.execCommand('selectAll', false, '');
let v = document.execCommand('insertText', false, '${request.password}');
if (v && '${request.detail}' !== 'no-submit') {
// submit
console.error(${request.detail});
let button = form.querySelector('input[type=submit]') || form.querySelector('[type=submit]');
if (button) {
button.click();
if (aElement) {
(function (success) {
if (!success) {
try {
aElement.value = '${cmd === 'insert-password' ? request.password : request.login}';
} catch (e) {}
}
if (${cmd === 'insert-both'}) {
let form = aElement.closest('form');
if (form) {
let password = form.querySelector('[type=password]');
if (password) {
password.focus();
document.execCommand('selectAll', false, '');
let v = document.execCommand('insertText', false, '${request.password}');
if (!v) {
try {
password.value = '${request.password}';
} catch (e) {}
}
else {
let onsubmit = form.getAttribute('onsubmit');
if (onsubmit && onsubmit.indexOf('return false') === -1) {
form.onsubmit();
if ('${request.detail}' !== 'no-submit') {
// submit
let button = form.querySelector('input[type=submit]') || form.querySelector('[type=submit]');
if (button) {
button.click();
}
else {
form.submit();
let onsubmit = form.getAttribute('onsubmit');
if (onsubmit && onsubmit.indexOf('return false') === -1) {
form.onsubmit();
}
else {
form.submit();
}
}
}
window.focus();
password.focus();
}
window.focus();
password.focus();
}
}
}
else {
aElement.focus();
window.focus();
}
})(
aElement &&
document.execCommand('selectAll', false, '') &&
document.execCommand(
'insertText',
false,
'${cmd === 'insert-password' ? request.password : request.login}'
)
);
else {
aElement.focus();
window.focus();
}
})(
document.execCommand('selectAll', false, '') &&
document.execCommand(
'insertText',
false,
'${cmd === 'insert-password' ? request.password : request.login}'
)
);
}
'';
`,
runAt: 'document_start',
Expand All @@ -95,20 +109,66 @@ chrome.runtime.onMessage.addListener((request, sender, response) => {
});
}
});

function notify (message) {
chrome.notifications.create({
title: 'KeePassHelper',
type: 'basic',
iconUrl: 'data/icons/128.png',
message
});
}

function copy (str) {
document.oncopy = (event) => {
event.clipboardData.setData('text/plain', str);
event.preventDefault();
};
document.execCommand('Copy', false, null);
}

// Context Menu
chrome.contextMenus.create({
id: 'open-commands',
id: 'open-keyboards',
title: 'Keyboard Shortcut Settings',
contexts: ['browser_action']
});
chrome.contextMenus.onClicked.addListener(() => chrome.tabs.create({
url: 'chrome://extensions/configureCommands'
}));
chrome.contextMenus.create({
id: 'generate-password',
title: 'Generate a Random Password',
contexts: ['browser_action']
});

chrome.contextMenus.onClicked.addListener((info) => {
if (info.menuItemId === 'open-keyboards') {
chrome.tabs.create({
url: 'chrome://extensions/configureCommands'
});
}
else {
chrome.storage.local.get({
charset: 'qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM1234567890',
length: 12
}, prefs => {
let password = Array.apply(null, new Array(prefs.length))
.map(() => prefs.charset.charAt(Math.floor(Math.random() * prefs.charset.length)))
.join('');
// copy to clipboard
copy(password);
// diplay notification
notify('Generated password is copied to the clipboard');
});
}
});

// FAQs & Feedback
chrome.storage.local.get('version', prefs => {
chrome.storage.local.get({
'version': null,
'faqs': navigator.userAgent.toLowerCase().indexOf('firefox') === -1 ? true : false
}, prefs => {
let version = chrome.runtime.getManifest().version;
let isFirefox = navigator.userAgent.indexOf('Firefox') !== -1;
if (isFirefox ? !prefs.version : prefs.version !== version) {

if (prefs.version ? (prefs.faqs && prefs.version !== version) : true) {
chrome.storage.local.set({version}, () => {
chrome.tabs.create({
url: 'http://add0n.com/keepass-helper.html?version=' + version +
Expand Down
39 changes: 17 additions & 22 deletions data/cmd/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,24 @@ input[type=search] {
div {
background-color: #282828;
}
body>div:not(:last-of-type) {
margin-bottom: 10px;
}
#list {
overflow-x: hidden;
overflow-y: auto;
}
#list:empty {
background: url('loading.svg') center center no-repeat;
}
#list span {
display: inline-block;
height: 26px;
line-height: 26px;
cursor: pointer;

select {
outline: none;
background-color: #282828;
color: #fff;
border: none;
margin: 10px 0;
width: 100%;
text-indent: 10px;
}
#list input:checked + span {
color: #fff;
background-color: #f18260;
font-weight: bold;
select:empty {
background-image: url('loading.svg');
background-repeat: no-repeat;
background-position: center center;
}
#list input {
display: none;
option {
padding: 5px;
}

#toolbar {
display: flex;
justify-content: space-around;
Expand All @@ -69,6 +61,9 @@ body>div:not(:last-of-type) {
#toolbar input {
padding: 0 10px;
}
#toolbar input:active {
opacity: 0.5;
}

[hbox] {
display: flex;
Expand Down
13 changes: 6 additions & 7 deletions data/cmd/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@
<link rel="stylesheet" href="index.css">
</head>
<body vbox>
<template id="entry">
<label><input type="radio" name="select"><span>Item 1</span></label>
</template>
<div>
<input type="search">
</div>
<div flex=1 vbox id="list"></div>
<select flex=1 id="list" size=5></select>
<div id="toolbar">
<input type="button" value="Close (ESC)" data-cmd="close">
<input type="button" value="Username (U)" title="Press Ctrl + U or Command + U to insert user-name" data-cmd="insert-login" disabled="true">
<input type="button" value="Password (P)" title="Press Ctrl + P or Command + P to insert password" data-cmd="insert-password" disabled="true">
<input type="button" value="Username & Password (B)" title="Press Ctrl + B or Command + B to perform {USERNAME}{TAB}{PASSWORD}{ENTER} sequence or press Ctrl + Shift + B or Command + Shift + B to perform {USERNAME}{TAB}{PASSWORD} sequence" data-cmd="insert-both" disabled="true">
<input type="button" value="Username (U)" title="Press Alt + U or Command + U to insert username" data-cmd="insert-login" disabled="true">
<input type="button" value="Password (P)" title="Press Alt + P or Command + P to insert password" data-cmd="insert-password" disabled="true">
<input type="button" value="Both (B)" title="Press Alt + B or Command + B to perform {USERNAME}{TAB}{PASSWORD} sequence or press Alt + Shift + B or Command + Shift + B to perform {USERNAME}{TAB}{PASSWORD}{ENTER} sequence" data-cmd="insert-both" disabled="true">
-
<input type="button" value="Copy (C/X)" title="Press Alt + C to copy username and press Alt + X to copy password to the clipboard" data-cmd="copy" disabled="true">
</div>
<script src="index.js"></script>
</body>
Expand Down
Loading

0 comments on commit b6623fe

Please sign in to comment.