Execute javascript on web page in background

I am trying to execute a javascript to do some web page scraping. I can accomplish this using the "execute Javascript in Safari" action as long as the page I want to scrape is frontmost. I'd like to be able to execute a JS script on a Safari tab that has a URL that contains a specific string even if that tab is not the frontmost tab.

Here are examples of what I've tried using the JS command "document.title" as simple command to test with. The web scraping that I want to do is a bit more complex.

document title test (JS).kmmacros (2.8 KB)

This returns the title of the frontmost Safari page. Is there a way to get this action to target another browser tab?

It seems like AppleScript might be able to solve this problem but I'm no expert and am looking for help here. In this example I pass in the JS script to execute as an KM variable, then search until it finds a Safari tab whose URL contains "keyboardmaestro". This example AS fails with the error "missing value". It also brings the select Safari window to the front via the "set index of oWin to 1" statement which is not not really what I want since I want to execute it on a background Safari tab. How can I target the "do javascript" command to a specific Safari tab that is not frontmost?

document title test (AS).kmmacros (2.7 KB)

Thanks in advance for any help.


You might want to take a look at this

It's for Chrome but you might get something useful out of it.

Thanks. I saw that post when I first started looking into this but figured there must be a way to do this with Safari. I currently have a bunch of macros geared towards Safari that deal with the web pages I want to scrape. I'll revisit this post and see if I can glean anything for Safari. Worst case is I'll rework my scripts for Chrome.