Here's a more generic way to load any script (or maybe just AMD-enabled script) using almond.js
(you can replace it with require.js
too if you want).
Place this script in the beginning of the scratchpad snippet. This snippet loads the almond.js
script loader.
var _ = function (run) {
var script = document.createElement("script")
// replace this url to load any other script loaders...
script.src = 'https://gitcdn.link/repo/requirejs/almond/master/almond.js';
// then run the code once require is available
script.onload = function () {
run(requirejs)
}
script.onerror = function () {
// uncaught!
throw new Error('error loading almond!')
}
document.body.appendChild(script)
}
Now, you can wrap your scratchpad code like this
_(function(require){
// here you may configure requirejs to load external scripts
// see: https://requirejs.org/docs/api.html#config
// For e.g. to load jQuery you can do something like this
require.config({
path: {
'jquery': 'https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'
}
})
// then trigger require js
require(['jquery'], function($){
// your code follows here...!
})
})