10 Replies Latest reply: Feb 18, 2008 11:10 PM by Tim Semic
Tim Semic Level 1 (130 points)
I added an entry to the /etc/hosts file, setting a domain to in order to block a domain. However the edit did not seem to have an effect.

What gives?

os = Tiger

Safari is bombing my console log with:
Unsafe JavaScript attempt to access frame with URL

Mac OS X (10.4.11)
  • red_menace Level 6 (15,000 points)
    What did you edit the file with? You need to use a plain text editor that uses Unix line endings (vim, pico, BBEdit, TextWrangler, etc). You can also use the NetInfo Manager to add entries.
  • Gnarlodious Level 4 (3,220 points)
    You may need to reboot for the changes to take effect. It's happened to me. Another possibility is to flush the DNS cache. I actually have this alias in my profile that opens the hosts file and flushes the cache:

    alias hosts='sudo dscacheutil -flushcache;sudo nano /private/etc/hosts'
  • Tim Semic Level 1 (130 points)
    I used vi as root to edit.

    I read that you have to change NetInfo's search order, moving the hosts file up in NetInfo's search order in order to have the hosts file block a domain, but they didn't specify which Mac OS version they were referring to, and I thought it better to practice on a test Mac (next week) instead of my MacBook for fear of corrupting the boot cycle. Does that sound necessary, to change the NetInfo search order? Which btw I don't know how to do. I know, rt*m.

    If Firefox is not throwing those errors into the console log, does that mean it's a bug in Safari, or would that mean that Firefox is simply not logging that particular error to the console? Safari is being more verbose in debugging?

    Thanks for your knowledge.
  • red_menace Level 6 (15,000 points)
    Not sure how verbose Safari is, you might try looking at the Activity window to see if you get the JavaScript errors in there also.

    To change the lookup order (see http://www.macwrite.com/criticalmass/mac-os-x-hosts-regrouped-pf.php - this is an older article, but still works for Tiger):

    [1] From the Terminal, check the current configuration
    lookupd -configuration

    [2] If the 'Host Configuration' does not have DNS following FF and NI, then change the configuration file:
    cd /etc
    sudo mkdir lookupd (make a new /etc/lookupd/ if it doesn't already exist))
    cd lookupd

    ls (check for files, just in case the directory already existed)
    cp hosts hosts.original (save the original, if needed)

    sudo echo LookupOrder Cache NI FF DNS DS > hosts (make a new host configuration lookup order)

    [3] Restart for any changes to take effect
  • Tim Semic Level 1 (130 points)
    Thanks for all the help. Red, the instructions you provided allowed me to configure NetInfo Manager to block the domains I was interested in. I used the managed hosts file from accs-net.com as well as adding several domains to it. It proved to be very interesting to load commercial web sites and see the results.

    I believe it to be proper to mention here that many sites are solely supported by ad revenue, so when using the knowledge found here you likely would be doing a disservice to web enterprises who are providing you a free service. It is worth thinking about.

    Myself I was interested in discovering if blocking domains would eliminate error messages that were getting generated by Safari. I am sad to say that this did not seem to help that problem. The site I am having problems with is myspace.com. Load it and check your console log.

    Google "Unsafe JavaScript attempt to access frame with URL" for more info on that issue.

    Thank you very much for your help. A very nice tidbit indeed.

  • red_menace Level 6 (15,000 points)
    I use the hosts file from this site, with the personal webserver configured to show me when something is getting blocked. I realize that some sites depend on ad revenue, but the hosts file usually doesn't block ads that are set in the page source, mostly those that are passed on to some ad server somewhere. If it isn't closely related to what I'm looking at, I am not interested in it anyway.

    If the site just has some non-standard (or just crappy) JavaScript, the hosts file probably won't help any. I don't think Safari can block individual JavaScripts, but maybe FIreFox has a plugin along those lines.
  • Tim Semic Level 1 (130 points)
    Yea, I hear ya, but I thought I'd put in the disclaimer for karma's sake.

    "with the personal webserver configured to show me when something is getting blocked"

    Can you tell me a little more about that, it sounds interesting.
  • red_menace Level 6 (15,000 points)
    Here is what I use:

    Setting up the webserver, if desired (see http://macmerc.com/sections.php?op=viewarticle&artid=83 ):

    [1] Edit the text file at /etc/httpd/httpd.conf (save the original somewhere)
    - look for the line "#ErrorDocument 404 /missing.html"
    - remove the comment character "#" and save the new file

    [2] Add the file missing.html to /Library/Webserver/Documents/
    - this can be anything, such as a blank HTML document
    - for feedback, use something like <i><strike>  ad blocked via hosts  </strike></i> or a small graphic

    [3] Turn on your personal webserver (System Preferences > Sharing)
    - note that this will also allow others to access your /Users/username/Sites folder if your router forwards to it.
  • Tim Semic Level 1 (130 points)
    Red, I got the 404 working with the strikethrough.

    Gnarlodious, I messed with the apple image a bit but then it started doing some funky stuff, like it got very big or stretched out at times depending on the site. I admit I was rushing a bit so I will have to spend some more time on it. When it was working correctly it was pretty cool.

    Thanks for sharing that stuff, it's been a pretty fun weekend messing with all that.