Jump to content

Script for blocking certain websites

Recommended Posts

Hey, I've made (or tried to make) a script that would block a user-input website for user-input X amount of time.

I was out with my friends yesterday and I asked them if they had any ideas for what I should code. They told me that when they're drunk they and go on Facebook, they usually post something that they wished they didn't. Therefore they wanted me to make a script that would block facebook for X amount of time.

So I guess my problem is that the method I'm using isn't working. The hosts file found at: "C:\WINDOWS\system32\drivers\etc\hosts" can't be replaced without Admin rights. I've tried to run this from within Scite, and I've also tried to compile the script to a ".exe" and running it as admin. It still didn't help. Any ideas?


;Script for being unable to access user-chosen websites for X amount of time.
;To do:
; * Find a way to overwrite the real hosts file (Maybe run as admin)
; * Make an "Unblock" script if someone (or the script) accidentally does something it shouldn't

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Website Blocker", 287, 125, 192, 124)
$Website = GUICtrlCreateLabel("Website to block:", 8, 16, 87, 17)
$Time = GUICtrlCreateLabel("Amount of time", 8, 48, 74, 17)
$Input1 = GUICtrlCreateInput("www.facebook.com", 104, 16, 177, 21)
GUICtrlSetTip(-1, "This is where you input the website, that you want to block the access to.")
$Input2 = GUICtrlCreateInput("1", 104, 48, 49, 21)
GUICtrlSetTip(-1, "This is where you input the X amount of time, you would like to block the aforementioned website for.")
$Combo1 = GUICtrlCreateCombo("Hour(s)", 176, 48, 57, 25)
GUICtrlSetData(-1, "Minute(s)|Second(s)")
GUICtrlSetTip(-1, "This is where you choose if you want the amount of time to be in seconds, minutes or hours.")
$Button1 = GUICtrlCreateButton("Block website!", 104, 88, 89, 25)
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
   Case $Button1

Func Block()
   FileCopy("C:\WINDOWS\system32\drivers\etc\hosts", @Scriptdir & "\hostsbackup")
   FileCopy("C:\WINDOWS\system32\drivers\etc\hosts", @Scriptdir & "\hosts")
   FileWrite(@ScriptDir & "\hosts", @CRLF & "    " & GUICtrlRead($Input1))
   $write = FileCopy(@ScriptDir & "\hosts", "C:\WINDOWS\system32\drivers\etc\hosts", 1)
   If $write = 0 Then
   MsgBox(64, "Error", "An error has occured with blocking " & GuiCtrlRead($Input1), 5)

   If GuiCtrlRead($Combo1) = "Hour(s)" Then
   Dim $Sleep = GuiCtrlRead($Input2) * 60 * 60
   ElseIf GuiCtrlRead($Combo1) = "Minute(s)" Then
   Dim $Sleep = GuiCtrlRead($Input2) * 60
   ElseIf GuiCtrlRead($Combo1) = "Second(s)" Then
   Dim $Sleep = GuiCtrlRead($Input2)
   For $s = $Sleep To 0 Step -1
  $hours = int($Tseconds/3600)
  $Remsecs = $Tseconds - ($hours * 3600)
  $minutes = int($Remsecs / 60)
  $Seconds = $Remsecs - ($minutes * 60)
  $TT = ToolTip(GUICtrlRead($Input1) & " will be unblocked in " & $hours & " hour(s), " & $minutes & " minute(s) and " & $Seconds & " second(s).", 5, 5, "Countdown until block removed" & @CRLF & "This script has been written by Reinn. Educational purposes only, all rights reserved - 2012.", 1)

   Dim $TT = ToolTip("")
   $Copy = FileCopy(@ScriptDir & "\hostsbackup", "C:\WINDOWS\system32\drivers\etc\hosts", 1)
   If $Copy = @error Then
   MsgBox(64, "Error", "An error has occured with unblocking " & GuiCtrlRead($Input1), 5)
   FileDelete(@ScriptDir & "\hosts")
   MsgBox(64, "Unblocked", "The website: " & GuiCtrlRead($Input1) & " has been unblocked and is now accesible.", 5)
Link to post
Share on other sites

*shrug* Ok. Your example of drunk friends is great ... but I doubt they'd have the mental capacity to run this script while drunk.

For the issue you're having though ... look at #requireadmin in the helpfile.

Thanks, I got it to work. Just added "#requireadmin" at the top of the script. Have a nice day :-)

Link to post
Share on other sites

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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...