Extracting Article Text To Popup Window?

excuse my noob ness, I've been scouring the net for the past 3 hours trying to find some jumping off points to cobble this together its no avail.

I have an Alfred action that will open the current songs lyric page from genius.com. i'd like to copy the reader view of the site that just shows the lyrics nice and clean, and copying that to a popup window that stays on top from KM.

I realize there are probably many ways to get the result. I've thought of using a bookmarklet that converts the article/lyric to text to display that, i've looked into ways to AppleScript the conversion of the html page to Reader view. Before I continue, I've got all these possible avenues but not sure what's the best way. In my noob mindset, I may be making it more complicated.

I'm not looking for a handout, i won't complain though, and it would be appreciated :wink: hopefully it makes sense what i'm trying to do. and would greatly appreciate any guidance/ideas/work arounds, etc.

thanks in advance

Hey @kleerkoat,

Please provide a specific example page from genius.com, so we have something to test with.

-Chris

hello ccstone, thanks for the reply.

Here's a random track.

when i click reader view i get exactly what i want visually, i would just prefer for it to be on a floating window.

Hey @kleerkoat,

Okay, the simplest way to do this is to leverage Safari:

Load Lyrics from Genius.com into KM Floating Window.kmmacros (8.3 KB)

I've used a fixed URL in this macro, but I'm sure you'll want to do something else.

Start with this and report back if there are things you want to change and don't know how.

-Chris

1 Like

I had a go at this as well, with the hope of removing Safari from the equation:

Genius.com Lyrics.kmmacros (19.0 KB)

This attempts to retrieve the page HTML. It uses the page found at /amp/..., as there's a lot less junk to sieve out. The macro uses a regular expression match to isolate the portion of the HTML that contains the song lyrics. This portion is, itself, HTML, so I stuck that into a temporary file and displayed it using a custom HTML prompt, which the AppleScript at the bottom resizes to the full length of the screen, and positions on the left-hand-side of the screen as a floating modal window.

You can see I tried to use some CSS to change the font of the lyrics away from Times New Roman into something sans serif, but that hasn't been successful. I'll let you play around with that and see what you discover.

1 Like

Oh wow, this is awesome guys! I'm speechless. :wink:

Thanks for the 2 different approaches. There are some little tweaks I'll make to them but these are spot on. Now to try and get the url from the front most tab and I'm gold, I'll see if I can figure that out.

Much thanks and a big high 5!

Hey @kleerkoat,

There's a text-token for that.

image

-Chris

Hey @CJK,

This macro is completely failing to produce a custom prompt for me on macOS 10.12.6 with Keyboard Maestro 8.2.4, and I'm not seeing any reason why offhand...

-Chris

Hey @kleerkoat,

If you're already in Safari on the lyrics page then there's really no reason to re-download the source of the page.

It can be grabbed from Safari directly.

And you can grab just the lyrics with JavaScript, since they are defined as a class.

By using styled-text in the display-text action you affect the displayed font and font-size.

-Chris


Genius Lyrics -- JavaScript Version.kmmacros (5.6 KB)

1 Like

:open_mouth: Hm, that is strange. My relevant stats: System info: AppleScript version: 2.7 System version: 10.13.6 Keyboard Maestro version: 8.2.4

Perhaps it's best that he sticks to your macro in that case until the problem can be diagnosed (if I can reproduce it, which, currently, I cannot).

Eyeballing it, one thing to try could be to disable the action that deletes the temporary file. It's possible that your system runs faster than mine (a measly MacBook 12"), so deletes the file before it's had a chance to load in the HTML prompt. Just a guess, though.

1 Like

this is by far the fastest. thank you

1 Like

I was mulling over various methods in my head before deciding on the one that, sadly, isn't working for you. But, the other one I was querying but couldn't find any solid documentation for online on how to implement it, was the possibility of using AppleScriptObjC to retrieve information about the HTML DOM of that page, without having to open the web browser. Then, from there, applying JavaScript to extract the relevant elements much like you've done here with Safari.

Do you know if something like that is possible ? I sat around reading Apple's WebKit docs for ages, and came across a lot of very useful-sounding DOM methods, but which are all annoyingly deprecated.

And obviously, we can download the HTML source using an NSURLSession method or something similar, but that wouldn't give us the object model data we'd need in order to navigate it.

Hey @CJK,

I thought I'd tried that...

But testing again proves it was the problem.

Thanks.

-Chris

Hey @CJK,

I'd say it should be.

Here's an example of creating a webarchive from remote content:

Create a webarchive file of a given web page (search disabled).scptd.zip (15.2 KB)

I think this might be the same basic code:

Create Web Archives (Download Web Page) from a List of URLs

A related Unix executable:

Download a complete webpage without opening

These don't get us into the DOM, but I expect Shane can explain how to do that.

-Chris

1 Like