josh-/Hubski-Enhancement-Suite — pull request #5 by markbahnman (2013-02-12 00:38:28+00:00)
Title: Major refactor with jQuery and the module pattern

I was inspired by [this post](https://shanetomlinson.com/) to do a major refactor of the userscript with a few goals in mind.

1. Make it more readable using jQuery
2. Make it easier to maintain
3. Make it testable

I believe I've accomplished the first two and with that made good progress towards the third.

### Why jQuery
Basically because you can turn this
```javascript
window.location = document.getElementsByClassName('gridfeed')[0].childNodes[feedSelectionIndex].childNodes[1].childNodes[2].childNodes[1].childNodes[1].href;
```
Into this
```javascript
feed.currentNode.find('.savesplit > a:contains("hide")').click();
```
Stuff like this just blew my mind.
### Chrome Compatibility
Unfortunately chrome doesn't allow us to access the jQuery already loaded by Hubski and doesn't support metatags. To get around this we basically inject the entire script onto the page so that the jQuery is within the script's scope.

### Globals
I pulled out variables that I think will either be used by multiple modules or are likely to change so that if something changes (such as they other day) it's easy to fix.

### Modules
Each piece of major functionality is wrapped into its own module. The modules have this general form
```javascript
modules['moduleKey'] = (function() {
var Module = {
init: function() { /*initialize stuff*/},
isLoaded: function() {/*determine if module should be run*/}
};
privateMember;
privateFunction() {}
return Module;
}());
```
Every module should have at least two public functions: **init()** to initialize the module (event handlers, etc) and **isLoaded()** to determine if the module should be initialized on the current page. Then loading each module of functionality is as simple as
```javascript
for(mod in modules) {
if(modules[mod].isLoaded()) {
modules[mod].init();
}
}
```

At some point I hope to put together a testing suite at which point I'll probably add destructors to each module to allow for cleanup after a unit test.

The current modules I have set up are:

##### Shortcuts Module
One of the major changes I made to the shortcut functions is instead of massive if/else blocks I mapped each keycode to a function in objects. We separate shortcut keycodes into different object for each page they work on and then build our master keyMap object at runtime. This simplifies the function call to just
```javascript
keyMap[code]();
```
##### Collapsing Comments Module
I rewrote this one to make it more readable but it has the same function. It inserts a span tag into the comhead of each comment so that whatever symbol we use for the collapsing button will inherit hubski's styles.
##### Infinite Scroll Module
Simple scroll event listener which clicks the more posts link when you get to the bottom of the page