Skip to content

Commit

Permalink
Refresh hotkeys on options save (fixes #3)
Browse files Browse the repository at this point in the history
Send a message to the background script telling it to reload the hotkeys from
Chrome storage when the user clicks save on the options page.
  • Loading branch information
jchang504 committed Apr 7, 2017
1 parent 457dc88 commit 4cf9271
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
55 changes: 27 additions & 28 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,18 @@ function closeCurrentTab() {
var mappings = {};

function get_mapped_domains() {
chrome.storage.sync.get({
hotkeys:[]
}, function(items){

for (hotkey_info of items.hotkeys){
var hotkey_map = {};
var hotkey = hotkey_info.hotkey;
hotkey_map.domain = hotkey_info.domain;
hotkey_map.deduplicate = hotkey_info.deduplicate;

mappings[hotkey] = hotkey_map;

}
});
mappings = {};
chrome.storage.sync.get({hotkeys: []}, function(items){
for (hotkey_info of items.hotkeys){
var hotkey_map = {};
var hotkey = hotkey_info.hotkey;
hotkey_map.domain = hotkey_info.domain;
hotkey_map.deduplicate = hotkey_info.deduplicate;
mappings[hotkey] = hotkey_map;
}
});
}

// TODO: Change this to get the hotkeys every time we receive one, so that user
// does not have to reload the background script after editing options for
// changes to take effect.
get_mapped_domains()

var NAV_LEFT_SYMBOL = '[';
Expand All @@ -102,7 +95,19 @@ var TAB_CLOSE_SYMBOL = ';';
var TAB_SEARCH_SYMBOL = '/';

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.hasOwnProperty("hotkey")) {
// Hold key event (pressed or released); broadcast to all tabs.
if (request.hasOwnProperty("holdKey")) {
var pressed = request.holdKey;
console.log("Broadcasting hold key " + (pressed ? "pressed" :
"released") + ".");
chrome.tabs.query({}, function(tabs) {
for (tab of tabs) {
chrome.tabs.sendMessage(tab.id, {holdKey: pressed});
}
});
}
// Hotkey sent.
else if (request.hasOwnProperty("hotkey")) {
var hotkey = request.hotkey;
console.log("Received hotkey: " + hotkey);

Expand Down Expand Up @@ -133,15 +138,9 @@ chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
}
}
}
// Hold key event (pressed or released); broadcast to all tabs.
else if (request.hasOwnProperty("holdKey")) {
var pressed = request.holdKey;
console.log("Broadcasting hold key " + (pressed ? "pressed" :
"released") + ".");
chrome.tabs.query({}, function(tabs) {
for (tab of tabs) {
chrome.tabs.sendMessage(tab.id, {holdKey: pressed});
}
});
// Refresh hotkeys after options edit.
else if (request.hasOwnProperty("refresh")) {
console.log("Refreshing hotkeys.");
get_mapped_domains();
}
});
2 changes: 2 additions & 0 deletions options.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ function saveOptions() {
chrome.storage.sync.set({
hotkeys: hotkeys
}, function() {
console.log("Sending refresh request to background script.");
chrome.runtime.sendMessage({refresh: true});
// Disable save button to indicate that options are saved.
$(SAVE_BUTTON_SELECTOR).prop(DISABLED, true);
});
Expand Down

0 comments on commit 4cf9271

Please sign in to comment.