Windows系统里提供了一个名为ScriptControl的OCX组件,可以用来执行JavaScript和VBScript等脚本语言。针对PB语言缺乏的各种基础算法功能,使用JavaScript相应的库来实现是一种比较好的方式。
下图为ScriptControl组件提供的方法:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/be5b003491ce7b57b433e5ceafd66bef.png)
URL编码与解码
n_script_control sc
string ls_text
string ls_encode_result, ls_decode_result
ls_text = "PB调用JavaScript的简单应用"
sc = create n_script_control
// URL编码
ls_encode_result = sc.eval('encodeURI("'+ls_text+'")')
// URL解码
ls_decode_result = sc.eval('decodeURI("'+ls_encode_result+'")')
return 0
解析XML
测试XML:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<callprcsrvResponse xmlns="http://tempurl.org">
<callprcsrvResult>PB调用JavaScript的简单应用</callprcsrvResult>
</callprcsrvResponse>
</soap:Body>
</soap:Envelope>
JavaScript代码:
function testParseXML(xmlString){
var xmlDoc = null;
xmlDoc = new ActiveXObject("MSXML2.DOMDocument");
xmlDoc.async = false;
xmlDoc.loadXML(xmlString);
return xmlDoc.getElementsByTagName("callprcsrvResult")[0].firstChild.nodeValue;
}
crypto-js库加解密算法
MD5
JavaScript代码:
function testMD5(signStr){
return CryptoJS.MD5(signStr).toString();
}
调用testMD5前,ScriptControl需要先加载依赖项core.js和md5.js
HmacSHA256
JavaScript代码:
function testHmacSHA256(msg, key){
return CryptoJS.HmacSHA256(msg, key).toString();
}
调用testHmacSHA256前,ScriptControl需要先加载依赖项core.js、sha256.js、hmac.js、hmac-sha256.js
源代码
这里仅对ScriptControl组件做简单的封装
发现BUG请留言或私信,以便修正(QQ:768310524 TEL:18649713925)
forward
global type n_script_control from nonvisualobject
end type
end forward
global type n_script_control from nonvisualobject
end type
global n_script_control n_script_control
type variables
private:
oleobject o
end variables
forward prototypes
public subroutine setlanguage (string language)
public subroutine addcode (string script)
public subroutine reset ()
public function string getlanguage ()
public subroutine settimeout (long milliseconds)
public function long gettimeout ()
public function any eval (string script)
public function any run (string procedurename, any param1)
public function any run (string procedurename)
public function any run (string procedurename, any param1, any param2)
public function any run (string procedurename, any param1, any param2, any param3)
public function any apply (string procedurename, any param[])
end prototypes
public subroutine setlanguage (string language);o.language = language
end subroutine
public subroutine addcode (string script);o.addcode(script)
end subroutine
public subroutine reset ();o.reset()
end subroutine
public function string getlanguage ();return o.language
end function
public subroutine settimeout (long milliseconds);o.timeout = milliseconds
end subroutine
public function long gettimeout ();return o.timeout
end function
public function any eval (string script);return o.eval(script)
end function
public function any run (string procedurename, any param1);return o.run(procedurename, param1)
end function
public function any run (string procedurename);return o.run(procedurename)
end function
public function any run (string procedurename, any param1, any param2);return o.run(procedurename, param1, param2)
end function
public function any run (string procedurename, any param1, any param2, any param3);return o.run(procedurename, param1, param2, param3)
end function
public function any apply (string procedurename, any param[]);any a
choose case upperbound(param)
case 0
a = run(procedurename)
case 1
a = run(procedurename, param[1])
case 2
a = run(procedurename, param[1], param[2])
case 3
a = run(procedurename, param[1], param[2], param[3])
end choose
return a
end function
on n_script_control.create
call super::create
TriggerEvent( this, "constructor" )
end on
on n_script_control.destroy
TriggerEvent( this, "destructor" )
call super::destroy
end on
event constructor;o = create oleobject
o.connecttonewobject( "MSScriptControl.ScriptControl")
this.setlanguage( 'JavaScript')
end event
event destructor;o.disconnectobject()
end event