From 1d1bf2f46053a2b041785f60df576150977e72db Mon Sep 17 00:00:00 2001 From: Kerrick Long Date: Sun, 29 Apr 2012 00:37:31 -0500 Subject: [PATCH 01/12] closes #21; added permalink to submitted comment --- js/common.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/js/common.js b/js/common.js index a39e7ed..e5ad74a 100644 --- a/js/common.js +++ b/js/common.js @@ -737,9 +737,11 @@ RedditAPI.prototype.submitComment = function (e) { var listItem, fullName, status, submitButton, cancelButton, textarea, comment, formData; function success (response) { - var url, oldCache; + var postedCommentUrl, commentsUrl, url, oldCache; - status.innerHTML = ''; + postedCommentUrl = response.jquery[18][3][0][0].data.id.split('_')[1]; + commentsUrl = document.getElementById(fullName).parentNode.getElementsByClassName('thumblink')[0].getAttribute('href'); + status.innerHTML = 'submitted! click to view your comment.'; submitButton.innerHTML = chrome.i18n.getMessage('button_submit'); cancelButton.innerHTML = chrome.i18n.getMessage('button_hide'); submitButton.setAttribute('disabled'); From e145caf818038ce252a2079728a4b10b5756e541 Mon Sep 17 00:00:00 2001 From: Kerrick Long Date: Sun, 29 Apr 2012 01:10:42 -0500 Subject: [PATCH 02/12] adding extra data- attributes for future development --- js/common.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/common.js b/js/common.js index e5ad74a..2b9feb6 100644 --- a/js/common.js +++ b/js/common.js @@ -1012,7 +1012,7 @@ Popup.prototype.createListHTML = function (url) { freshText = isFreshEnough ? 'fresh' : 'stale'; thumbSrc = data.thumbnail.indexOf('/') === 0 ? 'http://www.reddit.com' + data.thumbnail : data.thumbnail; - listHTML += '
  • '; + listHTML += '
  • '; listHTML += '
    '; listHTML += ''; listHTML += '' + data.score + ''; From 128f4ad9f08181eba133112ccaaa9a044051b3ee Mon Sep 17 00:00:00 2001 From: Kerrick Long Date: Sun, 29 Apr 2012 01:11:16 -0500 Subject: [PATCH 03/12] vote UP now changes score --- js/common.js | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/js/common.js b/js/common.js index 2b9feb6..9f2f7aa 100644 --- a/js/common.js +++ b/js/common.js @@ -463,18 +463,51 @@ RedditAPI.prototype.getInfo = function (url, tabId) { * @method */ RedditAPI.prototype.voteUpPost = function (e) { - var listItem, fullName, url, reqUrl, oldCache, voteWas, formData; + var listItem, fullName, url, reqUrl, oldCache, voteWas, scoreWas, upsWas, downsWas, scoreElem, formData; function success () { if (voteWas === '1') { + listItem.setAttribute('data-dir','0'); oldCache.posts[fullName].data.likes = null; + + listItem.setAttribute('data-score', scoreWas - 1); + oldCache.posts[fullName].data.score = scoreWas - 1; + scoreElem.innerHTML = scoreWas - 1; + scoreElem.setAttribute('title', chrome.i18n.getMessage('score', [(upsWas - 1).toString(), downsWas.toString()])); + + listItem.setAttribute('data-ups', upsWas - 1); + oldCache.posts[fullName].data.ups = upsWas - 1; + } else if (voteWas === '0') { + listItem.setAttribute('data-dir','1'); oldCache.posts[fullName].data.likes = true; + + listItem.setAttribute('data-score', scoreWas + 1); + oldCache.posts[fullName].data.score = scoreWas + 1; + scoreElem.innerHTML = scoreWas + 1; + scoreElem.setAttribute('title', chrome.i18n.getMessage('score', [(upsWas + 1).toString(), downsWas.toString()])); + + listItem.setAttribute('data-ups', upsWas + 1); + oldCache.posts[fullName].data.ups = upsWas + 1; + } else if (voteWas === '-1') { + listItem.setAttribute('data-dir','1'); oldCache.posts[fullName].data.likes = true; + + listItem.setAttribute('data-score', scoreWas + 2); + oldCache.posts[fullName].data.score = scoreWas + 2; + scoreElem.innerHTML = scoreWas + 2; + scoreElem.setAttribute('title', chrome.i18n.getMessage('score', [(upsWas + 2).toString(), (downsWas - 1).toString()])); + + listItem.setAttribute('data-ups', upsWas + 1); + oldCache.posts[fullName].data.ups = upsWas + 1; + + listItem.setAttribute('data-downs', downsWas - 1); + oldCache.posts[fullName].data.downs = downsWas - 1; + } cache.set(url, oldCache); @@ -483,6 +516,10 @@ RedditAPI.prototype.voteUpPost = function (e) { listItem = e.srcElement.parentNode.parentNode; fullName = listItem.id; voteWas = listItem.getAttribute('data-dir'); + scoreWas = listItem.getAttribute('data-score'); + upsWas = listItem.getAttribute('data-ups'); + downsWas = listItem.getAttribute('data-downs'); + scoreElem = listItem.getElementById('count_' + listItem.id); url = listItem.parentNode.getAttribute('data-url'); reqUrl = 'http://' + this.domain + '/api/vote?app=mh'; oldCache = cache.get(url); From 5f5fd9aad1505c82bc84d76b021867c99889810b Mon Sep 17 00:00:00 2001 From: Kerrick Long Date: Sun, 29 Apr 2012 01:20:12 -0500 Subject: [PATCH 04/12] vote DOWN now changes score --- js/common.js | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/js/common.js b/js/common.js index 9f2f7aa..620f5d3 100644 --- a/js/common.js +++ b/js/common.js @@ -500,7 +500,7 @@ RedditAPI.prototype.voteUpPost = function (e) { listItem.setAttribute('data-score', scoreWas + 2); oldCache.posts[fullName].data.score = scoreWas + 2; scoreElem.innerHTML = scoreWas + 2; - scoreElem.setAttribute('title', chrome.i18n.getMessage('score', [(upsWas + 2).toString(), (downsWas - 1).toString()])); + scoreElem.setAttribute('title', chrome.i18n.getMessage('score', [(upsWas + 1).toString(), (downsWas - 1).toString()])); listItem.setAttribute('data-ups', upsWas + 1); oldCache.posts[fullName].data.ups = upsWas + 1; @@ -546,18 +546,50 @@ RedditAPI.prototype.voteUpPost = function (e) { * @method */ RedditAPI.prototype.voteDownPost = function (e) { - var listItem, fullName, url, reqUrl, oldCache, voteWas, formData; + var listItem, fullName, url, reqUrl, oldCache, voteWas, scoreWas, upsWas, downsWas, scoreElem, formData; function success () { if (voteWas === '1') { + listItem.setAttribute('data-dir','-1'); oldCache.posts[fullName].data.likes = false; + + listItem.setAttribute('data-score', scoreWas - 2); + oldCache.posts[fullName].data.score = scoreWas - 2; + scoreElem.innerHTML = scoreWas - 2; + scoreElem.setAttribute('title', chrome.i18n.getMessage('score', [(upsWas - 1).toString(), (downsWas + 2).toString()])); + + listItem.setAttribute('data-ups', upsWas - 1); + oldCache.posts[fullName].data.ups = upsWas - 1; + + listItem.setAttribute('data-downs', downsWas + 1); + oldCache.posts[fullName].data.downs = downsWas + 1; + } else if (voteWas === '0') { + listItem.setAttribute('data-dir','-1'); oldCache.posts[fullName].data.likes = false; + + listItem.setAttribute('data-score', scoreWas - 1); + oldCache.posts[fullName].data.score = scoreWas - 1; + scoreElem.innerHTML = scoreWas - 1; + scoreElem.setAttribute('title', chrome.i18n.getMessage('score', [upsWas.toString(), (downsWas + 1).toString()])); + + listItem.setAttribute('data-downs', downsWas + 1); + oldCache.posts[fullName].data.downs = downsWas + 1; + } else if (voteWas === '-1') { listItem.setAttribute('data-dir','0'); oldCache.posts[fullName].data.likes = null; + + listItem.setAttribute('data-score', scoreWas + 1); + oldCache.posts[fullName].data.score = scoreWas + 1; + scoreElem.innerHTML = scoreWas + 1; + scoreElem.setAttribute('title', chrome.i18n.getMessage('score', [upsWas.toString(), (downsWas - 1).toString()])); + + listItem.setAttribute('data-downs', downsWas - 1); + oldCache.posts[fullName].data.downs = downsWas - 1; + } cache.set(url, oldCache); @@ -566,6 +598,10 @@ RedditAPI.prototype.voteDownPost = function (e) { listItem = e.srcElement.parentNode.parentNode; fullName = listItem.id; voteWas = listItem.getAttribute('data-dir'); + scoreWas = listItem.getAttribute('data-score'); + upsWas = listItem.getAttribute('data-ups'); + downsWas = listItem.getAttribute('data-downs'); + scoreElem = listItem.getElementById('count_' + listItem.id); url = listItem.parentNode.getAttribute('data-url'); reqUrl = 'http://' + this.domain + '/api/vote?app=mh'; oldCache = cache.get(url); From b1949ad8978ce123cae7416dc8db2303d7dcbccd Mon Sep 17 00:00:00 2001 From: Kerrick Long Date: Sun, 29 Apr 2012 01:26:44 -0500 Subject: [PATCH 05/12] fixed stupid issues --- js/common.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/js/common.js b/js/common.js index 620f5d3..513d8af 100644 --- a/js/common.js +++ b/js/common.js @@ -516,10 +516,10 @@ RedditAPI.prototype.voteUpPost = function (e) { listItem = e.srcElement.parentNode.parentNode; fullName = listItem.id; voteWas = listItem.getAttribute('data-dir'); - scoreWas = listItem.getAttribute('data-score'); - upsWas = listItem.getAttribute('data-ups'); - downsWas = listItem.getAttribute('data-downs'); - scoreElem = listItem.getElementById('count_' + listItem.id); + scoreWas = parseInt(listItem.getAttribute('data-score')); + upsWas = parseInt(listItem.getAttribute('data-ups')); + downsWas = parseInt(listItem.getAttribute('data-downs')); + scoreElem = document.getElementById('count_' + listItem.id); url = listItem.parentNode.getAttribute('data-url'); reqUrl = 'http://' + this.domain + '/api/vote?app=mh'; oldCache = cache.get(url); @@ -557,7 +557,7 @@ RedditAPI.prototype.voteDownPost = function (e) { listItem.setAttribute('data-score', scoreWas - 2); oldCache.posts[fullName].data.score = scoreWas - 2; scoreElem.innerHTML = scoreWas - 2; - scoreElem.setAttribute('title', chrome.i18n.getMessage('score', [(upsWas - 1).toString(), (downsWas + 2).toString()])); + scoreElem.setAttribute('title', chrome.i18n.getMessage('score', [(upsWas - 1).toString(), (downsWas + 1).toString()])); listItem.setAttribute('data-ups', upsWas - 1); oldCache.posts[fullName].data.ups = upsWas - 1; @@ -598,10 +598,10 @@ RedditAPI.prototype.voteDownPost = function (e) { listItem = e.srcElement.parentNode.parentNode; fullName = listItem.id; voteWas = listItem.getAttribute('data-dir'); - scoreWas = listItem.getAttribute('data-score'); - upsWas = listItem.getAttribute('data-ups'); - downsWas = listItem.getAttribute('data-downs'); - scoreElem = listItem.getElementById('count_' + listItem.id); + scoreWas = parseInt(listItem.getAttribute('data-score')); + upsWas = parseInt(listItem.getAttribute('data-ups')); + downsWas = parseInt(listItem.getAttribute('data-downs')); + scoreElem = document.getElementById('count_' + listItem.id); url = listItem.parentNode.getAttribute('data-url'); reqUrl = 'http://' + this.domain + '/api/vote?app=mh'; oldCache = cache.get(url); From 991b847610aff0c5c10d365b96bf9a13c6bbf715 Mon Sep 17 00:00:00 2001 From: Kerrick Long Date: Sun, 29 Apr 2012 02:58:15 -0500 Subject: [PATCH 06/12] fixed absolute references to the reddit domain --- js/common.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/js/common.js b/js/common.js index 513d8af..e7b3774 100644 --- a/js/common.js +++ b/js/common.js @@ -843,7 +843,7 @@ RedditAPI.prototype.submitComment = function (e) { formData.append('uh', cache.get('modhash')); status.innerHTML = 'submitting...'; try { - reddit.apiTransmit('POST', 'http://www.reddit.com/api/comment?app=mh', formData, success); + reddit.apiTransmit('POST', 'http://' + this.domain + '/api/comment?app=mh', formData, success); } catch (error) { status.innerHTML = error; } @@ -891,7 +891,7 @@ RedditAPI.prototype.submitLink = function (e, tabId) { formData.append('uh', cache.get('modhash')); status.innerHTML = 'submitting...'; try { - reddit.apiTransmit('POST', 'http://www.reddit.com/api/submit?app=mh', formData, success); + reddit.apiTransmit('POST', 'http://' + this.domain + '/api/submit?app=mh', formData, success); } catch (error) { status.innerHTML = error; } @@ -1083,7 +1083,7 @@ Popup.prototype.createListHTML = function (url) { commentText = value.savedCommentText === undefined ? '' : value.savedCommentText; if (!isFreshEnough) staleCounter++; freshText = isFreshEnough ? 'fresh' : 'stale'; - thumbSrc = data.thumbnail.indexOf('/') === 0 ? 'http://www.reddit.com' + data.thumbnail : data.thumbnail; + thumbSrc = data.thumbnail.indexOf('/') === 0 ? 'http://' + reddit.domain + data.thumbnail : data.thumbnail; listHTML += '
  • '; listHTML += '
    '; @@ -1091,16 +1091,16 @@ Popup.prototype.createListHTML = function (url) { listHTML += '' + data.score + ''; listHTML += ''; listHTML += '
    '; - listHTML += ''; + listHTML += ''; listHTML += '' + data.title + ''; listHTML += ''; listHTML += '
    '; - listHTML += '' + data.title + ' '; - listHTML += '(' + data.domain + ')'; + listHTML += '' + data.title + ' '; + listHTML += '(' + data.domain + ')'; listHTML += '
    '; listHTML += '' + chrome.i18n.getMessage('submitted_when', utils.prettyDate(utils.ISODateString(new Date(data.created_utc * 1000)))) + ' ' + chrome.i18n.getMessage('by') + ' '; - listHTML += '' + data.author + ' ' + chrome.i18n.getMessage('to') + ' '; - listHTML += '' + data.subreddit + ''; + listHTML += '' + data.author + ' ' + chrome.i18n.getMessage('to') + ' '; + listHTML += '' + data.subreddit + ''; listHTML += '
    '; listHTML += '
    '; switch (data.num_comments) { From c502754d059a158658abbc74ee6955170425eb51 Mon Sep 17 00:00:00 2001 From: Kerrick Long Date: Sun, 29 Apr 2012 02:59:20 -0500 Subject: [PATCH 07/12] fixes #24; fixes #27; updated error and success handling for link submission --- js/common.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/js/common.js b/js/common.js index e7b3774..e3b2928 100644 --- a/js/common.js +++ b/js/common.js @@ -861,16 +861,24 @@ RedditAPI.prototype.submitLink = function (e, tabId) { var url, title, subreddit, status, submitButton, formData; function success (response) { - if (response.jquery[22]) { - status.innerHTML = response.jquery[22][3][0]; - } else { - status.innerHTML = 'submitted! click to view your post.'; + if (typeof response.jquery[16][3][0] !== 'undefined' && response.jquery[16][3][0].substring(0,15) === '.error.NO_LINKS') { + status.innerHTML = 'that reddit only allows text posts'; + } else if (typeof response.jquery[16][3][0] !== 'undefined' && response.jquery[16][3][0].substring(0,24) === '.error.SUBREDDIT_NOEXIST') { + status.innerHTML = 'that reddit does not exist'; + } else if (typeof response.jquery[12][3][0] !== 'undefined' && response.jquery[12][3][0].substring(0,18) === '.error.ALREADY_SUB') { + status.innerHTML = 'already submitted! click to view that post.'; + } else if (typeof response.jquery[10][3][0] !== 'undefined' && response.jquery[10][3][0].substring(0,14) === '.error.BAD_URL') { + status.innerHTML = 'that URL cannot be submitted'; + } else if (typeof response.jquery[16][3][0] !== 'undefined') { + status.innerHTML = 'submitted! click to view your post.'; submitButton.innerHTML = chrome.i18n.getMessage('submit_page'); submitButton.setAttribute('disabled'); title.setAttribute('readonly'); subreddit.setAttribute('readonly'); cache.remove(url); button.setBadgeDefaults(parseInt(tabId)); + } else { + status.innerHTML = 'unknown error occured, please try again.' } } From 6cb3e84e0fa3d2dab8739ad030efa8fbb0ba7e24 Mon Sep 17 00:00:00 2001 From: Kerrick Long Date: Sun, 29 Apr 2012 03:32:45 -0500 Subject: [PATCH 08/12] moved cache clearing to its own method for future development --- html/background.html | 3 +-- js/common.js | 11 +++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/html/background.html b/html/background.html index 07b789a..a99e869 100644 --- a/html/background.html +++ b/html/background.html @@ -12,8 +12,7 @@ background = new Background(); function initBackground() { - cache.removeAll(); - button.setBadgeDefaults(); + background.clearCache(); chrome.tabs.onUpdated.addListener(background.prepareBrowserAction); background.watchMail(); reddit.getReddits(); diff --git a/js/common.js b/js/common.js index e3b2928..8d711b1 100644 --- a/js/common.js +++ b/js/common.js @@ -934,6 +934,17 @@ function Background() { return true; } +/** + * Clear cached data and reset cleared browser buttons + * @alias Background.clearCache(tabId, info, tab) + * @return {Boolean} Returns true. + * @method + */ + Background.prototype.clearCache = function () { + cache.removeAll(); + button.setBadgeDefaults(); + }; + /** * Prepare the browser action (badge, popup, etc.) for a given tab. * @alias Background.prepareBrowserAction(tabId, info, tab) From 4f787c67ca0b06aad819783f26013af0e2924d9f Mon Sep 17 00:00:00 2001 From: Kerrick Long Date: Sun, 29 Apr 2012 03:46:51 -0500 Subject: [PATCH 09/12] Background.clearCache() does not nuke the subscription cache. --- js/common.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/js/common.js b/js/common.js index 8d711b1..2a4a3a9 100644 --- a/js/common.js +++ b/js/common.js @@ -914,6 +914,7 @@ RedditAPI.prototype.submitLink = function (e, tabId) { */ RedditAPI.prototype.getReddits = function () { reddit.apiTransmit('GET', 'http://' + this.domain + '/reddits/mine.json?app=mh', null, function (response) { + cache.remove('reddits'); cache.set('reddits', response.data.children); return true; }); @@ -941,8 +942,12 @@ function Background() { * @method */ Background.prototype.clearCache = function () { + var oldReddits; + + oldReddits = cache.get('reddits'); cache.removeAll(); button.setBadgeDefaults(); + cache.set('reddits', oldReddits) }; /** From 9ada1823a35a1ef3a8136eafcc9302bf0460943a Mon Sep 17 00:00:00 2001 From: Kerrick Long Date: Sun, 29 Apr 2012 03:48:56 -0500 Subject: [PATCH 10/12] add the subreddit title as the title in the subreddit list :-) --- js/common.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/common.js b/js/common.js index 2a4a3a9..579ed29 100644 --- a/js/common.js +++ b/js/common.js @@ -1196,7 +1196,7 @@ Popup.prototype.createSubmitForm = function (tab) { submitHTML += '' + chrome.i18n.getMessage('popular_choices') + ''; submitHTML += ''; } From a52e8d0376a85e38bd5962529504f484f59d1f1b Mon Sep 17 00:00:00 2001 From: Kerrick Long Date: Sun, 29 Apr 2012 04:01:46 -0500 Subject: [PATCH 11/12] closes #25; clear the cache every settings.cacheTime minutes --- html/background.html | 1 + 1 file changed, 1 insertion(+) diff --git a/html/background.html b/html/background.html index a99e869..29fd089 100644 --- a/html/background.html +++ b/html/background.html @@ -15,6 +15,7 @@ background.clearCache(); chrome.tabs.onUpdated.addListener(background.prepareBrowserAction); background.watchMail(); + window.setInterval(background.clearCache, settings.cacheTime * 60 * 1000); reddit.getReddits(); } From 9360aecc1e327bf255522f59f543940fc511411d Mon Sep 17 00:00:00 2001 From: Kerrick Long Date: Sun, 29 Apr 2012 04:12:56 -0500 Subject: [PATCH 12/12] semver v1.4.0 --- manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manifest.json b/manifest.json index fcd69bc..7d94c4b 100644 --- a/manifest.json +++ b/manifest.json @@ -1,6 +1,6 @@ { "name": "Mostly Harmless", - "version": "1.3.0", + "version": "1.4.0", "description": "Find, vote on, save, hide, and report links on reddit; submit new links; These and more! http://kerrick.github.com/Mostly-Harmless", "browser_action": { "default_icon": "pix/alien.png",