Tuesday, May 29, 2012

VB6.0 - Set Mozilla Firefox & IE Default Home Site Via Code

Setelah berhasil mendefaultkan Google Chrome home page/site, maka sekarang kita akan mendefaultkan 2 browser lainnya, yaitu Internet Explorer dan Mozilla Firefox. Bagaimanakah caranya? Untuk Internet Explorer maka yang perlu kita lakukan adalah sedikit meng-utak-atik registry. Disini kita akan menggunakan cara akses registry yang mudah dengan menggunakan komponen jadi milik Microsoft yaitu "Microsoft Script Host Object Model" atau nama ocx-nya WSHOM.OCX seperti yang telah dibahas pada artikel yang lain. Adapun implementasi kodenya:
Option Explicit   

'Prosedure fungsi ini simpan di module
Public Sub SetIEHomePage(URL As String)
Dim wsh As New WshShell
wsh.RegWrite "HKCU" & "\Software\Microsoft\Internet Explorer\Main\Start Page", URL
Set wsh = Nothing
End Sub

'Cara menggukannya fungsi di atas
Private Sub Command1_Click()
Call SetIEHomePage("http://khoiriyyah.blogspot.com")
End Sub

Sedangkan untuk Mozilla Firefox hampir sama dengan Google Chrome yaitu dengan cara merubah beberapa jajar kode yang terdapat pada file tertentu. Adapun implementasi kodenya:
Option Explicit 

Public Sub
SetFirefoxHomepage(URL As String)

Dim
strPath As String, strProfile As String
Dim
strContent As String, strReplace As String
Dim
regex As RegExp

strPath = Environ("APPDATA")
strPath = strPath & "\Mozilla\Firefox\Profiles\"
strProfile = Dir(strPath & "*.default", vbDirectory)

If
Len(strProfile) Then
strPath = strPath & strProfile & "\prefs.js"
strReplace = "user_pref(""browser.startup.homepage"", """ & URL & """);"

strContent = fGetFileContents(strPath)
Set regex = New RegExp

If
InStr(1, strContent, Chr(34) & "browser.startup.homepage" & Chr(34)) = 0 Then
strContent = strContent & vbCrLf & "user_pref(""browser.startup.homepage"", """ & URL & """);"
sPutStringToFile strContent, strPath
Exit Sub
ElseIf
InStr(1, strContent, strReplace) Then
Exit Sub
End If

regex.Pattern = "user_pref\(""browser.startup.homepage"",\s""(.*)""\);"

strContent = regex.Replace(strContent, strReplace)
sPutStringToFile strContent, strPath

End If

End Sub

Public Function
fGetFileContents(strPath As String) As String
Dim
hFile As Integer
Dim
strFileContent As String


If
Len(Dir(strPath)) = 0 Then Exit Function

On Error GoTo
ErrGetFile
hFile = FreeFile

Open
strPath For Binary As #hFile
strFileContent = Space(LOF(hFile))
Get #hFile, , strFileContent
Close #hFile

fGetFileContents = strFileContent
Exit Function

ErrGetFile:

Close
MsgBox Err.Description, vbCritical, "GetFileContents"

End Function

Public Sub sPutStringToFile(strContent As String, strPath As String)
Dim hFile As Integer

'If file exists delete it.
On Error Resume Next
Kill
strPath
On Error GoTo ErrPutString

'Write file
hFile = FreeFile
Open
strPath For Binary As #hFile
Put #hFile, , strContent
Close #hFile

Exit Sub

ErrPutString:

Close #hFile
MsgBox Err.Description, vbCritical, "PutStringToFile"

End Sub

Apa kegunaan/manfaat mendefaultkan home page/site 3 browser besar di atas? Insya Allah dalam pertemuan lain kita akan membahasnya.