diff --git a/background.js b/background.js index 973c32d..9d00402 100644 --- a/background.js +++ b/background.js @@ -8,10 +8,11 @@ }); chrome.runtime.onInstalled.addListener(function(details) { chrome.alarms.create("updater", { "periodInMinutes": 1 }); + chrome.tabs.create({url: "options.html"}, function (tab) {}); }); // Update number on navigate to a new thread chrome.webNavigation.onCompleted.addListener(function (details) { ITCheck.getUnreadThreadCount(null); },{url: [{hostSuffix: 'ivorytower.com', pathPrefix: '/IvoryTower/ForumThread.aspx'}]}); -})(window); \ No newline at end of file +})(window); \ No newline at end of file diff --git a/cohort-year-adder.js b/cohort-year-adder.js index 3bb946f..73dd9b0 100644 --- a/cohort-year-adder.js +++ b/cohort-year-adder.js @@ -6,16 +6,29 @@ // @include http://ivorytower.dyndns.org/* // @include http://ivorytower.go.dyndns.org/* // ==/UserScript== +(function(){ + var years = { + // Sanitized for GitHub. Added back on building. + }; -var years = { - // Sanitized for GitHub. Added back on building. -}; - -var elements = document.evaluate("//a[starts-with(@href,'ProfileShow.aspx')]", - document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); -for(var i = 0; i < elements.snapshotLength; i++) { - var cur = elements.snapshotItem(i); - var name = cur.getAttribute('href').split('=')[1]; - var year = document.createTextNode(" (" + years[name] + ")"); - cur.appendChild(year); -} \ No newline at end of file + function runadder(showCohorts){ + if(showCohorts){ + var elements = document.evaluate("//a[starts-with(@href,'ProfileShow.aspx')]", + document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); + for(var i = 0; i < elements.snapshotLength; i++) { + var cur = elements.snapshotItem(i); + var name = cur.getAttribute('href').split('=')[1]; + var year = document.createTextNode(" (" + years[name] + ")"); + cur.appendChild(year); + } + } + } + + function storageGet(key, callback){ + var k = key; + chrome.storage.sync.get(k, function(storageObj){ + callback(storageObj[k]); + }); + } + storageGet('ITCheck.showCohorts', runadder); +})(); \ No newline at end of file diff --git a/core.js b/core.js index adcc38a..c4af4e9 100644 --- a/core.js +++ b/core.js @@ -57,9 +57,30 @@ chrome.browserAction.setTitle({ title: newTitle}); } + function storageGet(key, callback){ + var k = key; + chrome.storage.sync.get(k, function(storageObj){ + callback(storageObj[k]); + }) + } + + function storageSet(key, val){ + var pair = {}; + pair[key] = val; + chrome.storage.sync.set(pair); + } + + var shortcutKeys = ["nextPost","previousPost","unreadThread","rateUp","rateDown","showHidden"]; + storageSet("ITCheck.shortcutKeys", shortcutKeys); + + // core exports window.ITCheck.getUnreadThreadCount = getUnreadThreadCount; window.ITCheck.shouldSkipRequest = shouldSkipRequest; window.ITCheck.updateBadge = updateBadge; window.ITCheck.setTitle = setTitle; window.ITCheck.baseUrl = baseUrl; + window.ITCheck.storageGet = storageGet; + window.ITCheck.storageSet = storageSet; + //window.ITCheck.shortcutKeys = shortcutKeys; + })(window); \ No newline at end of file diff --git a/manifest.json b/manifest.json index eec3d1a..ec5677d 100644 --- a/manifest.json +++ b/manifest.json @@ -1,9 +1,9 @@ { "name": "IvoryTower Checker", "short_name": "IT Check", - "author": "Cody Jung", - "version": "1.3.0", - "description": "Checks IvoryTower for unread threads", + "author": "Cody Jung, Eric Clymer", + "version": "1.4.0", + "description": "Checks IvoryTower for unread threads, adds misc other functionality.", "icons": { "48": "icon48.png", "128": "icon128.png" @@ -16,8 +16,10 @@ "permissions": [ "alarms", "webNavigation", - "http://ivorytower.com/" + "http://ivorytower.com/", + "storage" ], + "options_page": "options.html", "background": { "scripts": ["jquery-2.1.3.min.js", "core.js", "background.js"], "persistent": false @@ -25,7 +27,7 @@ "content_scripts": [ { "matches": ["http://*.ivorytower.com/*"], - "js": ["jquery-2.1.3.min.js", "core.js", "cohort-year-adder.js", "shortcuts.js"] + "js": ["jquery-2.1.3.min.js", "cohort-year-adder.js", "shortcuts.js"] } ], "manifest_version": 2 diff --git a/options.html b/options.html new file mode 100644 index 0000000..0feee51 --- /dev/null +++ b/options.html @@ -0,0 +1,53 @@ + + + + + + +

IvoryTower Checker

+
+

Options

+
+
+
+ + +
+
+ + +
+
+
+

Customize Shortcut Keys

+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+
+ + + + + \ No newline at end of file diff --git a/options.js b/options.js new file mode 100644 index 0000000..487f7a3 --- /dev/null +++ b/options.js @@ -0,0 +1,53 @@ +(function(window, $){ + window.ITCheck = window.ITCheck || {}; + + ITCheck.storageGet("ITCheck.shortcutKeys", function(shortcutKeys){ + ITCheck.storageGet('ITCheck.showCohorts', function(val){ + if(val){ + options.showCohorts.checked = val; + }else{ + ITCheck.storageSet('ITCheck.showCohorts', true); + val = true; + } + options.showCohorts.checked = val; + }); + ITCheck.storageGet('ITCheck.shortcuts', function(val){ + if(val){ + options.shortcuts.checked = val; + }else{ + ITCheck.storageSet('ITCheck.shortcuts', true); + val = true; + } + options.shortcuts.checked = val; + }); + for(var i = 0; i < shortcutKeys.length; i++){ + var shortcutKey = shortcutKeys[i]; + populateShortcutKey(shortcutKey); + } + for(var i = 0; i < shortcutKeys.length; i++){ + var shortcutKey = shortcutKeys[i]; + options[shortcutKey].onchange = function(e, shortcutKey){ + var target = e.target; + ITCheck.storageSet('ITCheck.shortcutKey.'+target.name, target.value); + } + } + }); + + function populateShortcutKey(shortcutKey){ + ITCheck.storageGet('ITCheck.shortcutKey.'+shortcutKey, function(val){ + if(val){ + options[shortcutKey].value = val; + } + }); + } + + var options = $('#options')[0]; + options.showCohorts.onchange = function() { + ITCheck.storageSet('ITCheck.showCohorts', options.showCohorts.checked); + }; + + options.shortcuts.onchange = function() { + ITCheck.storageSet('ITCheck.shortcuts', options.shortcuts.checked); + $('#shortcutSection').toggle(options.shortcuts.checked); + }; +})(window, jQuery); \ No newline at end of file diff --git a/popup.js b/popup.js index 35cb89a..4494661 100644 --- a/popup.js +++ b/popup.js @@ -43,7 +43,7 @@ } else { // Or set it to blank ITCheck.updateBadge(""); - updatePopup("No unread threads.
Go to IvoryTower Today"); + updatePopup("No unread threads.
Go to IvoryTower Today
"); $("#ITToday").click(function() { openIvoryTowerToday(); diff --git a/shortcuts.js b/shortcuts.js index 9400583..e2ddf22 100644 --- a/shortcuts.js +++ b/shortcuts.js @@ -1,78 +1,111 @@ (function(win, $){ - function doNextUnreadThread(){ - var masterLink = $('#Master_ctl08 a[href*="ForumThread.aspx"]'); - if(masterLink.length > 0){ - console.log('moving to next unread thread'); - masterLink[0].click(); - }else{ - console.log('no unread threads'); - } - }; - - function jumpToNextPost(){ - if( $("#Post" + (currentPostNumber + 1)).length > 0 ){ - $("#Post" + currentPostNumber).css({"border-left-width": 1, "padding-left": 11}); - currentPostNumber++; - location.hash = 'Post'+currentPostNumber; - $("#Post" + currentPostNumber).css({"border-left-width": 3, "padding-left": 10}); - } - }; - - function jumpToPreviousPost(){ - if( $("#Post" + (currentPostNumber - 1)).length > 0 ){ - $("#Post" + currentPostNumber).css({"border-left-width": 1, "padding-left": 11}); - currentPostNumber--; - location.hash = 'Post'+currentPostNumber; - $("#Post" + currentPostNumber).css({"border-left-width": 3, "padding-left": 10}); - } - } - function ratePost(up){ - var postIdSelector = '#Post' + currentPostNumber; - var rateLinks = $(postIdSelector + ' span.Button a'); - if(up){ - rateLinks[0].click(); - }else{ - rateLinks[1].click(); + function runShortcuts(val){ + if(!val){ + return; } - } - - function showHiddenPosts(){ - $('.Clipped a')[0].click(); - } - - window.onkeypress = function(e){ - var tagKeyedIn = e.target.tagName.toLowerCase(); - if(tagKeyedIn !== 'input' && tagKeyedIn !== 'textarea'){ - if(e.which === 106){ - // j for next post - jumpToNextPost(); - }else if(e.which === 107){ - // k for previous post - jumpToPreviousPost(); - }else if(e.which === 117){ - // u for next unread thread - doNextUnreadThread(); - }else if(e.which === 97){ - // a for rate up - ratePost(true); - }else if(e.which === 102){ - // f for rate down - ratePost(false); - }else if(e.which === 104){ - // h for hidden - showHiddenPosts(); + + function doNextUnreadThread(){ + var masterLink = $('#Master_ctl08 a[href*="ForumThread.aspx"]'); + if(masterLink.length > 0){ + console.log('moving to next unread thread'); + masterLink[0].click(); + }else{ + console.log('no unread threads'); + } + }; + + function jumpToNextPost(){ + var nextEl = $("#Post" + (parseInt(currentPostNumber) + 1)); + if(nextEl.length){ + var currentEl = $("#Post" + currentPostNumber); + currentEl.css({"border-left-width": 1, "padding-left": 11}); + currentPostNumber++; + location.hash = 'Post'+currentPostNumber; + nextEl.css({"border-left-width": 3, "padding-left": 10}); + } + }; + + function jumpToPreviousPost(){ + var previousEl = $("#Post" + (parseInt(currentPostNumber) - 1)); + if(previousEl.length){ + var currentEl = $("#Post" + currentPostNumber); + currentEl.css({"border-left-width": 1, "padding-left": 11}); + currentPostNumber--; + location.hash = 'Post'+currentPostNumber; + previousEl.css({"border-left-width": 3, "padding-left": 10}); } } - }; - - //determine current post number - var firstNewPostId = $('span#New').parent().id; - if(!firstNewPostId || firstNewPostId.indexOf('Post') !== 0){ - firstNewPostId = $('.ForumThread td').last()[0].id + + function ratePost(up){ + var postIdSelector = '#Post' + currentPostNumber; + var rateLinks = $(postIdSelector + ' span.Button a'); + if(up){ + rateLinks[0].click(); + }else{ + rateLinks[1].click(); + } + } + + function showHiddenPosts(){ + $('.Clipped a')[0].click(); + } + + storageGet("ITCheck.shortcutKeys", function(shortcutKeys){ + var shortcutCodes = {}; + + function initializeShortcutKeyCode(shortcutKey){ + storageGet('ITCheck.shortcutKey.'+shortcutKey, function(val){ + shortcutCodes[shortcutKey] = val.charCodeAt(0); + }); + } + + for(var i = 0; i < shortcutKeys.length; i++){ + initializeShortcutKeyCode(shortcutKeys[i]); + } + + win.onkeypress = function(e){ + var tagKeyedIn = e.target.tagName.toLowerCase(); + if(tagKeyedIn !== 'input' && tagKeyedIn !== 'textarea'){ + if(e.which === (shortcutCodes["nextPost"] || 106)){ + jumpToNextPost(); + }else if(e.which === (shortcutCodes["previousPost"] || 107)){ + jumpToPreviousPost(); + }else if(e.which === (shortcutCodes["unreadThread"] || 110)){ + doNextUnreadThread(); + }else if(e.which === (shortcutCodes["rateUp"] || 117)){ + ratePost(true); + }else if(e.which === (shortcutCodes["rateDown"] || 100)){ + ratePost(false); + }else if(e.which === (shortcutCodes["showHidden"] || 104)){ + showHiddenPosts(); + } + } + }; + }); + //determine current post number + var firstNewPostId = $('span#New').parent().id; + if(!firstNewPostId || firstNewPostId.indexOf('Post') !== 0){ + var threadTds = $('.ForumThread td'); + if(threadTds.length){ + firstNewPostId = threadTds.last()[0].id + } + } + if(location.hash && location.hash.indexOf('Post') === 1){ + firstNewPostId = location.hash.substring(1); + } + if(firstNewPostId){ + var currentPostNumber = firstNewPostId.substring(4); + location.hash = 'Post'+currentPostNumber; + $("#Post" + currentPostNumber).css({"border-left-width": 3, "padding-left": 10}); + } } - if(location.hash && location.hash.indexOf('Post') === 1){ - firstNewPostId = location.hash.substring(1); + + function storageGet(key, callback){ + var k = key; + chrome.storage.sync.get(k, function(storageObj){ + callback(storageObj[k]); + }); } - var currentPostNumber = firstNewPostId.substring(4); + storageGet('ITCheck.shortcuts', runShortcuts); }(window, jQuery)) \ No newline at end of file diff --git a/style.css b/style.css index 8f37459..13b09d4 100644 --- a/style.css +++ b/style.css @@ -2,12 +2,17 @@ body{ color:#333; font-family:Arial, Helevetica, sans-serif; font-size:10pt; + text-align:center; +} + +div{ + margin-top:10px; + margin-bottom:10px; } #result{ width:350px; margin:0px auto; - text-align:center; } #empty{ @@ -18,4 +23,42 @@ body{ font-style:italic; cursor:pointer; color:#CCC; +} + +#shortcutSection{ + text-align:left; + width:300px; + margin:auto; + height:300px; +} + +.shortcutKey{ + height:20px; +} + +.shortcutKey label{ + float: left; +} + +.shortcutKey input{ + float: right; +} + +#topLevelSettings{ + text-align:left; + width:300px; + margin:auto; + height:70px; +} + +.topLevelSetting{ + height:20px; +} + +.topLevelSetting label{ + float: left; +} + +.topLevelSetting input{ + float: right; } \ No newline at end of file