Skip to content

Instantly share code, notes, and snippets.

@joshcarr
Last active August 9, 2023 08:51
Show Gist options
  • Save joshcarr/e4e68363a997f2f432da to your computer and use it in GitHub Desktop.
Save joshcarr/e4e68363a997f2f432da to your computer and use it in GitHub Desktop.
bookmarklet boilerplate
javascript:(function(){
// avoid the bookmarklet activating more than once
if (window.MyNamespace) {
return;
}
window.MyNamespace = { };
var version = 1,
script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('charset', 'UTF-8');
script.setAttribute('src', 'http://example.com/mynamespace.js?r=' + Math.random());
document.documentElement.appendChild(script);
script.onload = script.onreadystatechange = function() {
var rs = script.readyState;
if (!rs || rs === 'loaded' || rs === 'complete') {
script.onload = script.onreadystatechange = null;
// initialise or warn if older version
if (version !== window.MyNamespace.version) {
alert('This bookmarklet is out of date!');
} else {
window.MyNamespace.init();
}
}
};
}());
window.MyNamespace = (function(window, document, undefined){
var app = {
// increment to expire active bookmarklets
version : 1
};
app.init = function () {
window.alert('yes!');
};
return app;
})(window, window.document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment