Skip to content

Commit

Permalink
Add options page. Closes #1
Browse files Browse the repository at this point in the history
Options page lets you choose not to show specific accounts in the
drop down
  • Loading branch information
thomseddon committed Feb 15, 2014
1 parent ff93e77 commit eccca72
Show file tree
Hide file tree
Showing 7 changed files with 767 additions and 49 deletions.
7 changes: 6 additions & 1 deletion NOTICE
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
Twitcher
Copyright 2012-present Thom Seddon
Copyright 2012-present Thom Seddon

The majority of the CSS styling for the options page
was provided by the Chrome UI Bootstrap project
developed by Roy Kolak, available at:
https://github.com/roykolak/chrome-bootstrap
106 changes: 59 additions & 47 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,59 +47,18 @@ var accounts = {
});
},
getAll: function (callback) {
if (!callback) return this.accounts;
var self = this;
chrome.storage.sync.get('accounts', function (json) {
if (json.accounts && json.accounts.length) {
self.accounts = JSON.parse(json.accounts);
}
callback(self.accounts);
});
}
};

/**
* Convenience methods for accessing current account
*/

var currentAccount = {
change: function (uid) {
// Set token cookie
chrome.cookies.set({
url: 'https://twitter.com',
name: COOKIE_TOKEN,
value: accounts.get(uid).token,
domain: '.twitter.com',
path: '/',
secure: true,
httpOnly: true
});

// Remove user id cookie
chrome.cookies.remove({
url: 'https://twitter.com',
name: COOKIE_ID
});

// Reload
chrome.tabs.getSelected(null, function (tab) {
chrome.tabs.reload(tab.id);
});
},
save: function (user, callback) {
// Get the token
chrome.cookies.get({
url: 'https://twitter.com',
name: COOKIE_TOKEN
}, function (cookie) {
if (!cookie) return callback();

// Save cookie
accounts.set(user.uid, {
name: user.name,
img: user.img,
token: cookie.value
}, callback);
});
setAll: function (accounts, callback) {
this.accounts = accounts;
if (callback) this.save(callback);
}
};

Expand All @@ -123,12 +82,65 @@ chrome.extension.onMessage.addListener(function (request, sender, sendResponse)
});
break;
case 'switchAccount':
currentAccount.change(request.uid);
var account = accounts.get(request.uid);
if (account && !account.ignored) {
// Set token cookie
chrome.cookies.set({
url: 'https://twitter.com',
name: COOKIE_TOKEN,
value: account.token,
domain: '.twitter.com',
path: '/',
secure: true,
httpOnly: true
});

// Remove user id cookie
chrome.cookies.remove({
url: 'https://twitter.com',
name: COOKIE_ID
});
}

// Reload
chrome.tabs.getSelected(null, function (tab) {
chrome.tabs.reload(tab.id);
});
break;
case 'currentAccount':
// Make sure we have go all accounts before saving
accounts.getAll(function () {
currentAccount.save(request.currentAccount, sendResponse);
// Get the token
chrome.cookies.get({
url: 'https://twitter.com',
name: COOKIE_TOKEN
}, function (cookie) {
if (!cookie) return callback();

// Save cookie
var account = request.currentAccount;
accounts.set(account.uid, {
name: account.name,
img: account.img,
token: cookie.value
}, sendResponse);
});
});
break;
case 'ignore':
accounts.getAll(function (all) {
// Reset
for (var uid in all) {
all[uid].ignored = false;
}

// Update
request.ignore.forEach(function (uid) {
all[uid].ignored = true;
});

// Save
accounts.setAll(all, sendResponse);
});
break;
default:
Expand Down
Loading

0 comments on commit eccca72

Please sign in to comment.