Skip to content

Instantly share code, notes, and snippets.

@fproperzi
Last active April 26, 2024 08:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fproperzi/9a2bca3cbb7f19bd2c9714061f7b54e7 to your computer and use it in GitHub Desktop.
Save fproperzi/9a2bca3cbb7f19bd2c9714061f7b54e7 to your computer and use it in GitHub Desktop.
wsf file and config to use in wsf files
// https://stackoverflow.com/questions/34882591/upgrading-iis-classic-asp-javascript-jscript-scripting-engines-to-chakra
var _htmlfile = WScript.CreateObject('htmlfile');
_htmlfile.write('<meta http-equiv="x-ua-compatible" content="IE=11" />');
// expose more modern methods from htmlfile
var JSON = _htmlfile.parentWindow.JSON;
String.prototype.trim = _htmlfile.parentWindow.String.prototype.trim;
String.prototype.padStart = _htmlfile.parentWindow.String.prototype.padStart;
Array.prototype.indexOf = _htmlfile.parentWindow.Array.prototype.indexOf;
Array.prototype.forEach = _htmlfile.parentWindow.Array.prototype.forEach;
Array.prototype.map = _htmlfile.parentWindow.Array.prototype.map;
Array.prototype.reduce = _htmlfile.parentWindow.Array.prototype.reduce;
Array.prototype.filter = _htmlfile.parentWindow.Array.prototype.filter;
Array.prototype.includes = _htmlfile.parentWindow.Array.prototype.includes;
//Date.prototype.toJSON = _htmlfile.Date.prototype.toJSON;
Object.keys = _htmlfile.parentWindow.Object.keys;
Object.values = _htmlfile.parentWindow.Object.values;
_htmlfile.close(); // no longer needed
//-------------------------------------------------------------------------------------------------------------------------
// response.write di tutti gli argomenti: rw(1,2,3,4,'a','c')
var isObject = function(v){ return typeof v === 'object'};
rwold('Wscript:'+(typeof WScript)+(typeof Response === 'object'?' si':' no'));
function rwold() {
for(var i = 0; i < arguments.length; ++i) {
if(typeof arguments[i] == 'object')
WScript.Echo(JSON.stringify(arguments[i]));
else WScript.Echo(arguments[i]);
};
}
function rw() {
for(var i = 0; i < arguments.length; ++i) {
if(typeof arguments[i] == 'object')
if(typeof Response === 'object') Response.Write(JSON.stringify(arguments[i]));
else WScript.Echo(JSON.stringify(arguments[i]));
else
if(typeof Response === 'object') Response.Write(arguments[i]);
else WScript.Echo(arguments[i]);
}
}
// --- template _.lodash type: var f = ffTemplate(str) .... f(data)
function ffTemplate(str, data) {
var c = { evaluate: /{{([\s\S]+?)}}/g, interpolate: /{{=([\s\S]+?)}}/g, escape:/{{-([\s\S]+?)}}/g };
var tmpl = 'var __p=[],'+
'print=function(){__p.push.apply(__p,arguments);},' +
'escape=function(s){return s.replace(/[\u00A0-\u9999<>\&]/gim, function(i) {return "&#"+i.charCodeAt(0)+";";});};'+
'with(obj||{}){__p.push(\'' +
str.replace(/\\/g, '\\\\')
.replace(/'/g, "\\'")
.replace(c.escape, function(match, code) {
return "',escape(" + code.replace(/\\'/g, "'") + "),'";
})
.replace(c.interpolate, function(match, code) {
return "'," + code.replace(/\\'/g, "'") + ",'";
})
.replace(c.evaluate || null, function(match, code) {
return "');" + code.replace(/\\'/g, "'")
.replace(/[\r\n\t]/g, ' ') + "__p.push('";
})
.replace(/\r/g, '\\r')
.replace(/\n/g, '\\n')
.replace(/\t/g, '\\t')
+ "');}return __p.join('');";
//console.log(tmpl.replace(/;/g, '\n')); // <- dump compiled code to console before evaluating
var func = new Function('obj', tmpl);
return data ? func(data) : func;
};
<?XML Version="1.0" ?>
<?job error="true" debug="true"?>
<package>
<job id="VB Code">
<script language="VBScript">
<![CDATA[
Dim Ver, Dummy
Ver = "You are using " & ScriptEngine & " Version "
Ver = Ver & ScriptEngineMajorVersion & "."
Ver = Ver & ScriptEngineMinorVersion
Dummy = MsgBox(Ver, 64,"Scripting Engine")
]]>
</script>
</job>
<job id="JS Code">
<script language="JScript" src="config.wsf.js"></script>
<script language="JScript">
<![CDATA[
var Ver = "You are using " + ScriptEngine() + " Version ";
Ver += ScriptEngineMajorVersion() + ".";
Ver += ScriptEngineMinorVersion();
rw(Ver);
try {
var obj = {a:[1,2,3,4,5],b:'ciccio',html:'2<>&345'};
var myString = function(){/*
This is some
awesome multi-lined
string using a comment
inside a function
returned as a string.
Enjoy the jimmy rigged code.
my name is {{=b}} and i can count:
{{ for(var i=0;i<a.length;i++) print(i?',':'',i); }}
------------------------------
{{ a.reverse().forEach(function(v){ print(v); }); }}
------------------------------
test htmlencode
{{-html}}
*/}.toString().slice(14,-3);
//padding {{= str1.padStart(2, '0') }}
var sfTmpl_ciccio = ffTemplate(myString);
rw( sfTmpl_ciccio(obj));
//var p = str1.padStart(2, '0');
var s = ' cim ';
rw('|'+s.trim()+'|');
rw(obj);
} catch(e) {
rw('errore:'+JSON.stringify(e));
}
]]>
</script>
</job>
</package>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment