Why wait for someone to provide the support ? With small changes in the common javascript code and a javascript function which will import that script file will do the magic. Ofcourse, the usage is very limited but surely can be extended as your own wish and imagination. This script will currently work on Microsoft Windows' Scripting Environment.
The javascript function which imports another JavaScript file:
/////////////////////////////////////////////////////////////////////////////////////////
// SUPPORTED SYNTAX
//
// //FUNCTION-START
// function fn_name([args[,args]]){
// }
// //FUNCTION-END
//
// //GLOBAL-START
// var g_name;
// fn_name();
// //GLOBAL-END
//
// NOTE: Multiline comment (/**/) is not stripped.
// Singleline comment (//) and empty lines are stripped.
//
// USAGE: var global_stmt=importjs("path2js");
// if (global_stmt!="") eval(global_stmt);
//
////////////////////////////////////////////////////////////////////////////////////////
var ____err_count=0;
function importjs(filename){
var line_num=0
var file=(WScript.CreateObject("Scripting.FileSystemObject")).OpenTextFile(filename, 1, false, 0);
var fn_re=/^\s*function\s+(\S+)\s*\(([^\)]*)\)(.*)/g;
var script="",line="",prototype="",global="";
var fn, gen_gl=false, gen_fn=true;
while(true){
try{line=file.Readline();line_num++;}catch(fileread){break;}
if(null!=line.match(/FUNCTION.START/g)){gen_fn=true;continue;}
if(null!=line.match(/FUNCTION.END/g)){gen_fn=false;continue;}
if(null!=line.match(/GLOBAL.START/g)){gen_gl=true;continue;}
if(null!=line.match(/GLOBAL.END/g)){gen_gl=false;continue;}
if((line=line.replace(/(^|[^:])\/\/.*$/g,"$1"))=="")continue;//comment but not url
if(line.replace(/^\s+/g,"")=="")continue;//empty line
if(gen_gl){
try{
global+=("\n"+line);
}catch(e){
____err_count++;
WScript.Echo("error:"+e.message+" in "+filename+":"+line_num);
WScript.Echo(line);
}
}else if(gen_fn){
fn=fn_re.exec(line);
if (fn!=null && fn.length==4){
var arg="";
if(fn[2]!=null && fn[2]!=""){
var args=(fn[2]).split(", ");
for(i in args){
if(i>0) arg+=",";
arg+=("\""+args[i].replace(/\s+/g, "")+"\"");
}
if (arg!="") arg+=",";
}
if (prototype!=""){
try{
eval(prototype+script+"+\"\");");
}catch(e){
____err_count++;
WScript.Echo("error:"+e.message+" in "+filename+":"+line_num);
WScript.Echo(prototype+script+"+\"\");");
}
}
prototype=fn[1]+"=new Function("+arg+"\"";
script=fn[3].replace(/("|\\|')/g, "\\$1")+"\"";//"
}else{
script+=("\n+\""+line.replace(/("|\\|')/g, "\\$1")+"\"");//"
}
}
}
if (prototype!=""){
try{
eval(prototype+script+"+\"\");");
}catch(e){
____err_count++;
WScript.Echo("error:"+e.message+" in "+filename+":"+line_num);
WScript.Echo(prototype+script+"+\"\");");
}
}
//WScript.Echo(""+____err_count+" error(s) found.");
return global;
}
The function call usage:
var g=importjs("importable.js");
if(g!="") eval(g);
After the file has been imported, the function or global variables from the other file can be accessed from this importing script file as usual.
A sample "importable.js" content:
//GLOBAL-START
var g_global=true;
//GLOBAL-END
function deinit( one , two){
WScript.Echo("deinit "+one+" "+two);
}
function init(){
WScript.Echo("init");
}
function alert(message){
WScript.Echo("alert "+message);
}
//GLOBAL-START
WScript.Echo("global - single line statement");
if (true) {
var local="localvalue";
WScript.Echo("global - multi line (if) statement");
alert("global - calling another function (alert) in global scope");
alert("global - accessing a global variable - g_global="+g_global);
alert("global - accessing a local variable - local="+local);
}
//GLOBAL-END
WScript.Echo("global - single line statement, but will not be executed");
Sure, you could find bugs in this code. Please post them :)