Ryan
Ryan

Reputation: 15250

How can I reset Google Chrome via Applescript or shell on a Mac?

Here's what I have so far:

set chrome_clear_url to "chrome://settings/clearBrowserData"
set chrome_settings_url to "chrome:///settings"
tell application "Google Chrome" to quit
delay 1
tell application "Google Chrome" to launch
delay 1
tell application "Google Chrome" to activate
delay 2
tell application "Google Chrome" to open location chrome_clear_url
delay 2
tell application "Google Chrome"
    execute front window's active tab javascript "document.getElementById('clear-browser-data-commit').click();"
end tell
delay 2
tell application "Google Chrome" to open location chrome_settings_url
delay 2
tell application "Google Chrome"
    execute front window's active tab javascript "document.getElementById('reset-profile-settings').click()"
end tell
delay 2
return

This replies with a missing value:

execute active tab of window 1 javascript "document.getElementById('clear-browser-data-commit').click();"
    --> missing value

How can I properly load settings and reset the browser via AppleScript? I suppose I'm open to non-Applescript solutions as well provided they're efficient.

Upvotes: 2

Views: 3192

Answers (2)

mklement0
mklement0

Reputation: 437197

Your own answer is definitely the way to go here.

However, this answer explains why the JavaScript interaction didn't work; the techniques used here may come in handy in other situations.


The problem is not in your AppleScript code, but in your JavaScript code:

The problem is that the HTML source in chrome://settings/... pages uses <iframes> for the main content, so that accessing their elements directly with document.getElementById() does NOT work -- you'd instead have to target the iframe element's document object.

Unfortunately, since the URL of the <iframe> element in question, chrome://settings-frame/settings, has a different 'domain' from the enclosing page (settings vs. settings-frame), Chrome won't let you access the frame's separate document object for security reasons (same-origin policy); in other words: document.getElementsByTagName('iframe')[1].contentDocument.getElementById('clear-browser-data-commit') will NOT work.

However, you CAN simply load the <iframe> URL directly, as demonstrated in this snippet:

tell application "Google Chrome"
  open location "chrome://settings-frame/clearBrowserData" # !! The *iframe* URL!
  tell active tab of window 1 to execute javascript "
    window.addEventListener('load', function() { 
      document.getElementById('clear-browser-data-commit').click(); 
    });"
end tell

Note the use of a listener to the window object's load event, which ensure that the .click() attempt is only made once the page has fully loaded - approach courtesy of https://stackoverflow.com/a/22027436/45375

Upvotes: 3

Ryan
Ryan

Reputation: 15250

This approach is much more straightforward and is confirmed to work.

# reset Google Chrome with Applescript and Shell
tell application "Google Chrome" to quit
do shell script "rm -rf ~/Library/Application\\ Support/Google/Chrome/Default"

Upvotes: 1

Related Questions