Skip to content

Instantly share code, notes, and snippets.

@alexeyr
Created February 21, 2010 10:26
Show Gist options
  • Save alexeyr/310250 to your computer and use it in GitHub Desktop.
Save alexeyr/310250 to your computer and use it in GitHub Desktop.
nextplease.getLink = function (curWindow, phraseMap, imageMap, phraseRegEx, searchType) {
var text;
var isInt = /^\s*\[?\s*(\d+)\s*\]?\s*$/;
var useFrames = true;
var tmpPageNumLink = null;
var firstPageNumLink = null;
var firstPageNum;
var lastPageNumLink = null;
var nextPageNumLink = null;
var prevPageNumLink = null;
var currentPageNum = 100000; // Init to arbitrarily large num
var tmpPageNum = 100000; // Init to arbitrarily large num
var isFirstNumber = true;
var greatestNum = 1;
var i;
var link;
nextplease.readPreferences();
var range = document.createRange();
if (nextplease.useSmartNext) {
if ((searchType == nextplease.NEXT_SEARCH_TYPE) && (getBrowser().canGoForward)) {
return curWindow.history.forward();
} else if ((searchType == nextplease.PREV_SEARCH_TYPE) && (getBrowser().canGoBack)) {
return curWindow.history.back();
}
}
// Look for <LINK> tags
var linktags = curWindow.document.getElementsByTagName("link");
for (i = 0; i < linktags.length; i++) {
link = linktags[i];
// Look for rel attributes for next/prev/first/last
//
if (link.rel && link.href) {
// Hack to workaround PHPBB's incorrect use of LINK tags.
if (curWindow.location.href.match("viewtopic.php")) {
// do nothing
} else if ((searchType == nextplease.NEXT_SEARCH_TYPE) && (link.rel.toLowerCase() == "next")) {
return nextplease.openLink(curWindow, link);
} else if ((searchType == nextplease.PREV_SEARCH_TYPE) && ((link.rel.toLowerCase() == "prev") || (link.rel.toLowerCase() == "previous"))) {
return nextplease.openLink(curWindow, link);
} else if ((searchType == nextplease.FIRST_SEARCH_TYPE) && ((link.rel.toLowerCase() == "start") || (link.rel.toLowerCase() == "first"))) {
return nextplease.openLink(curWindow, link);
} else if ((searchType == nextplease.LAST_SEARCH_TYPE) && ((link.rel.toLowerCase() == "end") || (link.rel.toLowerCase() == "last"))) {
return nextplease.openLink(curWindow, link);
}
}
}
// Look for <A HREF="..."> tags
var alinks = curWindow.document.links;
// Search through each link
for (i = 0; i < alinks.length; i++) {
link = alinks[i];
// Look for rel attributes for next/prev/first/last
if (link.rel) {
if ((searchType == nextplease.NEXT_SEARCH_TYPE) && (link.rel.toLowerCase() == "next")) {
return nextplease.openLink(curWindow, link);
} else if ((searchType == nextplease.PREV_SEARCH_TYPE) && ((link.rel.toLowerCase() == "prev") || (link.rel.toLowerCase() == "previous"))) {
return nextplease.openLink(curWindow, link);
} else if ((searchType == nextplease.FIRST_SEARCH_TYPE) && ((link.rel.toLowerCase() == "start") || (link.rel.toLowerCase() == "first"))) {
return nextplease.openLink(curWindow, link);
} else if ((searchType == nextplease.LAST_SEARCH_TYPE) && ((link.rel.toLowerCase() == "end") || (link.rel.toLowerCase() == "last"))) {
return nextplease.openLink(curWindow, link);
}
}
range.selectNode(link);
text = nextplease.Trim(range.toString());
// If the text is in the lookup table,
// go to the URL
if (phraseMap[text] == true) {
// alert('found phrase match');
if (link.href.indexOf("/dictionary") < 0) {
return nextplease.openLink(curWindow, link);
}
} else {
// If we don't have an exact match,
// try a regular expression test.
// alert(text);
if (phraseRegEx.test(text)) {
if (link.href.indexOf("/dictionary") < 0) {
return nextplease.openLink(curWindow, link);
}
} else if (link.title) {
if (phraseRegEx.test(link.title)) {
return nextplease.openLink(curWindow, link);
}
}
// See if there's an image tag
var imgElems = link.getElementsByTagName("img");
if (imgElems.length > 0) {
// If the image matches, go to the URL.
//alert(imgElems[0].src);
var imgtext = imgElems[0].alt;
if (!imgtext) imgtext = imgElems[0].title;
if ((imageMap[imgElems[0].src]) || (phraseMap[imgtext] == true) || (phraseRegEx.test(imgtext))) {
// alert('found image match');
return nextplease.openLink(curWindow, link);
}
}
}
var intMatches = isInt.exec(text);
if (intMatches) {
var linkPageNum = parseInt(intMatches[1]);
// If the number is greater than a nextplease.MAX_LINK_NUM
// it probably doesn't have anything to do with
// a next/prev link.
// alert(linkPageNum);
if (linkPageNum < nextplease.MAX_LINK_NUM) {
// Try to figure out what the current page and
// next/prev links are for pages that just have
// numbered links like 1 2 x 4 5.
// if (linkPageNum == 1) {
// We're seeing a number link that is smaller
// than a previous one so assume that we're
// starting a new set of number links, and
// count from the beginning.
if (linkPageNum <= tmpPageNum) {
// alert(linkPageNum);
// alert(currentPageNum);
firstPageNumLink = link;
firstPageNum = linkPageNum;
greatestNum = linkPageNum;
prevPageNumLink = null;
nextPageNumLink = null;
lastPageNumLink = null;
currentPageNum = linkPageNum;
firstPageNumLink = link;
greatestNum = linkPageNum;
lastPageNumLink = null;
//} else if (currentPageNum == linkPageNum) {
// currentPageNum++;
// prevPageNumLink = link;
} else if (tmpPageNum + 1 == linkPageNum) {
lastPageNumLink = link;
} else if (tmpPageNum + 2 == linkPageNum) {
prevPageNumLink = tmpPageNumLink;
nextPageNumLink = link;
lastPageNumLink = link;
}
tmpPageNum = linkPageNum;
tmpPageNumLink = link;
}
}
}
// next and prev are null so that means
// we have a solid block of number 3,4,5,6,...
if ((nextPageNumLink == null) && (prevPageNumLink == null)) {
// If we start with 1, we're probably on last page.
// Set prev to be lastPage
if (firstPageNum == 1) {
prevPageNumLink = lastPageNumLink;
lastPageNumLink = null;
}
// If we start with 2, we're probably on first page.
// Set next to be first page
if (firstPageNum == 2) {
nextPageNumLink = firstPageNumLink;
firstPageNumLink = null;
}
if (firstPageNum > 2) {
nextPageNumLink = firstPageNumLink;
prevPageNumLink = lastPageNumLink;
}
}
if (firstPageNumLink && lastPageNumLink && (firstPageNumLink.text == lastPageNumLink.text)) firstPageNumLink = null;
// alert(firstPageNumLink);
// alert(prevPageNumLink);
// alert(nextPageNumLink);
// alert(lastPageNumLink);
// Try to find a match using our number algorithm
if ((searchType == nextplease.NEXT_SEARCH_TYPE) && nextPageNumLink) return nextplease.openLink(curWindow, nextPageNumLink);
else if ((searchType == nextplease.PREV_SEARCH_TYPE) && prevPageNumLink) return nextplease.openLink(curWindow, prevPageNumLink);
else if ((searchType == nextplease.FIRST_SEARCH_TYPE) && firstPageNumLink) return nextplease.openLink(curWindow, firstPageNumLink);
else if ((searchType == nextplease.LAST_SEARCH_TYPE) && lastPageNumLink) return nextplease.openLink(curWindow, lastPageNumLink);
// Otherwise try looking for next/prev submit buttons
// if the user allows it.
if (nextplease.useSubmit && nextplease.getForm(phraseMap, phraseRegEx)) return true;
// See if we can increment the URL to get to next/prev/first
var galleryLink = nextplease.getGalleryNumberLink(curWindow, searchType);
// alert(galleryLink);
// alert(nextplease.imageLocationArray.length);
if (galleryLink) curWindow.open(galleryLink, "_self", "");
// None of it worked, so make a recursive call to
// nextplease.getLink on the frame windows.
var frames = curWindow.frames;
for (var j = 0; j < frames.length; j++) {
if (nextplease.getLink(frames[j], phraseMap, imageMap, phraseRegEx, searchType)) return true;
}
return false;
}
nextplease.getGalleryNumberLink = function (curWindow, searchType) {
var i;
// alert(nextplease.imageLocationArray);
var matches = nextplease.galleryUrlRegex.exec(unescape(curWindow.location.href));
var prefixUrl;
var suffixUrl;
var curNumber;
var urlNumber;
var padStr;
var linkUrl;
if (matches && (matches.length == 4)) {
prefixUrl = matches[1];
suffixUrl = matches[3];
if (searchType == nextplease.NEXT_SEARCH_TYPE) {
curNumber = parseInt(matches[2], 10);
for (i = 1; i < nextplease.MAX_GALLERY_GAP; i++) {
urlNumber = curNumber + i;
padStr = nextplease.padNumber(matches[2], urlNumber);
linkUrl = prefixUrl + padStr + suffixUrl;
if (nextplease.imageLocationMap[linkUrl] >= 0) {
return linkUrl;
}
}
urlNumber = curNumber + 1;
padStr = nextplease.padNumber(matches[2], urlNumber);
linkUrl = prefixUrl + padStr + suffixUrl;
return linkUrl;
} else if (searchType == nextplease.PREV_SEARCH_TYPE) {
curNumber = parseInt(matches[2], 10);
for (i = 1; (i < nextplease.MAX_GALLERY_GAP) && (i <= curNumber); i++) {
urlNumber = curNumber - i;
padStr = nextplease.padNumber(matches[2], urlNumber);
linkUrl = prefixUrl + padStr + suffixUrl;
if (nextplease.imageLocationMap[linkUrl] >= 0) {
return linkUrl;
}
}
urlNumber = curNumber - 1;
if (urlNumber >= 0) {
padStr = nextplease.padNumber(matches[2], urlNumber);
linkUrl = prefixUrl + padStr + suffixUrl;
return linkUrl;
}
} else if (searchType == nextplease.FIRST_SEARCH_TYPE) {
urlNumber = 1;
padStr = nextplease.padNumber(matches[2], urlNumber);
linkUrl = prefixUrl + padStr + suffixUrl;
return linkUrl;
}
}
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment