booter
A tiny browser-side script loader designed to load your 3rd-party, embedded
Javascript library inside a sandboxed iframe, and give you transparent access
to it on the parent page.
Problem
You have some javascript code that someone else is going to embed in their
website. Maybe you're making a widget, or a JS library for your service.
Since [nearly] all of the global Javascript environment is modifiable, your
code is not guaranteed to run in a sane environment. In fact, it's almost
guaranteed that it will frequently be run in an environment that's been hacked
& slashed so much that it breaks your code.
Solution to Problem
Run your code inside an iframe! This gives you a clean sandboxed environment
in which to run your code. If you set it up right, you can call
functions directly, you can interact with your inside-the-iframe code so transparently,
you'll never know it's inside an iframe. That's what booter
does.
DIY: Not as Simple as it "Should Be"
Doing this can be trickier than it seems. Old & crappy browsers do strange things to
iframes, and you need to jump through some hoops to ensure sanity when communicating
between iframe & parent page.
Booter Makes it Simple
Inside your tiny load.js
file:
var loadScript = require('booter')
loadScript('//my-domain.com/js/my-lib.js', ['MYLIB'], onReady)
function onReady(my_lib) {
window.MYLIB = my_lib
my_lib.init()
}
When you (or your end-user) call any method on MYLIB, that method will
be executed in the sandboxed iframe context. There are no restrictions
on what you can pass to exposed function. For example, you can pass
parent-page DOM nodes to an exposed function with no worries. Outside a
few contrived examples, you'll never notice or care that it's in an iframe.
Usage:
Using Browserify
var loadScript = require('booter')
loadScript('//my-domain.com/js/my-lib.js')
Using a <script> tag
<script src="//cdn.my-site.com/path/to/booter.js"></script>
<script>booter.loadScript('//my-site.com/js/my-lib.js')</script>
Copy/pasting
You can copy/paste it directly into your load.js
script (not recommended, but it's small enough that you could if you wanted)
TODO:
- Add automated cross-browser tests with zuul & sauce labs
- Set doctype on iframe?