Skip to content
Browse files

fix all the things

  • Loading branch information...
1 parent f00debf commit 4e745ecda91f30b1191198083afe6ff1f37051eb @astreet committed Mar 20, 2012
Showing with 105 additions and 44 deletions.
  1. +1 −0 extension/background.html
  2. +30 −8 extension/background.js
  3. +9 −12 extension/content_script.js
  4. +42 −11 extension/settings.js
  5. +19 −13 extension/subreddit_settings.html
  6. +4 −0 extension/utils.js
View
1 extension/background.html
@@ -1,6 +1,7 @@
<html>
<head>
<title>Background Page</title>
+ <script type="text/javascript" src="lib/jquery.min.js"></script>
<script src="background.js"></script>
</head>
<body>
View
38 extension/background.js
@@ -1,10 +1,32 @@
-chrome.extension.onRequest.addListener(function(request, sender, resp) {
- if (request.fun === 'show_subreddit_settings') {
- chrome.windows.create({
- 'url': chrome.extension.getURL('subreddit_settings.html'),
- 'type': 'popup',
- 'width': 300,
- 'height': 400
- });
+chrome.extension.onRequest.addListener(function(request, sender, respond) {
+ console.log(localStorage);
+ switch (request.fun) {
+ case 'show_subreddit_settings':
+ chrome.windows.create({
+ 'url': chrome.extension.getURL('subreddit_settings.html'),
+ 'type': 'popup',
+ 'width': 300,
+ 'height': 400
+ });
+ break;
+ case 'settings_set':
+ localStorage[request.key] = request.value;
+ break;
+ case 'settings_multiget':
+ response = {};
+ $.each(request.keys, function(_, key) {
+ response[key] = localStorage[key];
+ })
+ respond({'values': response});
+ break;
+ case 'settings_get_all':
+ all = {};
+ $.each(Object.keys(localStorage), function(_, key) {
+ all[key] = localStorage[key];
+ })
+ respond({settings: all});
+ break;
+ default:
+ console.log('Received unknown message function: ' + request.fun);
}
});
View
21 extension/content_script.js
@@ -18,7 +18,6 @@ $(function() {
}
function setStatusOnResponse(response) {
- console.log('asdasdasdasdasdasdasdas');
switch (response.status) {
case 'connected':
accessToken = response.authResponse.accessToken;
@@ -32,8 +31,6 @@ $(function() {
var MAX_RETRIES = 5;
function postActionWithRetry(action, obj, retry_count) {
if (!connected || !shouldPublish(action, obj.getSubreddit())) {
- console.log(obj.getSubreddit());
- console.log(connected);
console.error('Not publishing');
return;
}
@@ -108,23 +105,24 @@ $(function() {
];
$.each(DEFAULT_SUBREDDITS, function(_, subreddit) {
- localStorage['settings.subreddit.' + subreddit.toLowerCase()] = 'enabled';
+ settings_set('settings.subreddit.' + subreddit.toLowerCase(), 'enabled');
})
- localStorage['settings.hasRunBefore'] = true;
+ settings_set('settings.hasRunBefore', true);
}
$('body').append(
'<div id="ogreddit" class="closed">' +
'<div id="fb-root" />' +
+ '<div class="content" />' +
'</div>'
);
- if (!localStorage['settings.hasRunBefore']) {
- firstRun();
- }
+ settings_get('settings.hasRunBefore', function(value) {
+ !value && firstRun();
+ });
- $('#ogreddit').load(chrome.extension.getURL('settings.html'), function() {
+ $('#ogreddit .content').load(chrome.extension.getURL('settings.html'), function() {
var settings = $('#ogreddit .settings');
setupSettings(settings);
$('#ogreddit .settingsNib').click(function() {
@@ -147,14 +145,13 @@ $(function() {
}
});
- FB.Event.subscribe('auth.statusChange', setStatusOnResponse);
FB.init({
appId : '288106721255039',
status : true,
cookie : true,
xfbml : true,
oauth : true,
});
- FB.getLoginStatus(function() {alert('asda');});
- console.log('done');
+
+ FB.Event.subscribe('auth.statusChange', setStatusOnResponse);
});
View
53 extension/settings.js
@@ -9,10 +9,36 @@ var SELECTOR_TO_SETTINGS_MAP = {
'#publish_post': 'settings.publish.post'
}
-function shouldPublish(action_name, subreddit_name) {
- return localStorage['settings.publish.killswitch'] != 'false'
- && localStorage['settings.publish.' + action_name] != 'false'
- && localStorage['settings.subreddit.' + subreddit_name.toLowerCase()] == 'enabled';
+function settings_get(key, callback) {
+ settings_multiget([key], function(values) {
+ callback(values[key]);
+ });
+}
+
+function settings_multiget(keys, callback) {
+ chrome.extension.sendRequest({fun: 'settings_multiget', keys: keys}, function(resp) {
+ callback(resp.values);
+ });
+}
+
+function settings_set(key, value) {
+ chrome.extension.sendRequest({fun: 'settings_set', key: key, value: value});
+}
+
+function settings_get_all(callback) {
+ chrome.extension.sendRequest({fun: 'settings_get_all'}, function(resp) {
+ callback(resp.settings);
+ });
+}
+
+function guardedPublish(action_name, subreddit_name, publish_function) {
+ settings_get_all(function(settings) {
+ if (settings['settings.publish.killswitch'] != 'false'
+ && settings['settings.publish.' + action_name] != 'false'
+ && settings['settings.subreddit.' + subreddit_name.toLowerCase()] == 'enabled') {
+ publish_function();
+ }
+ })
}
function setupSettings(settings) {
@@ -24,6 +50,7 @@ function setupSettings(settings) {
killswitch.change(function(e) {
reloadPublishKillswitch();
});
+ reloadPublishKillswitch();
settings.find('a.subredditSettingsLink').click(function() {
chrome.extension.sendRequest({fun: 'show_subreddit_settings'});
@@ -33,11 +60,13 @@ function setupSettings(settings) {
function reloadPublishKillswitch() {
var settings = $('#ogreddit .settings');
var children = settings.find('.publishSubsettings input');
- if (localStorage[SELECTOR_TO_SETTINGS_MAP['#publish_killswitch']] == 'false') {
- children.attr('disabled', 'disabled');
- } else {
- children.removeAttr('disabled');
- }
+ settings_get(SELECTOR_TO_SETTINGS_MAP['#publish_killswitch'], function(value) {
+ if (value == 'false') {
+ children.attr('disabled', 'disabled');
+ } else {
+ children.removeAttr('disabled');
+ }
+ });
}
function reloadSettings() {
@@ -48,13 +77,15 @@ function reloadSettings() {
}
function reloadSettingsCheckbox(checkbox, setting_name) {
- checkbox.attr('checked', localStorage[setting_name] != 'false');
+ settings_get(setting_name, function(value) {
+ checkbox.attr('checked', value != 'false');
+ });
}
function setupSettingsCheckbox(checkbox, setting_name) {
reloadSettingsCheckbox(checkbox, setting_name);
checkbox.change(function (event) {
- localStorage[setting_name] = (checkbox.attr('checked') != undefined);
+ settings_set(setting_name, checkbox.attr('checked') != undefined);
});
}
View
32 extension/subreddit_settings.html
@@ -1,40 +1,46 @@
<html>
+ <body class="subredditSettings">
+ <button onclick="save()">Save</button>
+ </body>
<script type="text/javascript" src="lib/jquery.min.js"></script>
+ <script type="text/javascript" src="utils.js"></script>
<script type="text/javascript">
- console.log('asd');
- console.log(Object.keys(localStorage));
+ var table = $('<table></table>');
$.each(Object.keys(localStorage), function(_, key) {
console.log(key);
if (!key.startsWith('settings.subreddit.')) {
return;
}
- var subreddit = key.substring(18);
+ var subreddit = key.substring(19);
var value = localStorage[key];
var setting = $(
- '<div class="subredditSetting">' +
- '<div>' + subreddit + ':</div>' +
- '<select>' +
+ '<tr class="subredditSetting">' +
+ '<td class="settingName">' + subreddit + ':</td>' +
+ '<td><select>' +
'<option value="disabled">Disabled</option>' +
'<option value="enabled">Enabled</option>' +
- '</select>' +
- '</div>'
+ '</select></td>' +
+ '</tr>'
);
setting.find('option[value="' + value + '"]').attr('selected', 1);
- $('body').append(setting);
+ table.append(setting);
});
function save() {
- $('.subredditSettings .subredditSetting').each(function(e) {
+ $('.subredditSettings .subredditSetting .settingName').each(function(_, e) {
+ e = $(e);
var subreddit = e.text().toLowerCase();
var value = e.find('select option:selected').attr('value');
+ console.log('Saving ' + subreddit + " = " + value);
localStorage['settings.subreddit.' + subreddit] = value;
});
+
+ window.close();
}
+
+ $('body').prepend(table);
</script>
- <body class="subredditSettings">
- <button onclick="save()">Save</button>
- </body>
</html>
View
4 extension/utils.js
@@ -21,4 +21,8 @@ function copy_into(obj, properties) {
function is_image(uri) {
return uri.match('(gif|png|jpg|bmp)$');
+}
+
+String.prototype.startsWith = function(str) {
+ return this.substring(0, Math.min(str.length, this.length)) === str;
}

0 comments on commit 4e745ec

Please sign in to comment.
Something went wrong with that request. Please try again.