Javascript in Chrome works in console but not from KM or Applescript

I've got a bit stuck with this so hopefully someone can point me in the right direction.

Basically I'm trying to execute a javascript in Chrome for a specific website.

The javascript works in Safari - from the Safari Dev Console, from the execute JS command in KM and also from Applescript Script Editor.

The same javascript works in Chrome from the Dev Console, but does NOT work from KM or from Script Editor.

Below is the Applescript code for bith Safari and Chrome. In Script Editor when targetting Chrome it just says "missing value", so unsurprisingly in KM it also doesn't do anything. The JS is meant to adda an item to the basket

Both require the following webpage to be open and the active tab:

tell application "Safari"
	activate
	set theJS to "Ecwid.Cart.addProduct({id: 616681326, quantity: 1, options: {'Print Size':'A4 297x210mm'},});"
	tell front document
		do JavaScript theJS
	end tell
end tell
tell application "Google Chrome"
	activate
	set theJS to "Ecwid.Cart.addProduct({id: 616681326, quantity: 1, options: {'Print Size':'A4 297x210mm'},});"
	execute front window's active tab javascript theJS
end tell

Other JS commands are working in Chrome from from KM or Script Editor so it doesn't appear to be a general issue with executing standard JS commands from KM or Script Editor

The actual javascript being executed is a site specific API Javascript function - not sure if that makes any difference when dealing with Chrome?

If anyone has any insight it would be so appreciated.

Did you ever get this resolved?
I have similar issue with a JavaScript working in the console. But when running from Keyboard Maestro it fails.

OP's issue was that a particular JavaScript worked in Safari, from the console or a KM macro, worked in the Chrome console, but didn't work in Chrome using a KM macro.

Is your issue that specific, or something else? Can you supply a macro or some JS that will demonstrate the problem? Do any "Execute JavaScript in..." actions targeting that browser work? Have they ever worked (Chromium's AppleScript support, required for the action, comes and goes with various updates)?

I never did get it to work in chrome, or work out why (JS and developer tools not my speciality at all).

My workaround for chrome was to get KM to open the Console, paste in the javascript, run it and then close the console. Not pretty, but works just fine.

Hi

Good idea just to test with a simple javascript. I will try this and found out if it is KM not able to "drive" Google Chrome.

I got it to work.

Maybe it has been something where KM changes the script or processes some tokens in my JavaScript.

This code does work in the Chrome console, but now when run from KM.

(function() {
  // Helper: set input.value via the native setter
  function setNativeValue(el, value) {
    const proto = Object.getPrototypeOf(el);
    const setter = Object.getOwnPropertyDescriptor(proto, 'value').set;
    setter.call(el, value);
  }

  // Find every variant title
  const titles = document.querySelectorAll('.ProductVariantTitleText, ._ProductVariantTitleText_c4050_13');

  titles.forEach(titleEl => {
    const text = titleEl.textContent.trim();
    const m = text.match(/(\d+)\s*[xร—]\s*(\d+)/i);
    if (!m) return;

    const w = +m[1], h = +m[2];
    const orientation = w > h ? 'Vandret' : w < h ? 'Lodret' : 'Kvadratisk';

    const row = titleEl.closest('[role="row"]');
    if (!row) return;

    // The Orientation cellโ€™s <input> is left-aligned
    const input = row.querySelector('input.Polaris-TextField__Input--alignLeft');
    if (!input) return;

    // 1) use native setter, 2) dispatch input & change
    setNativeValue(input, orientation);
    input.dispatchEvent(new Event('input',  { bubbles: true }));
    input.dispatchEvent(new Event('change', { bubbles: true }));
  });
})();

I then tried to minify it with this page https://www.toptal.com/developers/javascript-minifier and got this code, which works through KM.

!function(){let t=document.querySelectorAll(".ProductVariantTitleText, ._ProductVariantTitleText_c4050_13");t.forEach(t=>{let e=t.textContent.trim(),r=e.match(/(\d+)\s*[xร—]\s*(\d+)/i);if(!r)return;let l=+r[1],n=+r[2],i=t.closest('[role="row"]');if(!i)return;let o=i.querySelector("input.Polaris-TextField__Input--alignLeft");o&&(!function t(e,r){let l=Object.getPrototypeOf(e),n=Object.getOwnPropertyDescriptor(l,"value").set;n.call(e,r)}(o,l>n?"Vandret":l<n?"Lodret":"Kvadratisk"),o.dispatchEvent(new Event("input",{bubbles:!0})),o.dispatchEvent(new Event("change",{bubbles:!0})))})}();
1 Like

@peternlewis Is there a way to analyse the JavaScript before it was minified to see if there are something, which gets processed by KM before it gets to Google Chrome?

Is your Execute a JavaScript action configured for modern syntax? If so, you have an extra layer of wrapping.

You can see the script in the log with:

defaults write com.stairways.keyboardmaestro.engine Debug "JavaScript"

or more stuff probably with

defaults write com.stairways.keyboardmaestro.engine Debug "XTask JavaScript"

I do not seem to see ability to enable modern syntax. KM is version 11.0.3.

Click on the little arrow to the left of the text area.

1 Like

Thanks. I am so used to the options being in the gear menu.

I can then confirm the original JavaScript works, when disabling Modern Syntax.

It also works if I preprend my JavaScript with a semicolon. Then it works with Modern Syntax enabled.

Just for others, here is a screenshot where it is.

2 Likes

Incidentally, for a script like that which makes no use of Keyboard Maestro variables, you can get the best security and performance by choosing Include No Variables:

Screenshot 2025-05-06 at 2.45.40 am

actions:Execute a JavaScript in Browser [Keyboard Maestro Wiki]

1 Like