Skip to content

Commit

Permalink
Added support for configurable special key
Browse files Browse the repository at this point in the history
  • Loading branch information
pix0r committed Feb 4, 2010
1 parent 23ec775 commit 16ee688
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
11 changes: 9 additions & 2 deletions addDelicious.js
@@ -1,6 +1,7 @@
(function() {
var localSettings = {
bookmarkKeyChar: 'D'
bookmarkKeyChar: 'D',
bookmarkSpecialKey: 'alt'
};

function getVariableFromLocalStorage(variableName,defaultValue) {
Expand All @@ -17,12 +18,18 @@
};

getVariableFromLocalStorage('bookmarkKeyChar', localSettings.bookmarkKeyChar);
getVariableFromLocalStorage('bookmarkSpecialKey', localSettings.bookmarkSpecialKey);

// Listen for key press
window.addEventListener(
'keydown',
function(e) {
if (e.which == localSettings.bookmarkKeyChar.charCodeAt(0) && e.altKey) {
if ((e.which == localSettings.bookmarkKeyChar.charCodeAt(0))
&& (
(localSettings.bookmarkSpecialKey == 'alt' && e.altKey)
|| (localSettings.bookmarkSpecialKey == 'ctrl' && e.ctrlKey)
|| (localSettings.bookmarkSpecialKey == 'meta' && e.metaKey)
)) {
console.log('pressed ^d (or whatever key)');
addDeliciousFromContentScript();
}
Expand Down
25 changes: 21 additions & 4 deletions options_shortcut.html
Expand Up @@ -11,18 +11,30 @@
$(document).ready(function() {
$(window).load(function(){
if(localStorage.getItem("bookmarkKeyChar")) {
var bookmarkKeyChar_stored = localStorage.getItem("bookmarkKeyChar");
var bookmarkKeyChar_stored = localStorage.getItem("bookmarkKeyChar"),
bookmarkSpecialKey_stored = localStorage.getItem("bookmarkSpecialKey");
$('#bookmarkKeyChar').val(bookmarkKeyChar_stored);
if (bookmarkSpecialKey_stored === null) {
// Default to alt
$('#bookmarkSpeiclaKey').val('alt');
} else {
$('#bookmarkSpecialKey').val(bookmarkSpecialKey_stored);
}
}
});
$('#save_options').submit(function(){
var bookmarkKeyChar = $('#bookmarkKeyChar').val();
var bookmarkKeyChar = $('#bookmarkKeyChar').val(),
bookmarkSpecialKey = $('#bookmarkSpecialKey').val();

localStorage.setItem("bookmarkKeyChar", bookmarkKeyChar);
localStorage.setItem("bookmarkSpecialKey", bookmarkSpecialKey);

if(!localStorage.getItem("bookmarkKeyChar")) {
alert('Please enter a Keyboard shortcut');
} else {
alert('Keyboard shortcut now set to ' + localStorage.getItem("bookmarkKeyChar"));
alert('Keyboard shortcut now set to ' + localStorage.getItem("bookmarkKeyChar") + ', specialKey set to ' + localStorage.getItem('bookmarkSpecialKey'));
}

return false;
});
$('#bookmarkKeyChar').keyup(function() {
Expand All @@ -39,7 +51,12 @@
<form id="save_options" method="get">
<fieldset>
<legend>Keyboard Shortcut:</legend>
<input type="text" name="bookmarkKeyChar" id="bookmarkKeyChar" value="" />
<select name="bookmarkSpecialKey" id="bookmarkSpecialKey">
<option value="alt">Alt</option>
<option value="ctrl">Control</option>
<option value="meta">Meta</option>
</select>
<input type="text" name="bookmarkKeyChar" id="bookmarkKeyChar" value="" size="1" maxlength="1" />
<p><input type="submit" value="Save Options" /></p>
</fieldset>
</form>
Expand Down

0 comments on commit 16ee688

Please sign in to comment.