Execute jQuery in Google Chrome fails in El Capitan

I run jQuery snippets to interact with web pages. I believe updating to El Capitan has caused my scripts to fail. Here’s the message I’m getting in the Chrome console.

Uncaught ReferenceError: $ is not defined

Of course if I type the same command in the Chrome console, everything works fine. jQuery is included on the page. I restarted my computer and it still prints this error. I also tried including jquery from KM, but it’s not having any of that. Sending pure JS commands still works though.

Anyone have a guess to why this is happening?

Hey Derek,

Can you give an example of a snippet that was working and now is NOT working?

-Chris

It’s acting like jquery is not included. So even this is failing:

$(’.test’).val();

I suppose I could use standard JS, but I have a LOT of macros that rely on jquery.

Hey Derek,

How are you running that?

From an Execute JavaScript in Google Chrome action?

-Chris

Yes.
Execute JavaScript in Google Chrome > Execute text script (ignore results)

Have you tried it in a similar action in Safari to see if it works there?

Just tested. Looks like it works in Safari. Interesting…

Make sure Chrome is up to date and then quit and restart it a couple of times. (I’d probably reboot too just to be thorough.)

Retry your macro.

Sometimes Chrome’s connection to AppleScript breaks, and all KM’s Chrome actions use AppleScript.

If that doesn’t work then report the issue as a bug to Peter and provide test-cases.

Peter Lewis <support@stairways.com>

-Chris

JavaScript in the browsers is executed via AppleScript, similarly in both Safari and Chrome. For Chrome, the AppleScript looks like this:

tell application "Google Chrome"
	if it is running then
		tell window 1
			set r to execute active tab javascript "$('h1').css('color','red')"
			if r is missing value then
				set r to ""
			end if
		end tell
	else
		set r to "Not Running"
	end if
end tell
r

If you run that in Script Editor you get the Uncaught ReferenceError. based on comments, it seems this is new. I checked in both Yosemite and El Capitan and got the same results (no exception is noted in Yosemite, but it doesn’t work), so I suspect this is a new behaviour in Chrome. Whether this is some sort of security feature or some sort of bug, I don’t know. That is a question the Google Chrome folks would have to answer. But until the AppleScript code above works, or there is some sort of alternative, I’m afraid accessing jQuery this way is not going to work. As to what other workarounds there might be, I’m afraid I don’t know of any.

I will either use plain JS or switch to Safari. Thanks again for your time and for Keyboard Maestro. Big fan!

I'm having the same issue. It works on Safari, but not in Chrome.
Has anyone found a workaround other than using avoiding jQuery?
Thanks!

Having the same problem with Arc (Chromium based).

I've confirmed (via Console) that it is running and tried the AppleScript above as well - it acts the same as KM (of course).

Same JavaScript works within Safari, and it works fine in the Console.

I'll contact the dev, but I'm betting it's a Chrome/Chromium issue at heart.

What JavaScript?

Running from where? What action?

Does Arc even have this setting available? If so is it enabled?

image

Arc is still in beta – I doubt it's been added as an option to Keyboard Maestro's Front-Browser actions yet.

@peternlewis?

Yes, I enabled that in Arc.

I used this to add support to KM: Support for Arc Browser - #3 by overment

Then have KM target Arc. Using the run JavaScript action. It works, the JS gets run in Arc, it just doesn’t work because of scope or something. My point above was just that the same thing happens if I don’t use KM, so it seems to be a browser issue.

1 Like