Last active
August 9, 2023 08:51
-
-
Save joshcarr/e4e68363a997f2f432da to your computer and use it in GitHub Desktop.
bookmarklet boilerplate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
}; | |
}()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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