Hey folks,
Here's a script that prefetches content from links on a page. The point is to help speed up load times of pages that you may be potentially visiting. I whipped this one up pretty quickly, so there's quirks in it right now.
The way it works is, I load all
href attributes of anchor tags into separate iframes and then on the load of the iframe, I delete it It seems to work OK, but I already noticed a quirk. Links that are downloads sometimes pop up a Save dialog. As well, I currently skip loading links from iframes and frames just because I don't want to slow down the browser too much.
I think the FireFox team is working on a
page prefetcher for the browser, but for now my script is really just to tinker around a bit.
So to use the script, first, you need to be using Firefox. Second, you need to install Greasemonkey. Once you have all that, download my script, Page Prefetcher.
That's it. Presto!
I recently minified the code for the script using
YUI Compressor, so if you want to see the original code as of January 20th, 2009 check it out below. I formatted some of the lines which is why you see some strings broken up when it's not necessary.
// ==UserScript==
// @name Page Prefetcher
// @namespace Webnick.UI
// @description Pre-fetches content from links in a page. Released under the GPL license, http://www.gnu.org/copyleft/gpl.html
// Skips mailto:, ftp:, ftps:, chrome: and javascript:
// protocols (suggest other protocols to skip please).
// Also skips iframes and frames for now.
// This is a work in progress.
// @include *
// ==/UserScript==
(function() {
if (self != top) {
// skip frames and iframes for now.
return;
}
// Change this time to whatever you want.
// I found on my PC, 5 seconds (5000 milliseconds) works best.
var preFetchWaitTime = 5000;
// set this to true if you want to see links being processed.
var seePrefetching = false;
var links = self.document.getElementsByTagName("a");
var linksToProcess = [];
var currentLink;
var preFetcher;
if (seePrefetching) {
var div = document.createElement("div");
div.style.position = "fixed";
div.style.top = "0px";
div.style.left = "0px";
div.style.width = "600px";
div.style.height = "100px";
div.style.overflow = "auto";
div.style.backgroundColor = "orange";
div.style.textAlign = "left";
div.style.padding = "5px";
document.body.appendChild(div);
}
for (linkIndex = 0; linkIndex < links.length; linkIndex++) {
currentLink = links[linkIndex].href;
if (!(/^(mailto:)|(ftp:)|(ftps:)|(chrome:)/i.test(currentLink))
&& !linksToProcess[currentLink]) {
linksToProcess[linksToProcess.length] = currentLink;
}
}
setTimeout(
function preFetch() {
if (linksToProcess.length > 0) {
var url = linksToProcess.pop();
if (seePrefetching) {
div.innerHTML += "Pre-fetching " + url + "<br />";
}
preFetcher = document.createElement("iframe");
preFetcher.src = url;
preFetcher.id = (new Date()).getTime();
preFetcher.style.display = "none";
preFetcher.setAttribute("onload"
, "setTimeout(function(){top.document.body.removeChild("
+ document.getElementById('" + preFetcher.id + "'))}, 0);");
document.body.appendChild(preFetcher);
setTimeout(preFetch, preFetchWaitTime);
}
}
, preFetchWaitTime);
})();
Labels: firefox, greasemonkey, javascript