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.


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';


Here goes the real functionality :)

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

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