Header Ads

Change browser homepage programatically

vbscript-mini-logo

When you launch a Web browser, it automatically opens a new window with a default home page--usually Google.com or Bing.com--loaded and ready to go. You can set the home page to any website you prefer, however. Here's how to change the home page in Microsoft Internet Explorer, Google Chrome, and Mozilla Firefox.


'=================================================
'   Author: Kulverstukas
'   Date: 2014.06.23
'   Description:
'       Changes the homepage for 3 most used browsers: Firefox, Chrome and IE.
'       Could be added to a Logon script on a domain to do it every time user logs on.
'=================================================

Set wshShell = CreateObject("WScript.Shell")
Set objFS = CreateObject("Scripting.FileSystemObject")
 
'================= Firefox Block =================
Const ffFile = "\prefs.js"
Const ffTemp = "\prefs1.js"
Const homePageSubStr = """browser.startup.homepage"""
Const homePagePref = "user_pref(""browser.startup.homepage"", ""http://localhost/"");"
Const showHomepageSubStr = """browser.startup.page"""
Const showHomepagePref = "user_pref(""browser.startup.page"", 1);"
Const ffDir = "%appdata%\Mozilla\Firefox\Profiles"
foundHomePage = False
foundShowHomePage = False
 
If (objFS.FolderExists(wshShell.ExpandEnvironmentStrings(ffDir))) Then
    Set profiles = objFS.GetFolder(wshShell.ExpandEnvironmentStrings(ffDir)).SubFolders
    For Each profile in profiles  ' change the homepage for every profile we can find
        Set textStream = objFS.GetFile(profile & ffFile).OpenAsTextStream(1, -2)
        Set objOutFile = objFS.CreateTextFile(profile & ffTemp, True)
        Do Until textStream.AtEndOfStream
            strLine = textStream.ReadLine
            If (InStr(strLine, homePageSubStr) > 0) Then
                objOutFile.WriteLine(homePagePref)
                foundHomePage = True
            Else If (InStr(strLine, showHomepageSubStr) > 0) Then
                objOutFile.WriteLine(showHomepagePref)
                foundShowHomePage = True
            Else
                objOutFile.WriteLine(strLine)
            End If
            End If
        Loop
        ' append the configs if these lines were not found
        If (foundHomePage = False) Then
            objOutFile.WriteLine(homePagePref)
        End If
        If (foundShowHomePage = False) Then
            objOutFile.WriteLine(showHomepagePref)
        End If
 
        objOutFile.Close()
        textStream.Close()
        objFS.DeleteFile(profile & ffFile)
        objFS.MoveFile profile & ffTemp, profile & ffFile
    Next
End If
 
'================= Chrome  Block =================
Const chromeFile = "\Preferences"
Const chromeTempFile = "\Preferences1"
Const sessionRestoreSubStr = """restore_on_startup"""
Const sessionRestorePref = "      ""restore_on_startup"": 4,"
Const sessionStartupUrlSubStr = """startup_urls"""
Const sessionStartupUrlPref = "      ""startup_urls"": [ ""http://localhost/"" ],"
Const chromeDirVista = "%localappdata%\Google\Chrome\User Data\Default"
Const chromeDirXP = "%userprofile%\Local Settings\Application Data"
Const strComputer = "."
Const vistaVer = "6.1"
Const xpVer = "5.1"
 
' we must detect the system here so that we can know what path to take
Set dtmConvertedDate = CreateObject("WbemScripting.SWbemDateTime")
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set oss = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
finalDir = ""
For Each os in oss ' could be better I suppose than this I guess :P
    If (InStr(os.Version, xpVer) > 0) Then
        finalDir = wshShell.ExpandEnvironmentStrings(chromeDirXP)
    Else If (InStr(os.Version, vistaVer) > 0) Then
        finalDir = wshShell.ExpandEnvironmentStrings(chromeDirVista)
    End If
    End If
Next
If (objFS.FileExists(finalDir & chromeFile)) Then
    Set textStream = objFS.GetFile(finalDir & chromeFile).OpenAsTextStream(1, -2)
    Set objOutFile = objFS.CreateTextFile(finalDir & chromeTempFile, True)
    ' business as usual...
    Do Until textStream.AtEndOfStream
        strLine = textStream.ReadLine
        If (InStr(strLine, sessionRestoreSubStr) > 0) Then
            objOutFile.WriteLine(sessionRestorePref)
        Else If (InStr(strLine, sessionStartupUrlSubStr) > 0) Then
            objOutFile.WriteLine(sessionStartupUrlPref)
        Else
            objOutFile.WriteLine(strLine)
        End If
        End If
    Loop
    objOutFile.Close()
    textStream.Close()
    objFS.DeleteFile(finalDir & chromeFile)
    objFS.MoveFile finalDir & chromeTempFile, finalDir & chromeFile
End If
 
'================= Internet Explorer Block =======
wshShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\Main\Start Page", "http://localhost/", "REG_SZ"
wshShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\Main\RunOnceHasShown", "1", "REG_DWORD"
wshShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\Main\RunOnceComplete", "1", "REG_DWORD"
Powered by Blogger.