Safari Link Sniffing

September 9th, 2005

Keeping with the Javascript theme of my last entry, I’d like to share one of the ways I’ve used Safari’s AppleScript accessible javascript model to improve the quality of my web browsing experience.

It occurred to me one day while reading some long article on Salon.com that something had been bothering me for a long time. The way that web sites often force you to split up your reading into several pages, and the fact that almost every web site seems to have a different link name, location, etc., for performing this simple and extremely frequent task.

Why isn’t there a standard way to “go to the next page” in a web browser? We have forward and backward, but we have no way to get to the forward part of that equation unless we’ve already been there! Useless!

I decided to try my hand at automatically detecting the location of the “Next” button, and clicking it on the user’s behalf. Why should I, a human being with precious little time on this planet, have to waste huge chunks of it hunting around for links named “>>” or “Next >” or “Page “?

My solution hinges on the fact that javascript exposes a given web document’s links in the form of a “document.links” array. Not familiar with Javascript? Try typing the following into your browser’s address bar and press return:

javascript:alert(document.links[0]);

A dialog should appear showing you the location of the first link on this page. OK, good. But that’s useless for my purposes. I need to know what the link looks like to the user. Does it look like a “next” link? Try this javascript command next:

javascript:alert(document.links[0].innerText);

This bad boy will pop up the text as it appears (more or less) to the user. Great! So all I have to do is iterate through *all* the links on the page, look at their innerText, and if it looks like “Next” or any of the bajillion other variations on “go forward” that are currently in use on the web, then I’ll follow the link.

This approach basically works, and after collecting a number of examples from around the web, I came up with a pretty good, general purpose script for doing exactly what I wanted.

I shared the script through MacScripter.net and have found quite a few satisfied users who have also contributed by pointing me to sites that were not previously supported. The script now looks not only for links with names like “Next >” but also for graphical links whose files are named “next.gif”, etc. The result is a script, free for your use, which can take the tedium out of “going to the next page” for lots of the sites you access in Safari. It works best with a keyboard shortcut. For instance, I set mine up to trigger when I am in Safari and hit “cmd-right-arrow.” You can download Safari Next Page from the Red Sweater Scripts page. In addition to long stories on the web, it’s also great for search engines that return dozens of pages that you need to cruise through quickly.

If you use it, let me know when you find sites that can’t be tricked by it. I will do my best to add support in a general way that fixes it for the site in question and hopefully many others.

Update: Following a clue posted by Faried below, I found a rather more extensive version of this project for the Firefox browser. There are two similar projects both worked on by the same folks: Link Toolbar and LinkIt. Unfortunately, the Link Toolbar seems firmly entrenched in MPL/GPL/LGPL licensing restrictions. I can’t tell yet whether the LinkIt accumulated wisdom could be shared or not. I am impressed by the fact that they’re using regular expressions to do the comparisons. Much slicker than my brute-force method.

6 Responses to “Safari Link Sniffing”

  1. Faried Nawaz Says:

    Site authors can use link tags in their html to let browsers know what the next/prev links are. It’s <link rel=”previous” href=”blah”> and similarly for rel=”next”.

    I believe there are at least two Firefox extensions that add next/prev/up navigation links based on the link tags (one seems to guess, like your code does). Firefox even seems to prefetch the “next” link.

  2. Daniel Jalkut Says:

    Thanks, Faried. I wasn’t aware that somebody else was working on something like this. I should definitely look into those FF extensions.

    I’d also like to expand the functionality to include “go back” but it has been much less pressing than the next function.

  3. Corey Peterson Says:

    It might be worth checking the url of the linked page – in many cases it will be the same, with an appended number after it. Won’t catch everything, but it may help

  4. Daniel Jalkut Says:

    Hi Corey – I do something like that for Salon.com, actually. Instead of “Next” links they just have a list of numbered links at the bottom of the page. So I parse the current URL to figure out the index, and then search for a link with that number.

    Once I’ve found the linked page’s URL, I don’t think there’s anything useful to do with it but go there. :)

  5. Blake Seely Says:

    Most sites have a “printer friendly” version of the article that displays everything in a single, long page – which I would prefer even over auto-moving between pages. How about an applescript that scans a page and takes you to the printer-friendly, single-page version if it’s available? :)

  6. Daniel Jalkut Says:

    Good idea, Blake. I think I would find that useful, too. I will look into it!

Comments are Closed.

Follow the Conversation

Stay up-to-date by subscribing to the Comments RSS Feed for this entry.