Savage Love Next/Previous

By Rodrigo Queiro Last update Jul 27, 2009 — Installed 20 times. Daily Installs: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

There are 2 previous versions of this script.

// ==UserScript==
// @name           Savage Love Next/Previous
// @namespace      http://userscripts.org/users/7405
// @description    Adds links to the previous and next articles from Savage Love
// @include        http://www.thestranger.com/seattle/SavageLove?oid=*
// @require        http://code.jquery.com/jquery-latest.min.js
// ==/UserScript==

var thisPage = window.location;

function showLinks(archive) {
    function getPage(link) {
        return link.href;
    }
    function getTitle(link) {
        return $(link).html();
    }

    var allDates =
        $('ul.savageArchives > li > a[href*=/seattle/SavageLove?oid=]',
          archive);

    var prevPage = "none", nextPage = "none";
    var prevTitle, nextTitle;

    for (var i=0; i<allDates.length; i++) {
        var thatPage = getPage(allDates[i]);

        if (thisPage == thatPage) {
            if (i > 0) {
                nextPage = getPage(allDates[i-1]);
                nextTitle = getTitle(allDates[i-1]);
            }
            if (i+1 < allDates.length) {
                prevPage = getPage(allDates[i+1]);
                prevTitle = getTitle(allDates[i+1]);
            }
        }
    }

    var prevLink = "", nextLink = "";
    if (prevPage != "none") {
        prevLink = "<a href=\"" + prevPage + "\">&laquo; " + prevTitle + "</a>";
    }
    if (nextPage != "none") {
        nextLink = "<a href=\"" + nextPage + "\">" + nextTitle + " &raquo;</a>";
    }

    $('#savageTextWrap', document).append(
      '<div>'
    + '  <div align=left width=50% style=float:left>' + prevLink + '</div>'
    + '  <div align=right width=50%>' + nextLink + '</div>'
    + '</div>');
}

$(document).ready(function(){
    $(document.body).append('<iframe id="SLNPArchive" style="display:none;"/>');
    $('iframe#SLNPArchive').attr('src',
        'http://www.thestranger.com/seattle/SavageLove?archives=all');
    $('iframe#SLNPArchive').load(function(){
        showLinks($('iframe#SLNPArchive')[0].contentDocument);
    });
});