Skip to content

Instantly share code, notes, and snippets.

@ryangadams
Created October 4, 2012 21:40
Show Gist options
  • Save ryangadams/3836644 to your computer and use it in GitHub Desktop.
Save ryangadams/3836644 to your computer and use it in GitHub Desktop.
A js bookmarklet to show a preview of the whole of a long page (try it on the daily mail site)
// javascript:PagePreview%20=%20{run:%20function()%20{var%20ifr%20=%20document.querySelector(%22#my-preview-iframe%22);var%20source%20=%20window.location.href;if%20(!ifr)%20{ifr%20=%20document.createElement(%22iframe%22);ifr.setAttribute(%22id%22,%20%22my-preview-iframe%22);}var%20width%20=%20%22100%22;var%20height%20=%20%22100%22;ifr.setAttribute(%22style%22,%20%22position:absolute;top:0px;left:0px;z-index:100000;background:#fff;width:%22%20+%20width%20+%20%22px;height:%22%20+%20height%20+%20%22px;overflow:hidden;border:2px%20solid%20#008800;%22);ifr.setAttribute(%22src%22,%20source);document.body.appendChild(ifr);ifr.addEventListener(%22load%22,%20PagePreview.scalePage,%20false);ifr.contentWindow.location%20=%20window.location.href;},scalePage:%20function(){var%20pageWidth%20=%20document.body.scrollWidth;var%20pageHeight%20=%20document.body.scrollHeight;var%20scaleFactor%20=%200.1;var%20bd%20=%20this.contentDocument.body;bd.style.transformOrigin%20=%20%22center%20top%22;bd.style.transform%20=%20%22scale(%22+%20scaleFactor%20+%22)%20translateX(-%22%20+%20(((pageWidth/2)%20*%200.9)-40)%20+%20%22px)%22;this.style.width%20=%20(pageWidth%20*%20scaleFactor)%20+%20%22px%22;this.style.height%20=%20(pageHeight%20*%20scaleFactor)%20+%20%22px%22;}};PagePreview.run();
PagePreview = {
run: function() {
var ifr = document.querySelector("#my-preview-iframe");
var source = window.location.href;
if (!ifr) {
ifr = document.createElement("iframe");
ifr.setAttribute("id", "my-preview-iframe");
}
var width = "100";
var height = "100";
ifr.setAttribute("style", "position:absolute;top:0px;left:0px;z-index:100000;background:#fff;width:" + width + "px;height:" + height + "px;overflow:hidden;border:2px solid #008800;");
ifr.setAttribute("src", source);
document.body.appendChild(ifr);
ifr.addEventListener("load", PagePreview.scalePage, false);
ifr.contentWindow.location = window.location.href;
},
scalePage: function(){
var pageWidth = document.body.scrollWidth;
var pageHeight = document.body.scrollHeight;
var scaleFactor = 0.1;
var bd = this.contentDocument.body;
bd.style.transformOrigin = "center top";
bd.style.transform = "scale("+ scaleFactor +") translateX(-" + (((pageWidth/2) * 0.9)-40) + "px)";
this.style.width = (pageWidth * scaleFactor) + "px";
this.style.height = (pageHeight * scaleFactor) + "px";
}
};
PagePreview.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment