diff --git a/html/background.html b/html/background.html index 07b789a..29fd089 100644 --- a/html/background.html +++ b/html/background.html @@ -12,10 +12,10 @@ background = new Background(); function initBackground() { - cache.removeAll(); - button.setBadgeDefaults(); + background.clearCache(); chrome.tabs.onUpdated.addListener(background.prepareBrowserAction); background.watchMail(); + window.setInterval(background.clearCache, settings.cacheTime * 60 * 1000); reddit.getReddits(); } diff --git a/js/common.js b/js/common.js index a39e7ed..579ed29 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 + 1).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 = 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); @@ -509,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 + 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; + } 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); @@ -529,6 +598,10 @@ RedditAPI.prototype.voteDownPost = function (e) { listItem = e.srcElement.parentNode.parentNode; fullName = listItem.id; voteWas = listItem.getAttribute('data-dir'); + 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); @@ -737,9 +810,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'); @@ -768,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; } @@ -786,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.' } } @@ -816,7 +899,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; } @@ -831,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; }); @@ -851,6 +935,21 @@ 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 () { + var oldReddits; + + oldReddits = cache.get('reddits'); + cache.removeAll(); + button.setBadgeDefaults(); + cache.set('reddits', oldReddits) + }; + /** * Prepare the browser action (badge, popup, etc.) for a given tab. * @alias Background.prepareBrowserAction(tabId, info, tab) @@ -1008,24 +1107,24 @@ 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 += '
  • '; listHTML += '
    '; listHTML += ''; 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) { @@ -1097,7 +1196,7 @@ Popup.prototype.createSubmitForm = function (tab) { submitHTML += '' + chrome.i18n.getMessage('popular_choices') + ''; submitHTML += ''; } 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",