load_js
static async load_js(url, module_name){
// if module_name is null, load at global scope
// if module_name is specified, use that name as the module name
// otherwise, use the filename from the gist as the module name
//console.log("gisting", gist_id)
try{
const response = await fetch(`${url}?${Date.now()}`)
const code = await response.text()
try{
if(module_name===null){
await Jade.incorporate_code(code)
try{
auto_exec()
auto_exec=null// in case a later module gets loaded in the global scope, don't run an old auto_exec
}catch(e){
if(e.name!=="ReferenceError"){
console.error("Error running auto_exec()",e.name)
}
}
//Jade.incorporate_code("auto_exec=null")
}else if(!module_name){
const module_name=Jade.file_name_to_module_name(url.substring(url.lastIndexOf('/')+1))
await Jade.incorporate_code(code, module_name)
try{
jade_modules[module_name].auto_exec()
}catch(e){
console.error("Error running auto_exec()",e)
}
}else{
//console.log("module_name", module_name)
//console.log("file.content", file.content)
await Jade.incorporate_code(code, module_name)
try{
jade_modules[module_name].auto_exec()
}catch(e){
console.error("Error running auto_exec()",e)
}
}
}catch(e){
;console.error("Error loading JS",e)
}
}catch(e){
;console.error("Error fetching JS", e)
}
}
static incorporate_code(code, module_name_in){
// It turns out that the script block does not need to persist in the HTML
// once the script block is loaded, the JS is parsed and not again referenced.
// So, we create a script block, append it to the document body, then remove.
//console.log(module_name_in, code)
let new_code=code
if(module_name_in){
// a module_name is supplied, put this in Jade Modules
const module_name=module_name_in.toLowerCase()
const parsed_code = Jade.parse_code(code)
if(parsed_code.error){
;console.error("Error in",module_name_in, parsed_code)
return
}
const function_names=[]
for(const element of parsed_code.body){
if(element.type==="FunctionDeclaration"){
if(element.id && element.id.name){
//console.log("Found:",element.id.name)
function_names.push(element.id.name)
}
}else if(element.type==="VariableDeclaration" && element.declarations[0] && element.declarations[0].init&&element.declarations[0].init.type.includes("FunctionExpression")){
if(element.declarations[0].id && element.declarations[0].id.name){
// this is a named function
//console.log("Found:",element.declarations[0].id.name)
function_names.push(element.declarations[0].id.name)
}
}
}
//console.log("module_name",module_name)
//console.log("function_names",function_names)
//console.log("=====================================================")
//console.log("====================================================")
//console.log("=====================================================")
//console.log(code)
//console.log("=====================================================")
//console.log("====================================================")
//console.log("=====================================================")
new_code = `var JADE_USER_CODE_MASTER = function (){${code}
;jade_modules.add('${module_name}',${function_names})
}();`
}
const script = document.createElement("script");
script.innerHTML = new_code
document.body.appendChild(script);
document.body.lastChild.remove()
}
Labels: hide