Open & WebScrape Chrome Tab in Background [Example]
HOW TO USE
First, make sure you have followed instructions in the Macro Setup below.
See the below "How to Use" Comment Action
This macro is just an example written in response to a user's request. You will need to use as an example and/or change to meet your workflow automation needs.
MACRO SETUP
Carefully review the Release Notes and the Macro Actions
Make sure you understand what the Macro will do.
You are responsible for running the Macro, not me. ??
. Make These Changes to this Macro
Assign a Trigger to this Macro .
Move this macro to a Macro Group that is only Active when you need this Macro.
ENABLE this Macro, and the Macro Group it is in.
For more info, see KM Wiki article on Macro Activation
.
REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:
(all shown in the magenta color)
CHANGE to URL that you want to open and webscrape
DESC
CHANGE to JavaScript to WebScrape Above Web Page
AppleScript uses a 2 sec pause after URL is opened.
Based on my limited understand of the AppleScript, what the Macro does is: open a new tab as active tab (foreground), do the web scrape, then switch active tab to the original active tab. So in essence, the web scrape part is still in active tab, not in the background.
Nyet. Scraping takes place in the new tab, and this is documented in the script:
tell newTab
--- Set URL of New Tab ---
set jsResults to execute javascript jsCmdOpenURLStr
delay 2.0 -- would be better to do a pause util web page element is loaded
--- WebScrape This New Web Page ---
set jsResults to execute javascript jsCmdWebScrapeStr
end tell
Did you run the macro?
Did it not work correctly? It worked as expected for me.
Yes I did run the Macro, the scraping takes place in the new tab, but it is scraping when the new tab is the active tab. The restore statement is after scraping is done.
By "background", what I meant is the new tab should never be the active tab. From my understanding, KM runs javascript at the active tab of the front window. If this is the case, then I think I have to look for something else for my use case (like headless chrome etc).
This statement actually opens new tab as active tab. I put 'delay 5' after this statement and it does stay in the new tab for 5 seconds then switches back to origTab. Without delay statement, Chrome just switched back to the origTab so quick that our eyes don't even notice it.
I looked into the " Open & WebScrape Chrome Tab in Background [Example] Macro (v9.2)" and it appears to save the current front browser tab, then moves the tab that you want to execute the JS in to front, executes the JS then restores the original tab to the front. It is supposed to happen so fast you don't notice it. On my machine the tab switch is very noticeable so this approach will not work for me. Maybe I need a faster machine.
Google Chrome will not allow you to create windows or tabs in the background for security reasons.
When you create a new window or tab Chrome automatically goes to it, and there's no way I know of using either AppleScript or JavaScript to get around this.
I have been using AppleScript to execute a Javascript in a Safari background tab and it has been working well for me. I created a generic example macro of this that scrapes the new topic count from the KBM forum page at: Keyboard Maestro Discourse
In order this macro to work, you need to have a tab at this URL https://forum.keyboardmaestro.com/latest up, but it does not need to be the frontmost window. When you run the macro it will display the number of new forum messages.
In my application I use this method in several areas so I packaged the Applescript into a subroutine and then pass the Javascript in as a variable and return the JS results so I can parse the web scrape result.
I should have thought to mention that you could simply keep a singleton document or a tab in a document open in the background and use it at will without switching to it.