topical media & game development
mashup-flickr-09-Exercise-2-jumptoflickrpage.user.js / js
// ==UserScript==
// @name Jump to Flickr Page Exercise
// @namespace http://www.dopiaza.org/flickr/greasemonkey/
// @description Jump to a specific Flickr page in a paginated set of pages.
// @include http://www.flickr.com/*
// @include http://flickr.com/*
// ==/UserScript==
var paginators = document.evaluate(
"//div[@class='Paginator']",
document,
null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
null);
for (var i = 0; i < paginators.snapshotLength; i++)
{
var paginator = paginators.snapshotItem(i);
// Find the max page number
var anchors = paginator.getElementsByTagName('A');
// find the number of the last page linked
var lastPageNum = 1;
for (var j = 0; j < anchors.length; j++)
{
var children = anchors[j].childNodes;
if (children.length > 0)
{
var child = children[0];
if (child.nodeType == 3) // Text Node
{
var pageVal = child.nodeValue;
if (!isNaN(pageVal))
{
lastPageNum = Math.max(lastPageNum, parseInt(pageVal));
}
}
}
}
// Place the text box immediately before the "next" item, which is the last
// non-text element in the paginator (There is whitespace after it, which shows
// up as a text node. It may be an anchor or a span, depending on whether we are
// at the last page or not.
var paginatorChildren = paginator.childNodes;
var nextElement;
for (var j = paginatorChildren.length - 1; j >= 0; j--)
{
if (paginatorChildren[j].nodeType == 1) // Element Node
{
nextElement = paginatorChildren[j];
break;
}
}
var inputBox = document.createElement('input');
inputBox.setAttribute("type", "text");
inputBox.setAttribute("size", "3");
paginator.insertBefore(inputBox, nextElement);
inputBox.addEventListener('keypress', function(evt)
{
var keyVal = evt.which;
var key = String.fromCharCode(keyVal);
if (key == '\r')
{
var pageNum = evt.target.value;
if (pageNum <= 0 || pageNum > lastPageNum)
{
evt.target.value = "";
evt.preventDefault();
}
else
{
// So, where are we now?
var loc = window.location.href;
// Remove any existing page number
loc = loc.replace(/page\d+(\/)?
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.