Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 11/22/2016 in all areas

  1. Introduction JSON (Javascript Object Notation) is a popular data-interchange format and supported by a lot of script languages. On AutoIt, there is already a >JSON UDF written by Gabriel Boehme. It is good but too slow, and not supports unicode and control characters very well. So I write a new one (and of course, fast one as usual). I use a machine code version of JSON parser called "jsmn". jsmn not only supports standard JSON, but also accepts some non-strict JSON string. See below for example. Important Update!! I rename the library from jsmn.au3 to json.au3. All function names are changed, too. Decoding Function Json_Decode($Json) $Json can be a standard or non-standard JSON string. For example, it accepts: { server: example.com port: 80 message: "this looks like a config file" } The most JSON data type will be decoded into corresponding AutoIt variable, including 1D array, string, number, true, false, and null. JSON object will be decoded into "Windows Scripting Dictionary Object" retuned from ObjCreate("Scripting.Dictionary"). AutoIt build-in functions like IsArray, IsBool, etc. can be used to check the returned data type. But for Object and Null, Json_IsObject() and Json_IsNull() should be used. If the input JSON string is invalid, @Error will be set to $JSMN_ERROR_INVAL. And if the input JSON string is not finish (maybe read from stream?), @Error will be set to $JSMN_ERROR_PART. Encoding Function Json_Encode($Data, $Option = 0, $Indent = "\t", $ArraySep = ",\r\n", $ObjectSep = ",\r\n", $ColonSep = ": ") $Data can be a string, number, bool, keyword(default or null), 1D arrry, or "Scripting.Dictionary" COM object. Ptr will be converted to number, Binary will be converted to string in UTF8 encoding. Other unsupported types like 2D array, dllstruct or object will be encoded into null. $Option is bitmask consisting following constant: $JSON_UNESCAPED_ASCII ; Don't escape ascii charcters between chr(1) ~ chr(0x1f) $JSON_UNESCAPED_UNICODE ; Encode multibyte Unicode characters literally $JSON_UNESCAPED_SLASHES ; Don't escape / $JSON_HEX_TAG ; All < and > are converted to \u003C and \u003E $JSON_HEX_AMP ; All &amp;amp;amp;amp;s are converted to \u0026 $JSON_HEX_APOS ; All ' are converted to \u0027 $JSON_HEX_QUOT ; All " are converted to \u0022 $JSON_PRETTY_PRINT ; Use whitespace in returned data to format it $JSON_STRICT_PRINT ; Make sure returned JSON string is RFC4627 compliant $JSON_UNQUOTED_STRING ; Output unquoted string if possible (conflicting with $JSMN_STRICT_PRINT) Most encoding option have the same means like PHP's json_enocde() function. When $JSON_PRETTY_PRINT is set, output format can be change by other 4 parameters ($Indent, $ArraySep, $ObjectSep, and $ColonSep). Because these 4 output format parameters will be checked inside Jsmn_Encode() function, returned string will be always accepted by Jsmn_Decode(). $JSON_UNQUOTED_STRING can be used to output unquoted string that also accetped by Jsmn_Decode(). $JSON_STRICT_PRINT is used to check output format setting and avoid non-standard JSON output. So this option is conflicting with $JSON_UNQUOTED_STRING. Get and Put Functions Json_Put(ByRef $Var, $Notation, $Data, $CheckExists = False) Json_Get(ByRef $Var, $Notation) These functions helps user to access object or array more easily. Both dot notation and square bracket notation can be supported. Json_Put() by default will create non-exists objects and arrays. For example: Local $Obj Json_Put($Obj, ".foo", "foo") Json_Put($Obj, ".bar[0]", "bar") Json_Put($Obj, ".test[1].foo.bar[2].foo.bar", "Test") Local $Test = Json_Get($Obj, '["test"][1]["foo"]["bar"][2]["foo"]["bar"]') ; "Test" Object Help Functions Json_ObjCreate() Json_ObjPut(ByRef $Object, $Key, $Value) Json_ObjGet(ByRef $Object, $Key) Json_ObjDelete(ByRef $Object, $Key) Json_ObjExists(ByRef $Object, $Key) Json_ObjGetCount(ByRef $Object) Json_ObjGetKeys(ByRef $Object) Json_ObjClear(ByRef $Object) These functions are just warps of "Scripting.Dictionary" COM object. You can use these functions if you are not already familiar with it. == Update 2013/05/19 == * Add Jsmn_Encode() option "$JSMN_UNESCAPED_ASCII". Now the default output of Json_Encode() is exactly the same as PHP's json_encode() function (for example, chr(1) will be encoded into u0001). $JSON_UNESCAPED_ASCII ; Don't escape ascii charcters between chr(1) ~ chr(0x1f) == Update 2015/01/08 == * Rename the library from jsmn.au3 to json.au3. All function names are changed, too. * Add Json_Put() and Json_Get() * Add Null support * Using BinaryCall.au3 to loading the machine code. == Update 2018/01/13== (Jos) * Add JsonDump() to list all Json Keys and their values to easily figure out what they are. == Update 2018/10/01== (Jos) * Fixed JsonDump() some fields and values were not showing as discussed here - tnx @TheXman . == Update 2018/10/01b== (Jos) * Added Json_ObjGetItems, Tidied source and fixed au3check warnings - tnx @TheXman . == Update 2018/10/28== (Jos) * Added declaration for $value to avoid au3check warning - tnx @DerPensionist == Update 2018/12/16== (Jos) * Added another declaration for $value to avoid au3check warning and updated the version at the top - tnx @maniootek == Update 2018/12/29== (Jos) * Changed Json_ObjGet() and Json_ObjExists() to allow for multilevel object in string. == Update 2019/01/17== (Jos) * Added support for DOT notation in JSON functions. == Update 2019/07/15== (Jos) * Added support for reading keys with a dot inside when using a dot as separator (updated) == Update 2021/11/18== (TheXman) * Update details in below post: == Update 2021/11/20== (TheXman) * Minor RegEx update, no change to the functionality or result._Json(2021.11.20).zip
    1 point
  2. This is the result of the internal floating point representation of the numbers -> see https://en.wikipedia.org/wiki/Floating_point As a workaround you can use Round() ConsoleWrite( Round($a, 10) & @CRLF)
    1 point
  3. Hi, you could check the file association and get the executable. If the executable is Adobe Reader you can start it direct with the command line option /n. That means Reader will start the file in a new instance. If you find another PDF viewer you can start it also direct. If you find nothing you can give a hint or a link to a PDF viewer. br, Reinhard PS.: In cmd you can use "assoc .pdf" and then use the result in "ftype ....." to get the program, but I think I saw also examples here.
    1 point
  4. jchd

    SQL Query Manipulation

    @S0lidFr0st I say because it's always (like 99.999% of the times) a bad idea to mix data and schema names. To illustrate imagine you create a daily table of <something>. You end up filling your database with a large number of distinct tables storing information having the exact same semantics. Now say your duty is to exhibit rows matching some criterion and having occured in the last 7 days. Your query will look like this: select <list of columns> from Something_2016-11-16 where <criterion> union all select <list of columns> from Something_2016-11-17 where <criterion> union all select <list of columns> from Something_2016-11-18 where <criterion> union all select <list of columns> from Something_2016-11-19 where <criterion> union all select <list of columns> from Something_2016-11-20 where <criterion> union all select <list of columns> from Something_2016-11-21 where <criterion> union all select <list of columns> from Something_2016-11-22 where <criterion> order by ... Geez, you're stuck here: no good criterion order! (remember SQL tables are like maths sets, orderless) Compare with this, where the ISO date is part of the table: select <list of columns> from Something where <criterion> and theDate between date('now', '-6 days') and date('now') order by theDate Which do you find more practical and should be more efficient? EDIT: typing too fast, there should be double quotes or the delimiters your engine requires around the weird table names, e.g. "Something_2016-11-16" else SQL will treat - as substraction (my bad).
    1 point
  5. Instead of openning the files properties, you should use some function to search informations directy in the file : #include <AutoItConstants.au3> #include <Array.au3> Local $sCertfile = @Scriptdir & "\file.cer" Local $iPid = Run('certutil -dump "' & $sCertfile & '"', @SystemDir, @SW_HIDE, $STDOUT_CHILD) ProcessWait($iPid) Local $sContent = StdoutRead($iPid) Local $aInfos = StringRegExp($sContent, "(?ms)(^[A-Z][^:]+\h):(.*?)(?=(?1)|\Z)", 3) If @error Then Exit MsgBox(16, "Error", "Unable to get any certificate informations from this file.") Local $aInfos2D[UBound($aInfos) / 2][2], $iIndex For $i = 0 To UBound($aInfos) - 1 Step 2 $iIndex = $i / 2 $aInfos2D[$iIndex][0] = $aInfos[$i] $aInfos2D[$iIndex][1] = $aInfos[$i + 1] Next _ArrayDisplay($aInfos2D) @Danyfirex : i'm too late...
    1 point
  6. You can use IE* functions to handle that. read about in help file/forum. Saludos
    1 point
  7. Let's play at lower level: TCPStartup() If @error Then _ReportError(@error, @extended, @ScriptLineNumber - 1) Local $sIPAddress = TCPNameToIP("www.hearthpwn.com") If @error Then _ReportError(@error, @extended, @ScriptLineNumber - 1) Local $iSocket = TCPConnect($sIPAddress, 80) If @error Then _ReportError(@error, @extended, @ScriptLineNumber - 1) TCPSend($iSocket, "GET /?cookieTest=1 HTTP/1.1" & @CRLF & _ "Host: www.hearthpwn.com" & @CRLF & _ "Connection: close" & @CRLF & @CRLF) If @error Then _ReportError(@error, @extended, @ScriptLineNumber - 1) Local $sData = "", $hTimer = TimerInit() While 1 $sRecv = _TCPRecv($iSocket, 2048) If $sRecv Then $sData &= $sRecv If @error Or @extended Then If @error Then _ReportError(@error, @extended, @ScriptLineNumber - 1) ExitLoop EndIf If $sRecv Then $hTimer = TimerInit() If TimerDiff($hTimer) > 3000 Then ExitLoop Sleep(10) WEnd ConsoleWrite("Response received: " & $sData & @CRLF) TCPCloseSocket($iSocket) TCPShutdown() Func _ReportError($iError, $iExtended, $iLine) ConsoleWrite("Error: " & $iError & @CRLF & _ "Extended: " & $iExtended & @CRLF & _ "Line: " & $iLine & @CRLF) TCPShutdown() Exit EndFunc Func _TCPRecv($iMainsocket, $iMaxLen, $iFlag = 0) If IsArray($iMainsocket) And (UBound($iMainsocket, 0) = 1) And (UBound($iMainsocket) > 0) Then $iMainsocket = $iMainsocket[0] If $iFlag = Default Then $iFlag = 0 $iMainsocket = Number($iMainsocket) $iMaxLen = Number($iMaxLen) $iFlag = Number($iFlag) If $iMainsocket < 0 Or _ $iMaxLen < 1 Or _ Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2) Then Return SetError(-4, 0, -1) ; invalid parameter Local $hWs2 = DllOpen("Ws2_32.dll") If @error Then Return SetError(-2, 0, -1) ;missing DLL Local $bError = 0, $nCode = 0, $nExtended = 0 If Not $bError Then $aRet = DllCall($hWs2, "int", "ioctlsocket", "uint", $iMainsocket, "long", 0x8004667e, "ulong*", 1) ;FIONBIO If @error Then $bError = -1 ElseIf $aRet[0] <> 0 Then ;SOCKET_ERROR $bError = 1 EndIf EndIf Local $tBuf = DllStructCreate("char[" & $iMaxLen & "]") $aRet = DllCall($hWs2, "int", "recv", "uint", $iMainsocket, "ptr", DllStructGetPtr($tBuf), "int", $iMaxLen, "int", 0) If @error Then $bError = -1 ElseIf ($aRet[0] = -1) Or ($aRet[0] = 4294967295) Then ;SOCKET_ERROR $bError = 1 $aRet = DllCall($hWs2, "int", "WSAGetLastError") If @error Then $bError = -1 ElseIf $aRet[0] = 0 Or $aRet[0] = 10035 Then ;WSAEWOULDBLOCK $nCode = -10 ;internal function value, it means no error EndIf ElseIf $aRet[0] = 0 Then $bError = 1 $nCode = -10 $nExtended = 1 ;connection closed Else Local $sResult = DllStructGetData($tBuf, 1) ;data If BitAND($iFlag, 2) = 2 Then ;EOT If StringRight($sResult, 1) = Chr(3) Then $sResult = StringTrimRight($sResult, 1) $nExtended = 2 ;End of Text reached EndIf EndIf If BitAND($iFlag, 1) = 1 Then $sResult = Binary($sResult) EndIf If $bError < 0 Then $nCode = -1 ;internal error $nReturn = "" ;failure ElseIf $bError > 0 Then If Not $nCode Then $aRet = DllCall($hWs2, "int", "WSAGetLastError") If @error Then $nCode = -1 Else $nCode = $aRet[0] EndIf If $nCode = 0 Then $nCode = -3 ;undefined error EndIf If $nCode = -10 Then $nCode = 0 $nReturn = "" Else $nReturn = $sResult EndIf DllClose($hWs2) Return SetError($nCode, $nExtended, $nReturn) EndFunc ;==>_TCPRecv Post here the console output.
    1 point
  8. trancexx

    Winhttp API help

    Uhm... you should be the one sending PM. This isn't bizzaro world. Btw, what did you expect when you posted credentials? That there aren't fuckers around?
    1 point
  9. junkew

    Find text in Chrome page

    https://www.autoitscript.com/wiki/FAQ question 31 is a good point to start
    1 point
  10. junkew

    Value of JS variable in IE

    A nice oneline to call short scripts thru the addressbar $oIE.Navigate2("javascript:console.log(123);void(0);") So this should set a global object reference variable ;~$quote="""" ;~ $js="javascript:var JSglobal = (1,eval)(" & $quote & "this" & $quote & ");void(0);" $js="javascript:var JSglobal = typeof self === 'object' && self.self === self && self;void(0);" consolewrite($js & @CRLF) $oIE.Navigate2($js) @Chimp and this adds a nice helloworld function $js="javascript:document.hello=function(){alert('Really hello world')};void(0);" consolewrite($js & @CRLF) $oIE.Navigate2($js) and this to proof that function exists $js="javascript:document.body.hello();void(0);" consolewrite($js & @CRLF) $oIE.Navigate2($js) and then when called from AutoIT (at least with W10, IE11) consolewrite("Hocus pocus is coming " & @CRLF) $oie.document.hello(); If this logic would work its a small step to get global object returned $js="javascript:alert(JSglobal.ScriptEngine());void(0);" $oIE.Navigate2($js) and then returning it $js="javascript:document.getGlobalJS=function(){return JSglobal;};void(0);" $oIE.Navigate2($js) sleep(2000) Global $ohJS = $oie.document.getGlobalJS() if isobj($ohJS) Then consolewrite("Seems to have retrieved a global object " & @CRLF) EndIf but then failing when doing more in AutoIT consolewrite("Seems to have retrieved a global object " & $ohJS.ScriptEngine() & @CRLF) edit: but it solves at least the issue on not having eval/execscript as you can create your own function returning var values. Alternative could be to set with javascript a document.body.setattribute('whatever', <JS variable>) and do a document.body.getattribute('whatever')
    1 point
×
×
  • Create New...