Jump to content

Open all links in selection from a web page


Recommended Posts

I am trying to solve the follwoing:

  1. Select some text in a browser window.
  2. The selection contains links.
  3. Extract all links from the selection and open each of them in a nw browser tab.

The use case is being able to open all new posts in a forum or something similar in one go.

 

Once I have a set of URLs I guess the rest is trivial. But how do I get the markup from which to extract the URLs? When I do a regular MacOS selection I still have rich text containing stuff like links. When assigning this to a hotkey in an Alfred workflow the meta data is lost, and I have only the plain text which is of course not what I want.

 

Ideally I would even like to use a rectangular selection rather than the OS selection because that would allow me to limit the region from which to extract the URLs more precisely. But I haven't seen any custom selection in the workflow toolbox.

 

Anyone got an idea how I could approach this?

Edited by mbert
Link to comment

If you want to do the link extraction in Alfred, you'll have to implement your own "get current selection", as Alfred will not give you rich text or HTML.

 

I can point you at the code to for that if you like, but I think injecting JavaScript into the webpage is probably a better idea. Chrome and Safari have APIs for that.

Link to comment

Thank you. I probably tied my thinking too much to Alfred's APIs. Using a combination of AppleScript and shell scripting it is actually quite easy. The following shell script does it (and can also be used outside of Alfred):

 

#!/bin/sh

extractUrls() {
	cat | \
	tr '>' '\n' | \
	grep -i 'href=' | \
	tr "'" '"' | \
	perl -pe 's/.*href=["]+([^"]+).*/$1/' | \
	sort | \
    uniq
}

doHtml() {
	osascript -e 'the clipboard as «class HTML»' | \
	perl -ne 'print chr foreach unpack("C*",pack("H*",substr($_,11,-3)))' | \
	extractUrls
}

doRtfd() {
	osascript -e 'the clipboard as «class rtfd»' | \
	perl -ne 'print chr foreach unpack("C*",pack("H*",substr($_,11,-3)))' | \
	textutil -stdin -stdout -convert html -format rtfd | \
	extractUrls
}

CLASSES="`osascript -e 'the clipboard as record' | \
	tr '«' '\n' | \
	grep 'class.*»' | \
	perl -pe 's/^class +([^»]+)».*/$1/'`"

#echo "classes: $CLASSES"
URLS=""

for class in $CLASSES; do
	case $class in
		HTML) URLS="`doHtml`"; break;;
		rtfd) URLS="`doRtfd`"; break;;
	esac
done

for url in $URLS; do
#	echo "$url"
	open "$url"
done

 

I have implemented support for two classes that can be in the clipboard

  • HTML - this is what my browser gives me.
  • rftd - from TextEdit.app

Support for additional classes will be easy to add. In the end I simply run the 'open' command on all URLs. In my browser this will open all URLs in separate tabs.

 

Maybe there's a more "alfredish" way of doing this, e.g. by triggering a loop of actions that are way more customisable, like using particular browsers etc.?

 

Edited by mbert
Link to comment
10 minutes ago, mbert said:

HTML - this is what my browser gives me.


Firefox? Everything else uses RTF, AFAIK.

 

10 minutes ago, mbert said:

triggering a loop of actions that are way more customisable, like using particular browsers etc.?

 


Alfred doesn't support loops. That has to be done in code.

 

The way you're doing it is the usual way.

Edited by deanishe
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...