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.