Skip to content

Instantly share code, notes, and snippets.

@sh4t
Last active April 4, 2023 09:42
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sh4t/51a1face2056cbfa547a to your computer and use it in GitHub Desktop.
Save sh4t/51a1face2056cbfa547a to your computer and use it in GitHub Desktop.
clearing chrome dns cache and sockets via js
#!/bin/bash
#
# The MIT License (MIT)
# Copyright (c) 2015 Justin Shattuck <shat@f5.com>, F5 Networks, Inc.
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
echo ""
echo "Dumping Google Chrome DNS Cache"
osascript <<EOD
tell application "Google Chrome"
tell front window
set origTab to active tab
set origTabIndex to active tab index
set theTab to make new tab with properties {URL:"chrome://net-internals/#dns"}
set isLoadDone to not loading of theTab
repeat until isLoadDone
set isLoadDone to not loading of theTab
end repeat
delay 1
execute theTab javascript "g_browser.sendFlushSocketPools();g_browser.checkForUpdatedInfo(false);g_browser.sendClearHostResolverCache();"
close theTab
set active tab index to origTabIndex
-- uncomment the line below (remove -- ) if you would like previously opened tab to be reloaded.
-- reload origTab
end tell
end tell
EOD
echo "Finished dumping Google Chrome DNS Cache"
@sh4t
Copy link
Author

sh4t commented Aug 12, 2015

Use

Someone asked if it was possible to dump/flush the local dns cache within the browser using Javascript. This is one way to do it, using bash to execute applescript, but it requires the user (a) be using an Apple and (b) they are using Google Chrome. I've adopted this to FireFox as well and will release, but for now this should work.

$ chmod +x /path/to/flush_chrome_dns.sh
$ /path/to/flush_chrome_dns.sh

Next steps, which are done, but I will create a repository that has this as an application that supports numerous browsers, has command line interface as well as a .app which could be used from Alfred workflow etc. Simple enough.

@Sunxinqiang
Copy link

when i use this i got
Uncaught ReferenceError: g_browser is not defined at <anonymous>:1:17

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment