Loading GreaseMonkey userscripts from a server

Codeshelver relies heavily on the GreaseMonkey/GreaseKit userscript to customize the GitHub dashboard and repository pages. As I have got some more ideas for extending these functionalities (i.e. adding a list of recently shelved repositories to the dashboard) I was searching for a way to load the userscript from my server so that the users do not have to update the userscript manually everytime I add new features.

It turns out to be fairly simple, because you just have to split your userscript into two parts: The first one is the userscript that the user adds to his browser and which just loads the second part, namely a javascript file that gets integrated into the webpage and which contains the real functionality.

myapp.user.js

This is the userscript that you offer to download/add to the browser: It just integrates the real userscript from the server, so that the users do not have to update their userscripts and it also disables the userscript functionality in case your server is not available. The latter can be seen as a bug or a feature (depends on your use case), but for codeshelver.com this is a feature, because if the server is down you are not able to shelve repositories anyway ;)

(function() {
  //
  var script = document.createElement('script');
  script.src = 'http://myapp.com/userscript.js';
  script.type = 'text/javascript';
  document.getElementsByTagName('head')[0].appendChild(script);
})();

userscript.js

Here goes the real functionality :)

var Userscript = {
  init: function() {
    alert("Hello :)");
  }
};
Userscript.init();

As I said this is pretty straight forward, so go ahead and enhance the web!

iPhone app for GitHub

iOctocat

is GitHub in your pocket: The go to app for staying up to date with your projects on your iPhone, and iPod Touch.
It is