How To Trigger Macros Automatically From Google Chrome

YEEEEEEEEESS!!!

I've been wanting this and thinking about it ever since I really started using Keyboard Maestro in earnest. I now have a way to trigger my macros from Google Chrome reliably and efficiently and I'm excited to share it with everyone. :star_struck:

I created a simple Google Chrome extension using manifest V3. The extension is able to match against URLs and page titles and then call Keyboard Maestro using Keyboard Maestro's Web Server feature.

Sample files can be found on Github and a demo video on YouTube.

Google Chrome Trigger.kmmacros (51.2 KB)

Chrome extension manifest.json

{
  "name": "Keyboard Maestro Chrome Event Emitter",
  "manifest_version": 3,
  "version": "1.0",
  "description": "Call Keyboard Maestro with Google Chrome events for automation.",
  "author": "Mike Grace",
  "permissions": [
    "tabs"
  ],
  "background": {
    "service_worker": "background.js"
  },
  "icons": {
    "128": "logo-128.png"
  }
}

Chrome extension background.js

/****************
 * 
 * Script for Keyboard Maestro to trigger macros based on loaded
 * URL or Title.
 * 
 * This requires that Keyboard Maestro's Web Server is enabled
 * and the macro being triggered has a "Public Web Trigger" 
 * trigger enabled.
 * 
 * Created by Mike Grace starting on 2022-11-18
 * 
 ***************/


/***********************
 * Listen to every tab update event and filter for loading and completed
 * events to match specific URLs and page titles.
 * ********************/
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {

    // LOADING PAGE
    //////////////////////
    if (changeInfo.status == 'loading' && /^http/.test(tab.url)) {
        
        // partial match of URL
        if (tab.url.includes('//example.com/')) {
            macro('A85C7184-CAED-4613-A7B1-1FE157A45E57'); // Google Chrome Trigger
        
        // exact match of title
        } else if (tab.title == 'Keyboard Maestro Discourse - Discussion forum for Keyboard Maestro, the powerful macro program for macOS') {
            macro('A85C7184-CAED-4613-A7B1-1FE157A45E57'); // Google Chrome Trigger
        }

    
    // DONE LOADING PAGE
    //////////////////////
    } else if (changeInfo.status == 'complete' && /^http/.test(tab.url)) {
        
        // exact match of URL
        if (tab.url == 'https://wiki.keyboardmaestro.com/Home_Page') {
            macro('A85C7184-CAED-4613-A7B1-1FE157A45E57', tab.url); // Google Chrome Trigger
        }
      
    }
});


/***********************
 * Call macro using web server trigger. Response will get blocked
 * by CORS policy and that is ok since we only care about getting
 * the call out.
 * ********************/
function macro(macroUUID, value='') {
  fetch(`http://localhost:4490/action.html?macro=${macroUUID}&value=${value}`)
    .then(function(response) {
        return response;
    }).then(function(data) {
        // do nothing
    }).catch(function() {
        // do nothing
    });
}

Cheers and happy automating! :beers:

5 Likes

What's the benefit of this over triggering via front window change and an if-else dependent on %FrontBrowserURL%?

2 Likes
  • Keeps my engine.log much cleaner which I almost always have open and use constantly to monitor running macros and debug macros I'm creating.
  • Doesn't trigger each time I switch windows and applications.
  • Only triggers once per page load instead of potentially multiple times if I switch back and forth between windows.
  • Less strain on Keyboard Maestro? Haven't measured this and haven't run into issues so don't know how much and whether it would ever even be an issue.
4 Likes

Interesting. Great to have options!

1 Like

Hey Mike,

Well, that's quite spiffy! Congratulations.

There have been a bunch of people over the years who've wanted this feature, but only a few of them will be intrepid enough to load a browser extension and manipulate web code.

:sunglasses:

-Chris

2 Likes