How to I Use KM to Create FireFox JavaScript Bookmarklet?

firefox
javascript

#1

Continuing the discussion from Firefox Execute Bookmarklet Javascript .

I cannot get this method of executing Javascript in Firefox to work at all! is there any other way that works with KM 8.2.4 and FF 65.0.1?


Firefox Execute Bookmarklet Javascript
#2

In order for us to help you, please post the actual Macro and JavaScript that you are trying to use.


#3

Well, my end purpose is to find out how to click a specific element (defined by the html ID property in Firefox. I was having no luck with running the "Execute Javascript in the Front Browser" action, as it seems to only call Chrome or Safari.

This post seemed to offer a solution using this macro, but this failed to work either.

These posts are a little stale and was wondering if KM was better able to pass javascript to Firefox via some method.

Even using the example I couldn't get a snippet of JS (alert("hello")) to function via KM even though it executed in the console. So, I'm a bit stumped.


#4

I’d start with the simplest thing that you can - just to establish you’re even executing JavaScript against the DOM i.e. in the browser.

Try the following - in increasing order of ambition:

  1. alert(“Hello world”).
  2. Do a getElementById() and then alert() its innerHTML.

If you can do both of those then we can see what is wrong with the JavaScript.

Hint: The first one is the killer because it requires you to pass the JavaScript with any encoding necessary.

“Baby steps” such as the above is what I do when trying to work in a new environment.

And, yes, a sample of the not working JavaScript would be really handy.

Let us know how you get on.

[Edit just removed a stray apostrophe; I do know the difference between “its” and “it’s”. :slight_smile: ]


#5

I don't think you will be able to do that in FF. Many of us in the forum have long looked for ways to script FF and have never found a method that reliably works for complicated scripts. So, if you need to click on a specific element in the web page, I'd suggest:

  • If you use FF, then use the KM Move or Click Mouse action based on either a found image or relative location in the window
  • Otherwise, switch to Chrome or Safari if you want to use JavaScript in Browser
    • Many of here have found Chrome to be an excellent Browser, with many useful extensions and is very scriptable. It provides a great IDE for viewing, developing, and testing JavaScript to run on a specific page.

Good luck!


#6

Not to start a browser war, but I use Firefox because of its better handling of HTML 5 canvas...

... You can directly turn it into a PNG graphic from a context menu.

I’m inclined to experiment with javascript bookmarklets.


#7

So it looks like you can't just use a javascript: URL in the Location bar. But you could have a canned Bookmarklet if you save it as a Bookmark.

I don't know if that is flexible enough for you.

(Ref: https://support.mozilla.org/en-US/questions/917723)


#8

Do you know how to create a FF Bookmark that contains the JavaScript using KM or AppleScript?


#9

No but I should imagine it’s a matter of creating a bookmark for any page, navigating to edit it and pasting in. NOT simple.

I think this is the way it’s done on iOS / Mobile Safari, too.


#10

Here's a quick hack that can execute arbitrary JavaScript in Firefox and call Keyboard Maestro to return some result when finished.

Keyboard Maestro 8.2.4 “Run JavaScript in Firefox” Macro

Run JavaScript in Firefox.kmmacros (20 KB)

It uses the default keyboard shortcuts for opening and closing the developer tools views, so if you have modified them you'll have to change those parts of this sample macro.

Your actual JavaScript would go in the text box in the middle, inside the function block. If you want to return a value to Keyboard Maestro afterwards, you can do that by triggering another macro in the JavaScript as shown in this example; put in the name of the macro you want to call and the value you want to pass through, if any (which could be built up by JavaScript etc. depending on what you're trying to achieve).

(I wish I'd thought of this technique earlier, because I think I could make good use of this myself… :slight_smile:)


#11

Thanks for sharing.
That's a very clever idea to use a KM URL trigger.
That can be used in general with JavaScript in Browser, since normally JavaScript cannot set KM variables.


#12

Thanks @DavidShepherdson! A clever technique.