(*
Bypass DNS in current tab © RickoKid 2008
Version 0.1
This script was written to get around the annoying way that Safari
gives up very easily when waiting for a DNS lookup, but command
line DNS lookups still work fine.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*)
tell application "Safari"
set webURL to ""
set webPath to "/"
set currentTab to current tab of window 1
if (name of currentTab is not "Untitled") then
set webURL to URL of currentTab
else
set failedWin to text of currentTab
if failedWin contains "Safari can't open the page" then ¬
set webURL to text ((offset of """ in failedWin) + 1) thru ((offset of """ in failedWin) - 1) of failedWin
end if
if webURL is equal to "" then set webURL to text returned of (display dialog "Website to visit (bypassing DNS)" default answer webURL)
set webServer to webURL
if webURL contains "//" then set webServer to text ((offset of "//" in webURL) + 2) thru -1 of webURL
if webServer contains "/" then
set webServer to text 1 thru ((offset of "/" in webServer) - 1) of webServer
set webPath to text ((offset of webServer in webURL) + (length of webServer)) thru -1 of webURL
end if
set websiteIP to my nslookup(webServer)
set newURL to ("http://" & websiteIP & webPath) as string
log "webServer:" & webServer
log "webPath:" & webPath
log "websiteIP:" & websiteIP
log "newURL:" & newURL
set URL of currentTab to newURL
end tell
on nslookup(address)
set rawResult to (do shell script "host " & address & "| head -n1")
set AppleScript's text item delimiters to space
set parts to text items of rawResult
return last item of parts
end nslookup