Jump to content

Change Useragent of Firefox


Go to solution Solved by jguinch,

Recommended Posts

I do not think that FF.au3 can do that.

You can build your own function to do what you want. The function could be :

 - checks if Firefox is open (if yes, do not proceed)

 - search for FF user profile (locates the prefs.js file)

 - edit the prefs.js file and adds the line  user_pref("general.useragent.override","Some string");

 

I give you a point to start it with a function to list the user's profile(s) path(s)

#Include <File.au3>
#Include <Array.au3> ; just used for ArrayDisplay in the example

$aProfiles = _FF_ProfilesList()
_ArrayDisplay($aProfiles)

Func _FF_ProfilesList()
    Local $aResult[1][2] = [[0]]
    Local $sProfiles = @AppDataDir & "\Mozilla\Firefox\profiles.ini"
    Local $aSections = IniReadSectionNames($sProfiles)

    If @error OR NOT IsArray($aSections) Then Return SetError(1, 1, 0)

    For $i = 1 To $aSections[0]
        If $aSections[$i] <> "General" Then
            Local $sProfileName    = IniRead($sProfiles, $aSections[$i], "Name", "error")
            Local $bIsRelative     = IniRead($sProfiles, $aSections[$i], "IsRelative", "error")
            Local $sProfilePath    = IniRead($sProfiles, $aSections[$i], "Path", "error")

            If $bIsRelative = "error" OR $sProfilePath = "error" OR $sProfileName = "error" Then ContinueLoop

            If $bIsRelative = "1" Then $sProfilePath = _PathFull( @AppDataDir & "\Mozilla\Firefox\" & StringReplace($sProfilePath, "/", "\") )

            If NOT FileExists($sProfilePath & "\prefs.js") Then ContinueLoop
            $aResult[0][0] = UBound($aResult)
            Redim $aResult[ UBound($aResult) + 1][2]

            If IniRead($sProfiles, $aSections[$i], "Default", "error") = "1" Then $aResult[0][1] = UBound($aResult) - 1

            $aResult[UBound($aResult) - 1][0] = $sProfileName
            $aResult[UBound($aResult) - 1][1] = $sProfilePath
        EndIf
    Next

    If $aResult[0][1] = "" AND $aResult[0][0] > 0 Then $aResult[0][1] = 1

    Return $aResult

EndFunc
Link to comment
Share on other sites

Thank you very much.

What am I doing wrong with writing to prefs.js?

That is what I've tried:

#Include <File.au3>
#include <FF.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>


Func ChangeFFUseragent()
  Local Const $sPrefPath = @UserProfileDir & "\Application Data\Mozilla\Firefox\Profiles\autoitbrowser.default\prefs.js"
if FileExists($sPrefPath) Then
; If file not exists, exit
    If Not FileExists($sPrefPath) Then
       MsgBox(4096,"Error", " Error finding Pref.js     error:" & @error)
       Exit
    EndIf
    ; Open the file for writing (append to the end of a file) and store the handle to a variable.
    Local $hFileOpen = FileOpen($sPrefPath, 0)
    If $hFileOpen = -1 Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.")
    EndIf
 ; Read the contents of the file using the handle returned by FileOpen.
    Local $sFileRead = FileRead($hFileOpen)
;Write new Useragent
  FileWriteLine($hFileOpen, "user_pref("general.useragent.override", "Some string");" & @CRLF)
EndIf
EndFunc

;Start FF
 _FFConnect()
; If not connected to FF, exit
If _FFIsConnected() Then

    _FFOpenURL("http://www.whatsmyuseragent.com/")
    ; browser version
    $sVersion = _FFCmd("navigator.userAgent")
    If Not @error Then MsgBox(64,"Browser version:",$sVersion)
Else
    MsgBox(64,"Error:","Can't conncect to FireFox")
EndIf


 

Link to comment
Share on other sites

  Local Const $sPrefPath = @UserProfileDir & "\Application Data\Mozilla\Firefox\Profiles\autoitbrowser.default\prefs.js"

What is autoitbrowser.default ? Is it really the location of the profile ?

The line is written or no ?

Link to comment
Share on other sites

  • Solution

It's because you have open the file in Read mode.

Here is your version, with some changes to work :

Func ChangeFFUseragent()
    Local Const $sPrefPath = "C:\Users\Jerome\AppData\Roaming\Mozilla\Firefox\Profiles\er6wwvjm.default\prefs.js"
    if FileExists($sPrefPath) Then
        ; If file not exists, exit

        ; Open the file for writing (append to the end of a file) and store the handle to a variable.
        Local $hFileOpen = FileOpen($sPrefPath, 1)
        If $hFileOpen = -1 Then
            MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.")
        EndIf
        ; Read the contents of the file using the handle returned by FileOpen.
        Local $sFileRead = FileRead($hFileOpen)
        ;Write new Useragent
        FileWriteLine($hFileOpen, "user_pref(""general.useragent.override"", ""Some string"");" & @CRLF)
    Else
        MsgBox(4096,"Error", " Error finding Pref.js     error:" & @error)
        Exit
    EndIf
EndFunc

Now, if you call the ChangeFFUseragent several times, a line will be added each time. It's cleaner to add the line only if needed and the replace it if already exists.

Try with this shortest code (there are two func parameters :  profileDir and UserAgent string.

Global $FFProfile = @UserProfileDir & "\Application Data\Mozilla\Firefox\Profiles\autoitbrowser.default"
ChangeFFUseragent($FFProfile, "MyNewSuperUserAgentddd")


Func ChangeFFUseragent($sPrefPath, $sUserAgent)
    If ProcessExists("firefox.exe") Then Return SetError(2, 0, 0)
    
    Local $sFileRead = FileRead($sPrefPath & "\prefs.js")
    If @error Then Return SetError(1, 0, 0)
    
    $sFileRead = StringRegExpReplace($sFileRead, '(?i)("general\.useragent\.override", ?")([^"]+)', "$1" & $sUserAgent)
    If NOT @extended Then $sFileRead &= @CRLF & 'user_pref("general.useragent.override", "' & $sUserAgent & '");'
    
    $hFile = FileOpen($sPrefPath & "\prefs.js", 2)
    If $hFile = -1 Then Return SetError(1, 0, 0)
    
    FileWrite($hFile, $sFileRead)
    FileClose($hFile)
    
    Return 1
EndFunc
Edited by jguinch
Link to comment
Share on other sites

  • 1 year later...

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...