AppleScript Get HTML of Chrome Active Tab

applescriptgoogle-chromehtml

Is there a way to get the actual HTML of a current active tab in Chrome? Some websites use the position on the page to induce an AJAX call to expand the page further. So working with a URL only won't cut it. Actually grabbing the current HTML is what I'm after.

At the moment, I can ask Chrome to open a new tab with the source of the tab, but that's a bit cumbersome. The libraries haven't shown me anything with regards to grabbing current active HTML.

Can I somehow use AppleScript to get the current active tab HTML?

Best Answer

Here is one way to grab the HTML of the active tab of the front window of Google Chrome using AppleScript:

tell application "Google Chrome" to set sourceHTML to ¬
    execute front window's active tab javascript "document.documentElement.outerHTML"

Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors.