AutoHotkey Community

It is currently May 27th, 2012, 7:41 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: November 14th, 2004, 8:47 pm 
Offline

Joined: April 28th, 2004, 1:12 pm
Posts: 349
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).

Image

Code:
;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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 15th, 2004, 1:07 am 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 15th, 2004, 1:50 am 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
Nice one.
There is always Norton Firewall lol, which works A* :P


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 15th, 2004, 10:06 pm 
Offline

Joined: April 28th, 2004, 1:12 pm
Posts: 349
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.


Report this post
Top
 Profile  
Reply with quote  
PostPosted: November 28th, 2004, 11:02 am 
Offline

Joined: November 15th, 2004, 1:46 pm
Posts: 45
Location: Port Fairy
Syncronicity in action! I just made this last week. I'll get back after
checking it out.
Code:
;;;   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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 28th, 2004, 11:49 am 
Chris wrote:
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.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: November 28th, 2004, 9:01 pm 
Offline

Joined: November 13th, 2004, 4:08 am
Posts: 2951
Location: Minnesota
Adblock for Firefox works really good too; I've been satisfied with it from the beginning.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 20th, 2006, 10:42 am 
Offline

Joined: August 30th, 2006, 8:06 am
Posts: 10
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


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: nothing and 10 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group