var settings, cache, utils, button, reddit, background; settings = new Store('settings', { 'cacheTime': 3, 'timeoutLength': 5, 'freshCutoff': 7, 'popupWidth': 640, 'shamelessPlug': false, 'waitForClick': false, 'checkMail': true, 'mailInterval': 1, 'mailDisplayTime': 10, 'mailSound': false, 'excludedDomains': 'secure.ingdirect.com\nchaseonline.chase.com\nonline.wellsfargo.com', 'excludedRegex': 'chrome://.*\nchrome-extension://.*\nview-source://.*\nftp://.*\nhttps?://www\.google\.com/search.*\nhttps?://search\.yahoo\.com/search.*\nhttps?://www\.bing\.com/search.*\nhttps?://www.reddit.com/(?:r/(?:\\w|\\+)+/?)?(?:$|\\?count)' }); cache = new Store('cache'); /** * Create a new framework of utility functions. * @classDescription Creates a new framework of utility functions. * @type {Object} * @return {Boolean} Returns true. * @constructor */ function MHUtils() { return true; } /** * Convert an array to an object with properties of the array each having empty values, for use in "in" statements. * @alias MHUtils.objConvert(arr) * @param {Array} arr The array to be converted * @return {Object} Returns the object * @method */ MHUtils.prototype.objConvert = function (arr) { var obj = {}; for (var i = 0; i < arr.length; i++) { obj[arr[i]]=''; } return obj; }; /** * Escape special RegExp characters. * @alias MHUtils.regexEscape(str) * @param {String} str The string to be escaped * @return {String} Returns an escaped string * @method */ MHUtils.prototype.regexEscape = function (str) { return str.replace(/([.?*+\^$\[\]\\(){}\-])/g, "\\$1"); }; /** * Find the UNIX epoch time. * @alias MHUtils.epoch() * @return {Number} Returns the current epoch time * @method */ MHUtils.prototype.epoch = function () { return Math.floor(new Date().getTime() / 1000); }; /** * Iterate over an object. * @alias MHUtils.forEachIn(object, action) * @return {Boolean} Returns true. * @method */ MHUtils.prototype.forEachIn = function (object, action) { for (var property in object) { if (object.hasOwnProperty(property)) { action(property, object[property]); } } return true; }; /** * Iterates over an array. * @alias MHUtils.forEach(array, action) * @return {Boolean} Returns true. * @method */ MHUtils.prototype.forEach = function (array, action) { for (var i = 0; i < array.length; i++) { action(array[i]); } return true; }; /** * Parses a URL and returns a useful object. http://james.padolsey.com/javascript/parsing-urls-with-the-dom/ * @alias MHUtils.parseURL(url) * @param {String} url A string with a full URL to be parsed. * @return {Object} Returns a URL object. * @method */ MHUtils.prototype.parseURL = function (url) { var a = document.createElement('a'); a.href = url; return { source: url, protocol: a.protocol.replace(':',''), host: a.hostname, port: a.port, query: a.search, params: (function (){ var ret = {}, seg = a.search.replace(/^\?/,'').split('&'), len = seg.length, i = 0, s; for (;i'; staleCounter = 0; utils.forEachIn(cache.get(url).posts, function (name, value) { var data, voteDir, hiddenText, hideStatus, hideAction, saveText, saveStatus, saveAction, isFreshEnough, freshText, thumbSrc, commentText; data = value.data; if (data.likes === true) voteDir = 1; if (data.likes === null) voteDir = 0; if (data.likes === false) voteDir = -1; hideStatus = data.hidden; hiddenText = hideStatus === true ? 'unhide' : 'hide'; hideAction = hideStatus === true ? 'reddit.unhidePost(event)' : 'reddit.hidePost(event)'; saveStatus = data.saved; saveText = saveStatus === true ? 'unsave' : 'save'; saveAction = saveStatus === true ? 'reddit.unsavePost(event)' : 'reddit.savePost(event)'; isFreshEnough = settings.get('freshCutoff') === 91 ? 'true' : data.created_utc >= utils.epoch() - settings.get('freshCutoff') * 24 * 60 * 60; 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; listHTML += '
  • '; listHTML += '
    '; listHTML += ''; listHTML += '' + data.score + ''; listHTML += ''; listHTML += '
    '; listHTML += ''; listHTML += '' + data.title + ''; listHTML += ''; listHTML += '
    '; 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 += '
    '; listHTML += ''; listHTML += '
    '; listHTML += '
    '; listHTML += '
    '; listHTML += '' + chrome.i18n.getMessage('leave_comment') + ''; listHTML += ''; listHTML += ''; listHTML += ''; listHTML += ''; listHTML += '
    '; listHTML += '
    '; listHTML += '
  • '; }); listHTML += '' if (staleCounter > 0) { listHTML += '
    ' + chrome.i18n.getMessage('stale_posts_hiding', staleCounter.toString()) + '
    '; } return listHTML; }; /** * Create the HTML for a submission form. * @alias Popup.createSubmitForm(url) * @param {String} url The URL of the page to create the HTML for. * @return {String} Returns the generated Form. * @method */ Popup.prototype.createSubmitForm = function (url) { return 'Submit form not yet programmed.'; } /** * Show stale posts. * @alias Popup.showStalePosts() * @return {Boolean} Returns true. * @method */ Popup.prototype.showStalePosts = function () { var stalePosts; stalePosts = document.getElementsByClassName('stale'); document.getElementById('information').innerHTML = chrome.i18n.getMessage('stale_posts_showing'); while (stalePosts.length > 0) { stalePosts[0].className = 'stale-shown'; } }; /** * Show the comment form for a given post. * @alias Popup.showCommentForm(postId) * @param {String} postId The ID of the thing to show the comment form for. * @return {Boolean} Returns true. * @method */ Popup.prototype.showCommentForm = function (postId) { document.getElementById(postId).getElementsByClassName('comment')[0].style.display = 'block'; return true; }; /** * Cache a comment for a given post. * @alias Popup.cacheComment(event) * @param {Object} event The event object of the keyup. * @return {Boolean} Returns true. * @method */ Popup.prototype.cacheComment = function (e) { var value, postId, url, oldCache; value = e.srcElement.value; postId = e.srcElement.parentNode.parentNode.parentNode.id url = e.srcElement.parentNode.parentNode.parentNode.parentNode.getAttribute('data-url'); oldCache = cache.get(url); oldCache.posts[postId].savedCommentText = value; cache.set(url, oldCache); return true; };