Extract text from a website

Hi!

This topic came up a few times here but I can't get my macro to work. Assume I've got a webpage with the following code in Chrome/Brave,Whatever:

<div class="buy-config__title">
<span class="js-buy-config-select-color buy-config-select-color sr-only">
Example
</span>
<strong class="style-no">
<span class="js-buy-config-color-code">
EXPL
</span>
<span>
Model XXX
</span>
</strong>
</div>

Now I want to extract the value of 'js-buy-config-color-code'. This is my macro:

But I can't get it to work. Nothing happens, not even an error. (I had JS errors before in the console - "Allow JavaScript from Apple Events" is activated). I'd appreciate any help. Thank you.

Tried it with a dot right before the selector [document.querySelector('.span.js-buy-config-color-code').innerText;], and now at least I get an error message:

Uncaught TypeError: Cannot read properties of null (reading 'innerText')
at :37:57
at :38:4

Thank you! This is certainly an interesting idea. But does it work just as quickly? This is an action I'd need hundreds of times a day so I'm looking for an efficient way to solve this problem. I'm wondering why my query does not work. I might have an error in the syntax but tried all the examples I could find (with 'value' instead of 'innerText', or even just the trainling dot e.g.).

I'm also thinking about using a command line tool like Cheerio or BeautifulSoup for this. But since the page is already loaded into the browser, I'd prefer to go this way.

According to ChatGPT, this should work:

document.querySelector('.js-buy-config-color-code')

-rob.

Great idea to ask ChatGPT! I often do this for RegEx but it didn't came to my mind in this occasion. It can be really helpful. And what can I say - this is right! document.querySelector('.js-buy-config-color-code').innerText returns the text inside of the element when entered in Chrome Console. And so does 'span.js-buy-config-color-code':

But (a very big BUT): the macro returns nothing! I do explicitely ask for Chromium, but tried that in Safari as well. The result is the same: nothing happens, and I can clearly confirm that by watching the console.

That's because your JavaScript should read

return document.query.....

Assuming you're using the most recent version of KM.

1 Like

Have you checked your settings behind the chevron to the left of the JS source text field ?

If Modern syntax is selected (the default) then you are returning no value, and need to prefix your document.querySelector expression with return.

See Modern Syntax in the Keyboard Maestro wiki.

1 Like

Hah! Of course I didn't check these settings! It is that simple! Now it works - with both, modern and not so modern syntax. I read about the changes concerning the syntax but somehow didn't dive deeper. Thank you all, thank you very much! I think I looked for the Syntax thing in the preferences menu on the right and didn't find it there, and then I quickly forgot about it. But that explains why I stumbled upon a JS error in Chrome, it was complaining about a closing bracket at some point which I didn't write and couldn't explain.

This is a great function. Too bad it doesn't work with Firefox. But that could make things a lot easier for my work, so I'll probably switch browsers for that.