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.
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!