From 2907b83c4e21892bdb30c071b5a37b3d0393225b Mon Sep 17 00:00:00 2001 From: Kerrick Long Date: Tue, 19 Jul 2011 21:03:24 -0500 Subject: [PATCH] Submitting links now works! --- js/common.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/js/common.js b/js/common.js index 0550cd5..042c439 100644 --- a/js/common.js +++ b/js/common.js @@ -733,6 +733,56 @@ RedditAPI.prototype.submitComment = function (e) { } }; +/** + * Submits a link. + * @alias RedditAPI.submitLink(event) + * @param {Object} event The event object. + * @return {Boolean} Returns true. + * @method + */ +RedditAPI.prototype.submitLink = function (e, tabId) { + var url, title, subreddit, status, submitButton, formData; + + function afterSubmission (response) { + console.log(response); + + if (response.jquery[22]) { + status.innerHTML = response.jquery[22][3][0]; + } else { + 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)); + } + } + + submitButton = e.srcElement; + status = submitButton.parentNode.getElementsByClassName('status')[0]; + title = document.getElementById('submit_title'); + subreddit = document.getElementById('submit_reddit'); + url = document.getElementById('submit_url'); + + if (title.value === '' || subreddit.value === '') { + status.innerHTML = chrome.i18n.getMessage('error_empty'); + } else { + formData = new FormData(); + formData.append('title', title.value); + formData.append('url', url.value); + formData.append('sr', subreddit.value); + formData.append('kind', 'link'); + formData.append('uh', cache.get('modhash')); + status.innerHTML = 'submitting...'; + try { + reddit.apiTransmit('POST', 'http://www.reddit.com/api/submit?app=mh', formData, afterSubmission); + } catch (error) { + status.innerHTML = error; + } + } +}; + /** * Grabs the logged in user's reddits. * @alias RedditAPI.getReddits()