Created
February 8, 2012 11:27
-
-
Save XP1/1768302 to your computer and use it in GitHub Desktop.
Paginate Digg: Adds the previous and next pagination links to the `head` element for accessibility reasons.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Paginate Digg | |
// @version 1.01 | |
// @description Adds the previous and next pagination links to the `head` element for accessibility reasons. | |
// @author XP1 (https://github.com/XP1/) | |
// @namespace https://gist.github.com/1768302/ | |
// @include http*://digg.com/* | |
// @include http*://*.digg.com/* | |
// ==/UserScript== | |
/*jslint browser: true, vars: true, white: true, maxerr: 50, indent: 4 */ | |
(function (console, window) | |
{ | |
"use strict"; | |
var userScript = { | |
name: "Paginate Digg" | |
}; | |
var handleException = function handleException(theFunction) | |
{ | |
try | |
{ | |
theFunction(); | |
} | |
catch (exception) | |
{ | |
console.error("[" + userScript.name + "]: Exception: " + exception + "\n\nStack:\n" + exception.stack + "\n\nStacktrace:\n" + exception.stacktrace); | |
} | |
}; | |
function removeLinks() | |
{ | |
var document = window.document; | |
var head = document.head; | |
var links = head.querySelectorAll("link[rel=\"previous\"], link[rel=\"next\"]"); | |
var i = null; | |
var length = links.length; | |
for (i = 0; i < length; i += 1) | |
{ | |
var link = links[i]; | |
link.parentNode.removeChild(link); | |
} | |
} | |
function paginate() | |
{ | |
var document = window.document; | |
var head = document.head; | |
var foundPrevious = document.querySelector("a[rel=\"prev\"]"); | |
var foundNext = document.querySelector("a[rel=\"next\"]"); | |
var isPreviousFound = (foundPrevious !== null); | |
var isNextFound = (foundNext !== null); | |
if (!isPreviousFound && !isNextFound) | |
{ | |
console.error("[" + userScript.name + "]: No pagination links found."); | |
return; | |
} | |
var previous = null; | |
var next = null; | |
var fragment = document.createDocumentFragment(); | |
var isAppend = false; | |
if (isPreviousFound) | |
{ | |
previous = document.createElement("link"); | |
previous.setAttribute("rel", "previous"); | |
previous.setAttribute("href", foundPrevious.href); | |
fragment.appendChild(previous); | |
isAppend = true; | |
} | |
if (isNextFound) | |
{ | |
next = document.createElement("link"); | |
next.setAttribute("rel", "next"); | |
next.setAttribute("href", foundNext.href); | |
fragment.appendChild(next); | |
isAppend = true; | |
} | |
if (isAppend) | |
{ | |
removeLinks(head); | |
head.appendChild(fragment); | |
} | |
} | |
function initialize() | |
{ | |
var functions = [paginate]; | |
var i = null; | |
var length = functions.length; | |
for (i = 0; i < length; i += 1) | |
{ | |
handleException(functions[i]); | |
} | |
} | |
window.addEventListener("DOMContentLoaded", initialize, false); | |
}(this.console, this)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I posted this user JS in this thread:
Digg.com Fast Forward does not work:
http://my.opera.com/community/forums/topic.dml?id=1297932