this is basically what I do to block ads. put an entry for the ad server in my hosts file (not the URL, but the domain, using ::1 for the IP address).
I use ff together with either firebug or it's built-in debugger's inspector to once it's identified, I copy the domain (either with my memory or with ctrl-c)
this may be useful for IT folk. many of the same servers are used for the same ads. I have a collection of about 100 ad servers I have blocked. I didn't supply a list since this can be a highly subjective thing. although for parents sakes (for sanity and safety of their kids), I may include a sample hosts file.
I have discovered along the way that many of the ad servers no longer use IPV4. they now use IPV6 - so instead of using
127.0.0.1 for an IP address, you instead use
::1 in general. yes, it makes a difference. I was using 127.0.0.1 and could not figure out why this one ad would not block.
I don't mind harmless ads. but I do mind ads advertising dates and everything downhill from there.
caveats - you should know this first before trying!
this is purely up to you. be careful how you do this, if you block the wrong domain, some of those ad servers also serve up js, and the main js code SOMETIMES is dependent on the ad code, and if it is, and the ad code didn't load because the domain points to your local machine, things like some e-commerce/shopping sites won't function completely or at all. so you have to test and be wary of what you do. if you use this, you are on your own.
well, maybe now it's time to tense up a bit... print this out. the basic idea here is to copy domains from <img> tags, <object> tags, <embed> tags, and optionally <a> tags and paste them into notepad++ with a ::1 and a space in front, each entry on its own line. got it? don't got it? steps 1-11 just configure your environment so you can get the job done. steps 12-26 are the actual task.
- install firefox or chrome and use as main browser (import your bookmarks form the other browsers and tell ff to continue the script repeatedly) - something that has a debugger with a tag/property inspector. I looked at
- install notepad++, make sure when you do you run it as administrator so it works right.
- [windows-logo-flag-key]-[pause/break], advanced, environment variables, System variables, pick Path, Edit..., hit the [End] key. type C:\Program Files (x86)\Notepad++; and hit [Enter]
- click OK, OK. this will make Notepad++ easily accessible from the cmd shell or from the start menu search box.
- right [agghh - win8 doesn't have the ability to right click the cmd shell and run as admin! no start menu! have to do it the hard way folks (but actually makes things easier in the long run)...]
right click on the desktop and pick new, shortcut. put in
%windir%\system32\cmd.exefor the path to the executable.
- for the working directory, put in
- click advanced, make sure run as administrator is checked
- rename the shortcut (general tab) to admin cmd shell.
- click OK. I don't fully remember corretly, but if you delete this shortcut, you may not be able to use a cmd shell again without a windows reinstall/repair/refresh and some programs may not install or uninstall right which use the cmd shell, because sometimes deleting the shortcut can delete the file (at least it's that way with folders - I don't like that, there should be a safe unlink). keep that admin shell around, it will come in handy lots of times. things run from it will not have to be run as administrator. for instance, you can do a memory speed test by doing
winsat memformalfrom the admin cmd shell or run
chkdsk c: /f /rto fix the filesystem and refresh a hard drive (/r shortens the life of ssd's and flash so don't use on those).
- right click the shortcut and pick pin to taskbar.
- click the admin cmd shell you just created on the taskbar
- type notepad++ hosts and hit [Enter]. you should already be in c:\windows\system32\drivers\etc where the hosts file is located. it is a protected file and is readonly except when accessed from an admin cmd shell.
- visit the page with the offensive ad
- pull up the debugger. most browsers it's under the tools[, advanced] menu, ctrl-shift-i to bring up the debugger, in chrome you have to additionally click the leftmost button on the dbeugger toolbar on the bottom to make the debugger a popup window.
- click on the inspector (repeatedly make sure it's selected until you have your target, because it can turn off, and you don't want to click the ad!). in chrome, it's the magnifying glass. in ff it's the box with a mouse pointer inside it.
- inspect the image or swf on a page using the inspector by hivering over it. you are looking for an <object>, <img>, <embed>, or surrounding <a> tag. you can examine that <a> tag's href= attribute to see if you want to also block where the ad leads to (where the ad links/jumps/goes to and where the ad image comes from can be 2 different things).
- when you have the offending ad in your sights, and you have a usable domain, click. this will select it and keep the code highlighted.
- select/highlight and copy the domain with ctrl-c - please NOTE: if the domain matches the main web site's domain, or if there is no domain, you must skip this ad since blocking the domain will block whole the web site (of course, if you DO want to block certain sites to protect your kids' browsing habits... also consider integrityonline.com they offer an easy-to-setup proxy filtering service, and opendns.com they offer a free and a pay filtering service that works at the router level if you need that).
- switch to notepad++. go to bottom of file (ctrl-[End]).
- hit [Enter], type
::1followed by a space. then paste (ctrl-v) in the domain. basically the entry needs to be on a new line. it needs to look something like
- switch back to browser. refresh page to get another ad (as many times as necessary, sometimes they only change every certain amount of time)
- repeat to step 13 as often as necessary.
- switch to notepad++ and hit ctrl-s to save.
- close notepad++
- close the admin cmd shell
- close the browser
- [windows-logo-flag-key]-R taskmgr [Enter] or ctrl-alt-delete to start the task manager.
- click Applications tab
- wait until the browser is not listed in the task manager. I have 60+ tabs, and this can take a minute to close.
- close the task manager.
- start up ff again. ads should be gone now. for some reason, this requires a restart. I think it cleans the browser cache.
- be sure to test by hitting F5 to refresh the page to change ads.
- if there is still a problem, correct the entry in the hosts file by repeating to step 12.
- for each extra machine in the house, repeat steps 35-36 one time for each computer:
- visit this page and download/view the hosts.txt file (this is not the same filename as the hosts file!) and do ctrl-a ctrl-c to select all and copy to clipboard.
- for the rest of the machines in the house, do steps 2-12. append the clipboard contents to what's already there by doing ctrl-[End] ctrl-v ctrl-s in notepad++ which will go to end of file, paste, and save. then close notepad++ and the admin cmd shell.
...and yes, this is automatic for me, it goes pretty fast when you know what you are doing! just takes work on the testing part, editing is easy, especially with multiple monitors. no more ugly or yucky ads! yay! PTL!
- skip steps 34-36. the list can be converted to simply a domain "blocking" list for use in your router by either not putting in the
::1or doing an alt-drag and selecting the column with the
::1's and spaces in it and hitting the [Delete] key.
- include steps 34-36. the above list can have
http://prepended to each line so it can be used in ie or other browser site-blocking list.
how the hosts file works
- entries are each on their own line and look like
- you can comment out stuff using a hash mark (mesh, pound)
#is actually a comment from the
#to the end of the line).
- hint: with notepad++, you can put in
::1followed by a space for entire columns by alt-dragging to make a column cursor, and then just typing. this also works for cut/copy/paste.
- TCP/IP looks here first when it needs to look up a domain. if it can't find the domain in the hosts file, it uses DNS (Domain Name Service) network protocol over the internet as a client. that's how this idea works. it finds your bad ad server in the hosts file, and doesn't go looking any further. it thinks the ad server is your machine, and since your machine isn't serving up the ads, HTTP basically fails to load that file.
- c:\windows\system32\drivers\etc where the hosts file is located. it is a protected file and is readonly except when accessed from an admin cmd shell.
- you can also use 127.0.0.1 instead of ::1 if you want to make your own test & development web servers by using nonexistent domains like fred (not fred.com). so an entry for that would look like
127.0.0.1 fredthis works over your local LAN since typically most home routers work with IPV4 mostly because of NAT. an IPV6 version of NAT is just an idea right now.