I need to parse JavaScript code into AST (JSON string) using acorn (a tiny, fast JavaScript parser), or use UglifyJS to compress my JavaScript code. I wonder if there is a way for VB6 to use third party JavaScript libs. Thanks!
Last edited by dreammanor; Dec 20th, 2019 at 10:38 AM.
Checked a lot of information and also did some tests. I find that the interaction between VB6 and modern JavaScript libs is still very difficult.
If you find appropriate versions of these libs (which still support the IE-js-engine of IE9-IE11),
you can use a very simple Class (cJSWrap) to run them directly:
Code:
Option Explicit
Private mDoc As Object
Private Sub Class_Initialize()
Set mDoc = CreateObject("htmlfile")
End Sub
Public Sub AddSriptContent(JavaScript As String)
AddElmtToHead CreateScriptElmt(JavaScript)
End Sub
Public Function Func(FuncName As String, ParamArray P()) 'for max 5 Params currently
Select Case UBound(P) + 1
Case 0: Func = CallByName(mDoc.Script, FuncName, VbMethod)
Case 1: Func = CallByName(mDoc.Script, FuncName, VbMethod, P(0))
Case 2: Func = CallByName(mDoc.Script, FuncName, VbMethod, P(0), P(1))
Case 3: Func = CallByName(mDoc.Script, FuncName, VbMethod, P(0), P(1), P(2))
Case 4: Func = CallByName(mDoc.Script, FuncName, VbMethod, P(0), P(1), P(2), P(3))
Case 5: Func = CallByName(mDoc.Script, FuncName, VbMethod, P(0), P(1), P(2), P(3), P(4))
End Select
End Function
Public Property Get Prop(PropName As String)
Prop = CallByName(mDoc.Script, PropName, VbGet)
End Property
Public Property Let Prop(PropName As String, PropValue)
CallByName mDoc.Script, PropName, VbLet, PropValue
End Property
Private Sub AddElmtToHead(Elmt)
mDoc.GetElementsByTagName("head").Item(0).appendChild Elmt
End Sub
Private Function CreateScriptElmt(ScriptText As String)
Set CreateScriptElmt = mDoc.createElement("script")
CreateScriptElmt.Type = "text/javascript"
CreateScriptElmt.Text = ScriptText
End Function
Please add the above Class-Code into an empty VB-Form-Project (along with another one, named cIEFeatures) :
Code:
Option Explicit
'this module has built-in IE-feature-control according to: http://msdn.microsoft.com/en-us/library/ee330733%28v=vs.85%29.aspx
Const FeaturePageSetup As String = "HKCU\Software\Microsoft\Internet Explorer\PageSetup\"
Const FeatureBaseKey As String = "HKCU\Software\Microsoft\Internet Explorer\Main\FeatureControl\"
Private Declare Function GetModuleFileNameA Lib "kernel32" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
Private Sh As Object, mExeName As String, mInstalledVersion As String
Private Sub Class_Initialize()
Set Sh = CreateObject("WScript.Shell")
mExeName = Space$(260)
mExeName = Left$(mExeName, GetModuleFileNameA(0, mExeName, Len(mExeName)))
mExeName = Mid$(mExeName, InStrRev(mExeName, "\") + 1)
If FEATURE_BROWSER_EMULATION = 7000 Then 'in this case it is not elevated yet - and we ensure new elevated Defaults
FEATURE_ENABLE_WEB_CONTROL_VISUALS = 1
FEATURE_RESTRICT_ACTIVEXINSTALL = 1
FEATURE_ADDON_MANAGEMENT = 1
FEATURE_AJAX_CONNECTIONEVENTS = 1
FEATURE_ENABLE_CLIPCHILDREN_OPTIMIZATION = 1
FEATURE_MANAGE_SCRIPT_CIRCULAR_REFS = 0
FEATURE_MIME_HANDLING = 1
FEATURE_DOMSTORAGE = 1
FEATURE_GPU_RENDERING = 1 ' testen
FEATURE_IVIEWOBJECTDRAW_DMLT9_WITH_GDI = 0 ' testen
FEATURE_NINPUT_LEGACYMODE = 0
FEATURE_DISABLE_LEGACY_COMPRESSION = 1
FEATURE_LOCALMACHINE_LOCKDOWN = 0
FEATURE_BLOCK_LMZ_OBJECT = 0
FEATURE_BLOCK_LMZ_SCRIPT = 0
FEATURE_DISABLE_NAVIGATION_SOUNDS = 1
FEATURE_SCRIPTURL_MITIGATION = 1
FEATURE_SPELLCHECKING = 1
FEATURE_STATUS_BAR_THROTTLING = 1
FEATURE_TABBED_BROWSING = 1
FEATURE_VALIDATE_NAVIGATE_URL = 1
FEATURE_WEBOC_DOCUMENT_ZOOM = 1
FEATURE_WEBOC_POPUPMANAGEMENT = 0
FEATURE_WEBOC_MOVESIZECHILD = 1
FEATURE_WEBSOCKET = 1
FEATURE_WINDOW_RESTRICTIONS = 0
FEATURE_XMLHTTP = 1
End If
End Sub
Public Property Get ExeName() As String
ExeName = mExeName
End Property
Public Property Get InstalledVersion() As String
On Error Resume Next
InstalledVersion = Sh.RegRead("HKLM\Software\Microsoft\Internet Explorer\svcVersion")
If Len(InstalledVersion) = 0 Then mInstalledVersion = Sh.RegRead("HKLM\Software\Microsoft\Internet Explorer\Version")
If Len(InstalledVersion) = 0 Then mInstalledVersion = 7 'let's assume that as a fallback, since it's the default-emulation for a Browser-Control
If Err Then Err.Clear
End Property
Public Sub WriteFeature(FeatureKey As String, ByVal Value, Optional ValueType As String = "REG_DWORD")
Sh.RegWrite FeatureBaseKey & FeatureKey & "\" & mExeName, Value, ValueType
End Sub
Public Function ReadFeature(FeatureKey As String, Optional ByVal Default)
On Error Resume Next
ReadFeature = Sh.RegRead(FeatureBaseKey & FeatureKey & "\" & mExeName)
If Err Then
If Not IsEmpty(Default) And FeatureKey <> "FEATURE_BROWSER_EMULATION" Then WriteFeature FeatureKey, Default
ReadFeature = Default
End If
On Error GoTo 0
End Function
Public Property Get FEATURE_BROWSER_EMULATION() As Long
FEATURE_BROWSER_EMULATION = ReadFeature("FEATURE_BROWSER_EMULATION", 7000)
End Property
Public Property Let FEATURE_BROWSER_EMULATION(ByVal Version As Long)
If Version < 7 Then Version = 7
If Version < 1000 Then Version = Version * 1000
WriteFeature "FEATURE_BROWSER_EMULATION", Version
End Property
Public Property Get FEATURE_ENABLE_WEB_CONTROL_VISUALS()
FEATURE_ENABLE_WEB_CONTROL_VISUALS = ReadFeature("FEATURE_ENABLE_WEB_CONTROL_VISUALS", 1)
End Property
Public Property Let FEATURE_ENABLE_WEB_CONTROL_VISUALS(ByVal Value)
WriteFeature "FEATURE_ENABLE_WEB_CONTROL_VISUALS", Value
End Property
Public Property Get FEATURE_RESTRICT_ACTIVEXINSTALL()
FEATURE_RESTRICT_ACTIVEXINSTALL = ReadFeature("FEATURE_RESTRICT_ACTIVEXINSTALL", 1)
End Property
Public Property Let FEATURE_RESTRICT_ACTIVEXINSTALL(ByVal Value)
WriteFeature "FEATURE_RESTRICT_ACTIVEXINSTALL", Value
End Property
Public Property Get FEATURE_ADDON_MANAGEMENT()
FEATURE_ADDON_MANAGEMENT = ReadFeature("FEATURE_ADDON_MANAGEMENT", 0)
End Property
Public Property Let FEATURE_ADDON_MANAGEMENT(ByVal Value)
WriteFeature "FEATURE_ADDON_MANAGEMENT", Value
End Property
Public Property Get FEATURE_AJAX_CONNECTIONEVENTS()
FEATURE_AJAX_CONNECTIONEVENTS = ReadFeature("FEATURE_AJAX_CONNECTIONEVENTS", 1)
End Property
Public Property Let FEATURE_AJAX_CONNECTIONEVENTS(ByVal Value)
WriteFeature "FEATURE_AJAX_CONNECTIONEVENTS", Value
End Property
Public Property Get FEATURE_ENABLE_CLIPCHILDREN_OPTIMIZATION()
FEATURE_ENABLE_CLIPCHILDREN_OPTIMIZATION = ReadFeature("FEATURE_ENABLE_CLIPCHILDREN_OPTIMIZATION", 1)
End Property
Public Property Let FEATURE_ENABLE_CLIPCHILDREN_OPTIMIZATION(ByVal Value)
WriteFeature "FEATURE_ENABLE_CLIPCHILDREN_OPTIMIZATION", Value
End Property
Public Property Get FEATURE_MANAGE_SCRIPT_CIRCULAR_REFS()
FEATURE_MANAGE_SCRIPT_CIRCULAR_REFS = ReadFeature("FEATURE_MANAGE_SCRIPT_CIRCULAR_REFS", 1)
End Property
Public Property Let FEATURE_MANAGE_SCRIPT_CIRCULAR_REFS(ByVal Value)
WriteFeature "FEATURE_MANAGE_SCRIPT_CIRCULAR_REFS", Value
End Property
Public Property Get FEATURE_MIME_HANDLING()
FEATURE_MIME_HANDLING = ReadFeature("FEATURE_MIME_HANDLING", 1)
End Property
Public Property Let FEATURE_MIME_HANDLING(ByVal Value)
WriteFeature "FEATURE_MIME_HANDLING", Value
End Property
Public Property Get FEATURE_DOMSTORAGE()
FEATURE_DOMSTORAGE = ReadFeature("FEATURE_DOMSTORAGE", 1)
End Property
Public Property Let FEATURE_DOMSTORAGE(ByVal Value)
WriteFeature "FEATURE_DOMSTORAGE", Value
End Property
Public Property Get FEATURE_GPU_RENDERING()
FEATURE_GPU_RENDERING = ReadFeature("FEATURE_GPU_RENDERING", 1)
End Property
Public Property Let FEATURE_GPU_RENDERING(ByVal Value)
WriteFeature "FEATURE_GPU_RENDERING", Value
End Property
Public Property Get FEATURE_IVIEWOBJECTDRAW_DMLT9_WITH_GDI()
FEATURE_IVIEWOBJECTDRAW_DMLT9_WITH_GDI = ReadFeature("FEATURE_IVIEWOBJECTDRAW_DMLT9_WITH_GDI", 0)
End Property
Public Property Let FEATURE_IVIEWOBJECTDRAW_DMLT9_WITH_GDI(ByVal Value)
WriteFeature "FEATURE_IVIEWOBJECTDRAW_DMLT9_WITH_GDI", Value
End Property
Public Property Get FEATURE_NINPUT_LEGACYMODE()
FEATURE_NINPUT_LEGACYMODE = ReadFeature("FEATURE_NINPUT_LEGACYMODE", 0)
End Property
Public Property Let FEATURE_NINPUT_LEGACYMODE(ByVal Value)
WriteFeature "FEATURE_NINPUT_LEGACYMODE", Value
End Property
Public Property Get FEATURE_DISABLE_LEGACY_COMPRESSION()
FEATURE_DISABLE_LEGACY_COMPRESSION = ReadFeature("FEATURE_DISABLE_LEGACY_COMPRESSION", 1)
End Property
Public Property Let FEATURE_DISABLE_LEGACY_COMPRESSION(ByVal Value)
WriteFeature "FEATURE_DISABLE_LEGACY_COMPRESSION", Value
End Property
Public Property Get FEATURE_LOCALMACHINE_LOCKDOWN()
FEATURE_LOCALMACHINE_LOCKDOWN = ReadFeature("FEATURE_LOCALMACHINE_LOCKDOWN", 0)
End Property
Public Property Let FEATURE_LOCALMACHINE_LOCKDOWN(ByVal Value)
WriteFeature "FEATURE_LOCALMACHINE_LOCKDOWN", Value
End Property
Public Property Get FEATURE_BLOCK_LMZ_OBJECT()
FEATURE_BLOCK_LMZ_OBJECT = ReadFeature("FEATURE_BLOCK_LMZ_OBJECT", 0)
End Property
Public Property Let FEATURE_BLOCK_LMZ_OBJECT(ByVal Value)
WriteFeature "FEATURE_BLOCK_LMZ_OBJECT", Value
End Property
Public Property Get FEATURE_BLOCK_LMZ_SCRIPT()
FEATURE_BLOCK_LMZ_SCRIPT = ReadFeature("FEATURE_BLOCK_LMZ_SCRIPT", 0)
End Property
Public Property Let FEATURE_BLOCK_LMZ_SCRIPT(ByVal Value)
WriteFeature "FEATURE_BLOCK_LMZ_SCRIPT", Value
End Property
Public Property Get FEATURE_DISABLE_NAVIGATION_SOUNDS()
FEATURE_DISABLE_NAVIGATION_SOUNDS = ReadFeature("FEATURE_DISABLE_NAVIGATION_SOUNDS", 1)
End Property
Public Property Let FEATURE_DISABLE_NAVIGATION_SOUNDS(ByVal Value)
WriteFeature "FEATURE_DISABLE_NAVIGATION_SOUNDS", Value
End Property
Public Property Get FEATURE_SCRIPTURL_MITIGATION()
FEATURE_SCRIPTURL_MITIGATION = ReadFeature("FEATURE_SCRIPTURL_MITIGATION", 1)
End Property
Public Property Let FEATURE_SCRIPTURL_MITIGATION(ByVal Value)
WriteFeature "FEATURE_SCRIPTURL_MITIGATION", Value
End Property
Public Property Get FEATURE_SPELLCHECKING()
FEATURE_SPELLCHECKING = ReadFeature("FEATURE_SPELLCHECKING", 1)
End Property
Public Property Let FEATURE_SPELLCHECKING(ByVal Value)
WriteFeature "FEATURE_SPELLCHECKING", Value
End Property
Public Property Get FEATURE_STATUS_BAR_THROTTLING()
FEATURE_STATUS_BAR_THROTTLING = ReadFeature("FEATURE_STATUS_BAR_THROTTLING", 1)
End Property
Public Property Let FEATURE_STATUS_BAR_THROTTLING(ByVal Value)
WriteFeature "FEATURE_STATUS_BAR_THROTTLING", Value
End Property
Public Property Get FEATURE_TABBED_BROWSING()
FEATURE_TABBED_BROWSING = ReadFeature("FEATURE_TABBED_BROWSING", 1)
End Property
Public Property Let FEATURE_TABBED_BROWSING(ByVal Value)
WriteFeature "FEATURE_TABBED_BROWSING", Value
End Property
Public Property Get FEATURE_VALIDATE_NAVIGATE_URL()
FEATURE_VALIDATE_NAVIGATE_URL = ReadFeature("FEATURE_VALIDATE_NAVIGATE_URL", 1)
End Property
Public Property Let FEATURE_VALIDATE_NAVIGATE_URL(ByVal Value)
WriteFeature "FEATURE_VALIDATE_NAVIGATE_URL", Value
End Property
Public Property Get FEATURE_WEBOC_DOCUMENT_ZOOM()
FEATURE_WEBOC_DOCUMENT_ZOOM = ReadFeature("FEATURE_WEBOC_DOCUMENT_ZOOM", 1)
End Property
Public Property Let FEATURE_WEBOC_DOCUMENT_ZOOM(ByVal Value)
WriteFeature "FEATURE_WEBOC_DOCUMENT_ZOOM", Value
End Property
Public Property Get FEATURE_WEBOC_POPUPMANAGEMENT()
FEATURE_WEBOC_POPUPMANAGEMENT = ReadFeature("FEATURE_WEBOC_POPUPMANAGEMENT", 1)
End Property
Public Property Let FEATURE_WEBOC_POPUPMANAGEMENT(ByVal Value)
WriteFeature "FEATURE_WEBOC_POPUPMANAGEMENT", Value
End Property
Public Property Get FEATURE_WEBOC_MOVESIZECHILD()
FEATURE_WEBOC_MOVESIZECHILD = ReadFeature("FEATURE_WEBOC_MOVESIZECHILD", 0)
End Property
Public Property Let FEATURE_WEBOC_MOVESIZECHILD(ByVal Value)
WriteFeature "FEATURE_WEBOC_MOVESIZECHILD", Value
End Property
Public Property Get FEATURE_WEBSOCKET()
FEATURE_WEBSOCKET = ReadFeature("FEATURE_WEBSOCKET", 1)
End Property
Public Property Let FEATURE_WEBSOCKET(ByVal Value)
WriteFeature "FEATURE_WEBSOCKET", Value
End Property
Public Property Get FEATURE_WINDOW_RESTRICTIONS()
FEATURE_WINDOW_RESTRICTIONS = ReadFeature("FEATURE_WINDOW_RESTRICTIONS", 1)
End Property
Public Property Let FEATURE_WINDOW_RESTRICTIONS(ByVal Value)
WriteFeature "FEATURE_WINDOW_RESTRICTIONS", Value
End Property
Public Property Get FEATURE_XMLHTTP()
FEATURE_XMLHTTP = ReadFeature("FEATURE_XMLHTTP", 1)
End Property
Public Property Let FEATURE_XMLHTTP(ByVal Value)
WriteFeature "FEATURE_XMLHTTP", Value
End Property
Public Sub WritePageSetup(PageSetupKey As String, ByVal Value, Optional ValueType As String = "REG_SZ")
' sets e.g. "header" to a new string (attention: these are general settings for all IE-instances (their Print-Jobs)
' possible values for PageSetupKey are:
' "header", "footer", "margin_top","margin_left","margin_right""margin_bottom", "Shrink_To_Fit","Print_Background"
Sh.RegWrite FeaturePageSetup & PageSetupKey, Value, ValueType
End Sub
Public Function ReadPageSetup(PageSetupKey As String, Optional ByVal Default)
On Error Resume Next
ReadPageSetup = Sh.RegRead(FeaturePageSetup & PageSetupKey)
If Err Then
ReadPageSetup = Default
If Not IsEmpty(Default) Then WritePageSetup PageSetupKey, Default
End If
On Error GoTo 0
End Function
'----------------------- End of IE-Features-Interface --------------------------
Now you can use the UglifyJS-lib without any further dependencies, as long as you run this on a system which has an IE-engine > 8.
Here the Form-Code (as said, no dependencies in the Project - just the two Classes cJSWrap and cIEFeatures need to be in place).
Code:
Option Explicit
Private IEF As New cIEFeatures, JSW As New cJSWrap
Private Sub Form_Initialize()
IEF.FEATURE_BROWSER_EMULATION = Val(IEF.InstalledVersion) 'init the extended features of the IE-Engine for this Process
'add the larger js-content of the UglifyJS-lib as Text (indirectly retrieved via a CDN-download)
JSW.AddSriptContent DownloadAsText("https://cdn.jsdelivr.net/npm/[email protected]/build.min.js")
'and a smaller test-function, to check-out the library which was just added above
JSW.AddSriptContent "function test(code){var res = UglifyJS.minify(code); return res.code? res.code: res.error.message}"
End Sub
Private Sub Form_Click() 'this will minify the passed js-Text (here only a simple add-function)
Print JSW.Func("test", "function add(first, second) { return first + second; }") 'and print it to the Form
End Sub
Private Function DownloadAsText(URL As String) As String
With CreateObject("WinHttp.WinHttpRequest.5.1")
.Open "GET", URL, 0: .Send
DownloadAsText = .ResponseText
End With
End Function
Fanstastic! Olaf, you helped me too much, extremely grateful. Merry Christmas!
Glad it works... and out of curiosity...
In the TestWebApp.zip in the other thread you've included a (browser-ified and minified) "uglifyjs-test.js"-File
(which BTW works equally well with the shown approach)...
In my Test-Code above I've downloaded this browser-ified and minified UglifyJS-lib from "cdn.jsdelivr.net" (in version 3.0.0) directly.
Not sure, whether your file (which is a bit larger) is a newer, or an older version.
Where did you find your (minfied) variant of the UglifyJS-lib - and which version is it?
Re: [RESOLVED] Interaction between VB6 and JavaScript
Hi Olaf, the generation process of uglifyjs-test.js is this:
According to the tips on the official website http://lisperator.net/uglifyjs/:
(1) npm install uglify-js
(2) git clone git://github.com/mishoo/UglifyJS2.git
- cd UglifyJS2
- npm link
(3) uglifyjs --self -c -m -o /tmp/uglifyjs.js
Because the filename of uglifyjs.js conflicted with the bin (Cmd Console) name of uglifyjs, I changed the
generated uglifyjs.js to uglifyjs-test.js.
On my computer, uglifyjs-test.js goes wrong in most cases, and your "https://cdn.jsdelivr.net/npm/uglifyj...0/build.min.js" can run normally (but there is a little problem). After a while I'll post the test program.
Last edited by dreammanor; Dec 23rd, 2019 at 02:31 PM.
(1) "[email protected]/build.min.js" does not recognize the "let" keyword, for example, if compressing code "let foo = 123;" an error will occur.
(2) When the local file ""uglifyjs-browser3.0.0-build.min.js" is directly referenced in the index2.html file, the Chrome browser prompts the following error message:
Uncaught ReferenceError: module is not defined
at uglifyjs-browser3.0.0-buld.min.js:5
So I deleted "module.exports = global.UglifyJS" and "delete global.UglifyJS" in the file and saved the file as "uglifyjs-browser3.0.0-build.min-adjusted.js". Then reference the local file "uglifyjs-browser3.0.0-build.min-adjusted.js" in index2.html. At this time, the Chrome browser prompts that the code is normal.
Last edited by dreammanor; Dec 23rd, 2019 at 02:28 PM.