{"id":2280,"date":"2014-06-25T19:30:36","date_gmt":"2014-06-25T16:30:36","guid":{"rendered":"http:\/\/9v.lt\/blog\/?p=2280"},"modified":"2022-01-19T08:34:39","modified_gmt":"2022-01-19T06:34:39","slug":"change-browser-homepage-programatically-windows","status":"publish","type":"post","link":"https:\/\/9v.lt\/blog\/change-browser-homepage-programatically-windows\/","title":{"rendered":"Change browser homepage programatically for Windows"},"content":{"rendered":"<p>At work we had to change the homepage of all browsers for students for convenience reasons and so I wrote this VBScript, which is my first ever, so it could be made better maybe, but it works great &#8211; I seem to fall in love with it :D . I only wrote the script to change Firefox, Chrome and InternetExploder settings, because only those browsers are available on computers accessible to students.<br \/>\nThe script was put in the Logon script of our Domain to run every time a user logs in.<br \/>\nWhole script can be downloaded from <a href=\"http:\/\/9v.lt\/projects\/other\/change_browser_pref.vbs\" target=\"_blank\" rel=\"noopener\">here<\/a> and I&#8217;m posting the code for quick review :)<br \/>\n<!--more--><\/p>\n<pre lang=\"vb\" line=\"1\">\r\n'=================================================\r\n'   Author: Kulverstukas\r\n'   Date: 2014.06.23\r\n'   Description:\r\n'       Changes the homepage for 3 most used browsers: Firefox, Chrome and IE.\r\n'       Could be added to a Logon script on a domain to do it every time user logs on.\r\n'=================================================\r\n\r\nSet wshShell = CreateObject(\"WScript.Shell\")\r\nSet objFS = CreateObject(\"Scripting.FileSystemObject\")\r\n\r\n'================= Firefox Block =================\r\nConst ffFile = \"\\prefs.js\"\r\nConst ffTemp = \"\\prefs1.js\"\r\nConst homePageSubStr = \"\"\"browser.startup.homepage\"\"\"\r\nConst homePagePref = \"user_pref(\"\"browser.startup.homepage\"\", \"\"http:\/\/localhost\/\"\");\"\r\nConst showHomepageSubStr = \"\"\"browser.startup.page\"\"\"\r\nConst showHomepagePref = \"user_pref(\"\"browser.startup.page\"\", 1);\"\r\nConst ffDir = \"%appdata%\\Mozilla\\Firefox\\Profiles\"\r\nfoundHomePage = False\r\nfoundShowHomePage = False\r\n\r\nIf (objFS.FolderExists(wshShell.ExpandEnvironmentStrings(ffDir))) Then\r\n    Set profiles = objFS.GetFolder(wshShell.ExpandEnvironmentStrings(ffDir)).SubFolders\r\n    For Each profile in profiles  ' change the homepage for every profile we can find\r\n        Set textStream = objFS.GetFile(profile & ffFile).OpenAsTextStream(1, -2)\r\n        Set objOutFile = objFS.CreateTextFile(profile & ffTemp, True)\r\n        Do Until textStream.AtEndOfStream\r\n            strLine = textStream.ReadLine\r\n            If (InStr(strLine, homePageSubStr) > 0) Then\r\n                objOutFile.WriteLine(homePagePref)\r\n                foundHomePage = True\r\n            Else If (InStr(strLine, showHomepageSubStr) > 0) Then\r\n                objOutFile.WriteLine(showHomepagePref)\r\n                foundShowHomePage = True\r\n            Else\r\n                objOutFile.WriteLine(strLine)\r\n            End If\r\n            End If\r\n        Loop\r\n        ' append the configs if these lines were not found\r\n        If (foundHomePage = False) Then\r\n            objOutFile.WriteLine(homePagePref)\r\n        End If\r\n        If (foundShowHomePage = False) Then\r\n            objOutFile.WriteLine(showHomepagePref)\r\n        End If\r\n        \r\n        objOutFile.Close()\r\n        textStream.Close()\r\n        objFS.DeleteFile(profile & ffFile)\r\n        objFS.MoveFile profile & ffTemp, profile & ffFile\r\n    Next\r\nEnd If\r\n\r\n'================= Chrome  Block =================\r\nConst chromeFile = \"\\Preferences\"\r\nConst chromeTempFile = \"\\Preferences1\"\r\nConst sessionRestoreSubStr = \"\"\"restore_on_startup\"\"\"\r\nConst sessionRestorePref = \"      \"\"restore_on_startup\"\": 4,\"\r\nConst sessionStartupUrlSubStr = \"\"\"startup_urls\"\"\"\r\nConst sessionStartupUrlPref = \"      \"\"startup_urls\"\": [ \"\"http:\/\/localhost\/\"\" ],\"\r\nConst chromeDirVista = \"%localappdata%\\Google\\Chrome\\User Data\\Default\"\r\nConst chromeDirXP = \"%userprofile%\\Local Settings\\Application Data\"\r\nConst strComputer = \".\"\r\nConst vistaVer = \"6.1\"\r\nConst xpVer = \"5.1\"\r\n\r\n' we must detect the system here so that we can know what path to take\r\nSet dtmConvertedDate = CreateObject(\"WbemScripting.SWbemDateTime\")\r\nSet objWMIService = GetObject(\"winmgmts:{impersonationLevel=impersonate}!\\\\\" & strComputer & \"\\root\\cimv2\")\r\nSet oss = objWMIService.ExecQuery(\"Select * from Win32_OperatingSystem\")\r\nfinalDir = \"\"\r\nFor Each os in oss ' could be better I suppose than this I guess :P\r\n    If (InStr(os.Version, xpVer) > 0) Then\r\n        finalDir = wshShell.ExpandEnvironmentStrings(chromeDirXP)\r\n    Else If (InStr(os.Version, vistaVer) > 0) Then\r\n        finalDir = wshShell.ExpandEnvironmentStrings(chromeDirVista)\r\n    End If\r\n    End If\r\nNext\r\nIf (objFS.FileExists(finalDir & chromeFile)) Then\r\n    Set textStream = objFS.GetFile(finalDir & chromeFile).OpenAsTextStream(1, -2)\r\n    Set objOutFile = objFS.CreateTextFile(finalDir & chromeTempFile, True)\r\n    ' business as usual...\r\n    Do Until textStream.AtEndOfStream\r\n        strLine = textStream.ReadLine\r\n        If (InStr(strLine, sessionRestoreSubStr) > 0) Then\r\n            objOutFile.WriteLine(sessionRestorePref)\r\n        Else If (InStr(strLine, sessionStartupUrlSubStr) > 0) Then\r\n            objOutFile.WriteLine(sessionStartupUrlPref)\r\n        Else\r\n            objOutFile.WriteLine(strLine)\r\n        End If\r\n        End If\r\n    Loop\r\n    objOutFile.Close()\r\n    textStream.Close()\r\n    objFS.DeleteFile(finalDir & chromeFile)\r\n    objFS.MoveFile finalDir & chromeTempFile, finalDir & chromeFile\r\nEnd If\r\n\r\n'================= Internet Explorer Block =======\r\nwshShell.RegWrite \"HKCU\\Software\\Microsoft\\Internet Explorer\\Main\\Start Page\", \"http:\/\/localhost\/\", \"REG_SZ\"\r\nwshShell.RegWrite \"HKCU\\Software\\Microsoft\\Internet Explorer\\Main\\RunOnceHasShown\", \"1\", \"REG_DWORD\"\r\nwshShell.RegWrite \"HKCU\\Software\\Microsoft\\Internet Explorer\\Main\\RunOnceComplete\", \"1\", \"REG_DWORD\"\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>At work we had to change the homepage of all browsers for students for convenience<\/p>\n","protected":false},"author":2,"featured_media":2282,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,750],"tags":[911,912,144,414,910],"class_list":["post-2280","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-projects","category-software-projects","tag-browser","tag-homepage","tag-script","tag-vbscript","tag-visual-basic"],"_links":{"self":[{"href":"https:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/posts\/2280","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/comments?post=2280"}],"version-history":[{"count":0,"href":"https:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/posts\/2280\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/media\/2282"}],"wp:attachment":[{"href":"https:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/media?parent=2280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/categories?post=2280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/tags?post=2280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}