Letraindusoir Posted January 14, 2021 Share Posted January 14, 2021 (edited) Quote function typeObj(obj){ var type=Object.prototype.toString.call(obj); if(type=='[object Array]'){ return 'Array'; }else if(type=='[object Object]'){ return 'Object'; }else{ return "obj is not object or array" } } As above javascript function code. I want to run the code with eval to get the results. As follows: #include <IE.au3> Global $oIE = _IECreate("about:blank") Local $sS = "" $sS &= 'function typeObj(obj){' $sS &= ' var type=Object.prototype.toString.call(obj);' $sS &= ' if (type==' & "'" & '[object Array]' & "'" & '){' $sS &= ' return ' & "'" & 'Array' & "'" & ';' $sS &= ' }else if (type==' & "'" & '[object Object]' & "'" & '){' $sS &= ' return ' & "'" & 'Object' & "'" & ';' $sS &= ' }else{' $sS &= ' return "obj is not object or array"' $sS &= ' }' $sS &= '};var x=new typeObj(' & $oIE & ');return x;' ConsoleWrite("JSEval : " & JSEval($sS) & @CRLF) _IEQuit($oIE) $oIE = 0 Func JSEval($str) ;Javascript表达式求值 Local $sRet = "" With ObjCreate("MSScriptControl.ScriptControl") .Language = "javascript" $sRet = .Eval($str) EndWith Return $sRet EndFunc But but I get an error: Quote $sRet = .Eval($str) $sRet = ^ ERROR Thanks in advance! Edited January 15, 2021 by Letraindusoir Link to comment Share on other sites More sharing options...
Danp2 Posted January 14, 2021 Share Posted January 14, 2021 What is your end goal? Please describe what you want to accomplish with this code. Where did you find this Javascript code? There are issues with the Javascript code. You can test it in one of the online sites like https://js.do/ Letraindusoir 1 Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
Nine Posted January 14, 2021 Share Posted January 14, 2021 Here one way to create a JS object in IE : #include <Constants.au3> #include <IE.au3> Global $oIE = _IECreate("about:blank") $oIE.Document.parentWindow.execScript("document.body.oAutoIt = eval;") Global $eval = Execute("$oIE.Document.body.oAutoIt") If Not IsObj($eval) Then Exit MsgBox ($MB_SYSTEMMODAL,"","Error creating JS object") MsgBox($MB_SYSTEMMODAL, "Eval", $eval('typeof 5')) ; execute js and get return $eval("alert('Test');") Letraindusoir 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Letraindusoir Posted January 15, 2021 Author Share Posted January 15, 2021 (edited) Thank you two great master for paying attention to this small problem! Now I'm learning to transform some JS functions in Au3 ways to verify that the same results can be obtained A few minor syntax errors in the previous post have been corrected: elseif-->else if Now I know why I was wrong.The Correct code is as follows: #include <IE.au3> Global $oIE = _IECreate("about:blank",0,0,1,0) Local $sS = "" $sS &= 'function typeObj(obj){' $sS &= ' var type=Object.prototype.toString.call(obj);' $sS &= ' if (type==' & "'" & '[object Array]' & "'" & '){' $sS &= ' return ' & "'" & 'Array' & "'" & ';' $sS &= ' }else if (type==' & "'" & '[object Object]' & "'" & '){' $sS &= ' return ' & "'" & 'Object' & "'" & ';' $sS &= ' }else{' $sS &= ' return "obj is not object or array"' $sS &= ' }' $sS &= '};typeObj(123);' ConsoleWrite("JSEval : " & JSEval($sS) & @CRLF) _IEQuit($oIE) $oIE = 0 Func JSEval($str) Local $sRet = "" With ObjCreate("MSScriptControl.ScriptControl") .Language = "javascript" $sRet = .Eval($str) EndWith Return $sRet EndFunc Quote var x=new typeObj(' & $oIE & ');return x; <----Syntax Error Edited January 15, 2021 by Letraindusoir Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now