Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Block Websites / Hosts Editor


  • Please log in to reply
7 replies to this topic
Jon
  • Members
  • 349 posts
  • Last active: Aug 30 2011 08:35 PM
  • Joined: 28 Apr 2004
The following script lets you edit the hosts file to block websites (or ads/images). Enter just the website domain name you want to block e.g. www.google.com and click "Block".

The "Delete" button will remove the selected domain name from the block list.

The "Delete All" button will delete the hosts file and create a new empty one i.e. no websites are blocked.

The "Restore" button will restore the hosts file back to before you started editing it. (a backup of the hosts file is created when the script starts).

Posted Image

;Checks if HOSTS file exists

If A_OSVersion = WIN_XP
{
;XP Home
Ifexist, C:\WINDOWS\SYSTEM32\DRIVERS\ETC\HOSTS
hostsdir=C:\WINDOWS\SYSTEM32\DRIVERS\ETC\HOSTS

;XP Pro
ifexist,  c:\winnt\system32\drivers\etc\hosts
hostsdir= c:\winnt\system32\drivers\etc\hosts
}

If A_OSVersion in WIN_2000, WIN_NT4
{
Ifexist, C:\WINNT\SYSTEM32\DRIVERS\ETC\HOSTS
hostsdir=C:\WINNT\SYSTEM32\DRIVERS\ETC\HOSTS
}

If A_OSVersion in WIN_95, WIN_98, WIN_ME
{
Ifexist,  c:\windows\HOSTS
hostsdir= c:\windows\HOSTS
}

if hostsdir=
{
msgbox, Hosts file not found or OS not supported
exitapp
}

; backup hosts file before editing

filedelete, %hostsdir%.bak

FileCopy, %hostsdir%, %hostsdir%.bak


site=

Loop, read, %hostsdir%
{
	Loop, parse, A_LoopReadLine, `n
	{
		if A_LoopField <>
		ifnotinstring, A_LoopField, #
		ifnotinstring, A_LoopField, localhost
		site=%A_LoopField%|%site%
	}
} 

stringreplace, site, site, 127.0.0.1,,all
stringreplace, site, site, %a_space%,,all

Gui, Add, Text,x26 y5 w100 h20,Enter Site to Block:
Gui, Add, Edit, vBlocksite x26 y20 w320 h20, website.com
Gui, Add, Button, gBlock x356 y20 w60 h20, Block

Gui, Add, ListBox, x16 y50 w410 h190 AltSubmit sort,%site%

Gui, Add, Button, x16 y250 w60 h20 gDelete, Delete
Gui, Add, Button, x76 y250 w60 h20 gRemove_all, Delete all
Gui, Add, Button, x136 y250 w60 h20 gRestore, Restore


Gui, Show, x404 y320 h285 w442, Hosts Editor
Return

Delete:
ControlGet, todelete, Choice, , ListBox1, Hosts Editor

Loop, read, %hostsdir%
{

line=%A_LoopReadLine%

IfInString, line, %todelete%
StringReplace, line, line,%line%,,All

file=%file%`n%line%
}

filedelete, %hostsdir%

fileappend, %file% ,%hostsdir%

GuiControlGet, Number, , ListBox1

control, Delete, %Number%, ListBox1, Hosts Editor

return

Block:
ControlGetText, Blocksite, Edit1, Hosts Editor

if Blocksite=
msgbox, No website was entered., return

stringreplace, Blocksite, Blocksite, http://,

StringRight, Slash, Blocksite, 1

if Slash=/
StringTrimRight, Blocksite, Blocksite, 1

fileappend, `n127.0.0.1        %Blocksite%, %hostsdir%

control, Add, %Blocksite%, ListBox1, Hosts Editor

msgbox, %Blocksite% has been Blocked

return

Restore:

filedelete, %hostsdir%

Loop, read, %hostsdir%.bak, %hostsdir%
{
FileAppend, %A_LoopReadLine%`n

siterestore=%A_LoopField%|%site%
}

GuiControl,, ListBox1, %siterestore%

msgbox, All changes since last edit have been removed.

return

Remove_all:
filedelete, %hostsdir%
fileappend,127.0.0.1       localhost`n,%hostsdir%

GuiControl,, ListBox1, |

msgbox, All Sites have been unblocked
return

GuiClose:
ExitApp


Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
Looks great; thanks for sharing it.

I used to use a large hosts file to block all kinds of annoying sites. Although I use something else now, I remember there was a way (via a running service of some kind) to have plain white squares substituted for all the animated ads and whatnot. The only advantage of this is that it made sites look better by avoiding squares containing link error messages.

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
Nice one.
There is always Norton Firewall lol, which works A* :p

Jon
  • Members
  • 349 posts
  • Last active: Aug 30 2011 08:35 PM
  • Joined: 28 Apr 2004
Thanks, I used to use a large host file as well, mainly for blocking ads. I did try a program that put your own image inplace of a blocked image which was good because then you knew why that image wasn't loading. Another way I could have done it was to edit the registry to add the websites to the restricted zone. That wouldn't have shown an error, just a blank page.

When I moved to firefox I used the adblock extension instead which will remove an IFrame or image and put a transparent image in it's place or removes it all together.

I made that script just to see if I could really and I thought it might be something that others would find useful.

bLisTeRinG
  • Members
  • 45 posts
  • Last active: Nov 07 2012 11:15 AM
  • Joined: 15 Nov 2004
Syncronicity in action! I just made this last week. I'll get back after
checking it out.
;;;   Hosts-file Utility v0.2 bLisTeRinG 2004
;Run, properties c:\autoexec.bat
#Persistent
#SingleInstance

;;;      Locate an ini-file -check the Command-line.
in = %A_ScriptDir%\HostUtil.ini
If %0% <> 0
  {
  iPath = %1%
  }
Else
  {
  iniRead, iPath, %in%, options, iniPath
  }
If iPath = ERROR
 iPath = %in%
IfNotExist, %iPath%
  GoSub, hoIniSet

;;;   Locate the Hosts-file.
iniRead, ho, %iPath%, options, HostsFile
oh = %ho%.off
IfNotExist, %ho%
  IfNotExist, %oh%
    {
    ho = C:\Windows\Hosts
    oh = %ho%.off
    IfNotExist, %ho%
      IfNotExist, %oh%
        {
        ho = C:\Windows\System32\Etc\Hosts
        oh = %ho%.off
        IfNotExist, %ho%
          IfNotExist, %oh%
            GoSub, hoHoSet
        }
    }

;;;   Locate an Editor.
iniRead, ed, %iPath%, options, Editor
IfNotExist, %ed%
  {
  ed = C:\Progra~1\Tools\ePad\EditPad.exe
  IfNotExist, %ed%
    GoSub, hoEdSet
  }

;;;   Check status of Hosts-file & Setup Tray-Menu.

Menu, tray, add, &Active, hoToggle
IfExist %ho%
  {
  tg = 1
  Menu, tray, Check, &Active
  }
Else
  {
  tg = 0
  Menu, tray, UnCheck, &Active
  IfNotExist %oh%
    GoSub hoHoSet
  }
Menu, tray, Default, &Active
Menu, tray, add, &Edit, hoEdit
Menu, tray, add, &Paste, hoPaste
Menu, tray, add  ;separator
Menu, tray, add, &Setup, hoSet
Menu, tray, add, &Help, hoHelp
Menu, tray, add, E&xit, hoExit
Menu, tray, NoStandard
Return

;;;   All the routines in alphabetical order.

hoChk:
IfExist %ho%
  {
  tg = 1
  Menu, tray, Check, &Active
  }
Else
  {
  tg = 0
  Menu, tray, UnCheck, &Active
  }
Return

hoEdit:
GoSub hoChk
If tg = 1
  {
  Run, %ed% %ho%
  }
Else
  {
  Run, %ed% %oh%
  }
Return

hoEdSet:
FileSelectFile, hoho, 8, *.exe, Choose Editor (%ed%)
If ErrorLevel = 1, Return
MsgBox, 4100, Choose Editor, `nOld Editor:`n%ed%`n`nNew Editor:`n%hoho%, 8
IfMsgBox, No
  {
  Return
  }
iniWrite, %hoho%, %iPath%, options, Editor
ed = %hoho%
hoho =
Return

hoExit:
ed =
ho =
hoho =
in =
iPath =
oh =
tg =
Exit

hoHelp:
GoSub hoChk
If tg = 1
  {
  hoho = %ho%
  }
Else
  {
  hoho = %oh%
  }
MsgBox, `nActive:`tToggles the Filtering On/Off.`nEdit:`tEdit the Hosts-file.`nPaste:`tNew server address(es)`n`t...to Hosts-file filter.`n`nHosts:`t%hoho%`n`nEditor:`t%ed%`n`nini-file:`t%iPath%`n
Return

hoHoSet:
FileSelectFile, hoho, 8, C:\Windows\ hosts*, Choose Hosts File (%ho%)
If ErrorLevel = 1, Return
MsgBox, 4100, Choose Hosts File, `nOld Hosts File:`n%ho%`n`nNew Hosts File:`n%hoho%, 8
IfMsgBox, No
  {
  If ed = 
    Return
  hoho = 1
  Return
  }
iniWrite, %hoho%, %iPath%, options, HostsFile
ho = %hoho%
oh = %ho%.off
hoho =
Return

hoIniSet:
FileSelectFile, hoho, 8, %A_ScriptDir%\ *.ini, Choose Initialization File (%iPath%)
If ErrorLevel = 1, Return
MsgBox, 4100, Choose Initialization File, `nOld iniFile:`n%iPath%`n`nNew iniFile:`n%hoho%, 
IfMsgBox, No
  {
  If ed = 
    Return
  hoho = 1
  Return
  }
iniWrite, %hoho%, %in%, options, iniPath
iniWrite, %hoho%, %hoho%, options, iniPath
If ed <> 0
  {
  Reload
  Sleep, 1000
  }
iPath = %hoho%
hoho =
Return

hoPaste:
GoSub hoChk
Inputbox, hoho, Clipboard, Entries should look something like this:`n   www.greedypigs.com,,280,140,,,,,%clipboard%
If ErrorLevel = 1
  {
  Return
  }
If tg = 1
  {
  FileAppend, `n0 %hoho%, %ho%
  }
Else
  {
  FileAppend, `n0 %hoho%, %oh%
  }
Return

hoSet:
GoSub hoIniSet
If hoho = 1, Return
hoho =
GoSub hoHoSet
If hoho = 1, Return
hoho =
GoSub hoEdSet
hoho =
Return

hoToggle:
GoSub hoChk
If tg = 1
  {
  FileMove, %ho%, %oh%
  tg = 0
  Menu, tray, UnCheck, &Active
  }
Else
  {
  FileMove, %oh%, %ho%
  tg = 1
  Menu, tray, Check, &Active
  }
Suspend
Return


  • Guests
  • Last active:
  • Joined: --

Looks great; thanks for sharing it.

I used to use a large hosts file to block all kinds of annoying sites. Although I use something else now, I remember there was a way (via a running service of some kind) to have plain white squares substituted for all the animated ads and whatnot. The only advantage of this is that it made sites look better by avoiding squares containing link error messages.


The program to do this is eDexter

Not only does it replace the blanks in the missing ads, it also speeds up HTTP Responses and blocking generally.

Btw, I used to use HOSTS file to do ad-blocking too; but I've shifted to Ad-Muncher now.

jonny
  • Members
  • 2951 posts
  • Last active: Feb 24 2008 04:22 AM
  • Joined: 13 Nov 2004
Adblock for Firefox works really good too; I've been satisfied with it from the beginning.

Soldier3570
  • Members
  • 10 posts
  • Last active: Sep 20 2006 09:55 AM
  • Joined: 30 Aug 2006
just so you know norton anti virus is the worst AV i have ever seen/ used.....
i used it and a trojan came through my connection and the norton told me about it , so i went to go delete it and the quarantine action was unavailable so i couldnt do anything about it....

the best Av is McAfee the latest version because it will delete a virus\ trojan and tell you, you wont get a chance to do anything because its all done by McAfee
Soldier3570