Skip to content

Commit

Permalink
Use chrome's sync'd storage api instead of localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
EranSch committed Sep 10, 2014
1 parent 4e67959 commit 2f25d95
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
41 changes: 26 additions & 15 deletions src/lib/brain.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,42 @@
var wpAdminHide = {

/*
* Adds a domain name to localStorage
* Adds a domain name to Chrome storage
*/
addD: function(d) {
localStorage[d] = true;
console.log(d + " added.");
var save = {};
save[d] = true;
chrome.storage.sync.set(save, function(result) {
console.log(d, "added to storage, admin bar removed.");
});
},

/*
* Removes a domain from localStorage
* Removes a domain from Chrome storage
*/
remD: function(d) {
localStorage.removeItem(d);
console.log(d + " removed.");
chrome.storage.sync.remove(d, function() {
console.log(d, "removed from storage, admin bar restored.");
});
},

/*
* Checks for the existence of a localStorage item that matches the domain passed as the first
* Checks for the existence of a Chrome storage item that matches the domain passed as the first
* argument. Second and third arguments are what to do if there is or isn't a match, respectively.
*/
chkD: function(tabId, pass, fail) {
chrome.tabs.get(tabId, function(tab) {
d = tab.url.split("/")[2];
if (localStorage[d]) {
pass(d);
} else {
fail(d);
if(tab.url.indexOf('chrome') == 0){
console.error('can\'t run on chrome pages, sorry :(');
}else{
d = tab.url.split("/")[2];
chrome.storage.sync.get(d, function(result){
if (result[d]) {
pass(d);
} else {
fail(d);
}
});
}
});
},
Expand Down Expand Up @@ -79,11 +89,12 @@ var wpAdminHide = {
});
}
}

};

/*
* Listener for browser action button clickage. Checks the active tab against localstorage and toggles
* the state of the plugin accordingly
* Listener for browser action button clickage. Checks the active tab against Chrome storage
* and toggles the state of the plugin accordingly.
*/
chrome.browserAction.onClicked.addListener(function(tab) {
wpAdminHide.chkD(
Expand All @@ -101,7 +112,7 @@ chrome.browserAction.onClicked.addListener(function(tab) {

/*
* Keep the icon up to date based on whether or not the domain exists in local storage.
* Going to assum that the hiding was handled on tab load so this is just for keeping up
* Going to assume that the hiding was handled on tab load so this is just for keeping up
* appearances.
*/
chrome.tabs.onActivated.addListener(function(tab) {
Expand Down
6 changes: 4 additions & 2 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

"name": "Wordpress Admin Bar Control",
"short_name": "WP Admin Bar Hider",
"version": "1.0.3",
"version": "1.1.0",
"description": "Hide & show the admin bar with one easy click!",

"browser_action": {
Expand All @@ -15,7 +15,9 @@
},

"permissions": [
"tabs", "*://*/*"
"storage",
"tabs",
"*://*/*"
],

"background":{
Expand Down

0 comments on commit 2f25d95

Please sign in to comment.