4ggr35510n Posted November 18, 2010 Share Posted November 18, 2010 (edited) Two functions (but second is only operating on data returned by the first one) receiving cookies from Firefox. Enjoy! Comments and feedback are welcome!PS FF.au3 required!expandcollapse popup;; ======= ;; ;; EXAMPLE ;; ;; ======= ;; #include <FF.au3> #include <Array.au3> _FFStart() $cookie_name = _FFGetSingleCookie('ID', 'google') ; receives single cookie - first one containing 'ID' (case sensitive!) in name, and 'google' in host MsgBox(0, '', $cookie_name) $COOKIE_ARRAY = _FFGetSingleCookie('id', '', True) ; receives 3-elements array with cookie: name, value, host of a first cookie, where name contains 'id' (case sensitive!) _ArrayDisplay($COOKIE_ARRAY) $timer = TimerInit() $global_cookie_array = _FFGetAllCookies() ; returns all cookies in 2dimensional, 3column array with cookies NAME, VALUE and HOST MsgBox(0, '', TimerDiff($timer)) ; as you can see it's pretty fast... BUT ArrayDisplaying is rather slow with many cookies... _ArrayDisplay($global_cookie_array) MsgBox(0, '', TimerDiff($timer) ) ;; ======= ;; ;; ======= ;; ;=============================================================================== ; ; Function Name: _FFGetAllCookies ; Description:: Returns array with _ALL_ cookies ; Parameter(s): - none - ; Requirement(s): FF.au3 ; Return Value(s): On succes: 2-dimensional array with all cookies (column names:) ; NAME VALUE HOST ; On failure: '_FFCmdErr' ; ;=============================================================================== Func _FFGetAllCookies() ;~ © 4ggr35510n © Local $aT, $aTA $sCS = _FFCmd("var cookiestring='';var cookieManager=Components.classes['@mozilla.org/cookiemanager;1'].getService(Components.interfaces.nsICookieMana"& _ "ger);var iter=cookieManager.enumerator;while(iter.hasMoreElements()){var cookie=iter.getNext();if(cookie instanceof Components.interfaces.nsICookie){"& _ "cookiestring+=cookie.name+'='+cookie.value+'='+cookie.host+';';}};cookiestring;") $aTA = StringSplit($sCS, ';') Local $aCA[$aTA[0]-1][3] For $i = 1 to $aTA[0] - 1 $aT = StringSplit($aTA[$i], '=') $aCA[$i-1][0] = $aT[1] If $aT[0] > 3 Then ; in case that cookie.value contains equals signs '=' [ it's more common then you think! ] For $j = 2 to $aT[0] - 1 $aCA[$i-1][1] &= $aT[$j] & '=' Next $aCA[$i-1][1] &= $aT[$aT[0]-1] Else $aCA[$i-1][1] &= $aT[2] EndIf $aCA[$i-1][2] = $aT[$aT[0]] Next Return $aCA ;~ © 4ggr35510n © EndFunc ;=============================================================================== ; ; Function Name: _FFGetSingleCookie ; Description:: Returns cookie with a given name ; ; Parameter(s): ; $sName - Name of a cookie you want receive, used in StringRegExp as a pattern ; $sUrl - Optional: (default = '') : url (host) of a cookie, used in StringRegExp as a pattern ; $iMode - Optional: (Default = False) : determines is Return Value a string containing value of cookie, or 3-elements array containing ; name, value and host of a cookie ; | False - returns string (cookie.value) ; | True - returns array (cookie.name, cookie.value and cookie.host) ; Requirement(s): FF.au3 ; Return Value(s): On succes: Cookie value or 3-elements array ; On failure (no cookie matches given sName and sUrl pattern): -1 ; ;=============================================================================== Func _FFGetSingleCookie($sName, $sUrl = '', $iMode = False) ;~ © 4ggr35510n © If $sURL = Default Then $sUrl = '' Local $aCA = _FFGetAllCookies() If $iMode Then Local $aOut[3] EndIf For $i = 0 To UBound($aCA) -1 If StringRegExp($aCA[$i][0], $sName, 0) And StringRegExp($aCA[$i][2], $sURL, 0) Then Switch $iMode Case False Return $aCA[$i][1] Case True $aOut[0] = $aCA[$i][0] $aOut[1] = $aCA[$i][1] $aOut[2] = $aCA[$i][2] Return $aOut EndSwitch EndIf Next Return -1 ;~ © 4ggr35510n © EndFunc Edited November 19, 2010 by 4ggr35510n Link to comment Share on other sites More sharing options...
wakillon Posted November 19, 2010 Share Posted November 19, 2010 Nice job, Thanks to share ! AutoIt 3.3.14.2 X86 - SciTE 3.6.0 - WIN 8.1 X64 - Other Example Scripts Link to comment Share on other sites More sharing options...
Acanis Posted August 9, 2013 Share Posted August 9, 2013 Hey, extreme usefull! I added 2 functions to use this stuff to f.e. share sessions between FF and WinHttp. expandcollapse popup;=============================================================================== ; Function Name: _FFGetCookie ; Description:: Returns string with the whole Cookie. ; Parameter(s): $sUrl - URL of the target ; Requirement(s): FF.au3 ; Return Value(s): On succes: String with the whole Cookie. ; On failure: '_FFCmdErr' ; Author : Acanis @codebot.de ;=============================================================================== Func _FFGetCookie($sUrl) Local $aArray, $sString $aArray = _FFGetCookieGroup($sUrl) For $i = 0 To UBound($aArray) - 1 $sString &= $aArray[$i][0] & "=" & $aArray[$i][1] & "; " Next Return $sString EndFunc ;=============================================================================== ; Function Name: _FFGetCookieGroup ; Description:: Returns array with all cookies from one address. ; Parameter(s): $sUrl - URL of the target ; Requirement(s): FF.au3 ; Return Value(s): On succes: 2-dimensional array with "all wanted" cookies. ; On failure: '_FFCmdErr' ; Author : Acanis @codebot.de ;=============================================================================== Func _FFGetCookieGroup($sUrl) Local $aCookies, $iCounter, $iPlace $aCookies = _FFGetAllCookies() For $i = 0 To UBound($aCookies) - 1 If StringRegExp($aCookies[$i][2], $sURL, 0) Then $iCounter += 1 Next Dim $aCookieGroup[$iCounter][2] For $i = 0 To UBound($aCookies) - 1 If StringRegExp($aCookies[$i][2], $sURL, 0) Then $aCookieGroup[$iPlace][0] = $aCookies[$i][0] $aCookieGroup[$iPlace][1] = $aCookies[$i][1] $iPlace += 1 EndIf Next Return $aCookieGroup EndFunc 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