
Untitled
By: a guest on
Jun 14th, 2012 | syntax:
JavaScript | size: 1.73 KB | hits: 17 | expires: Never
// ==UserScript==
// @name Google Docs viewer
// @namespace mikecupcake
// @version 0.1
// @include *
// @exclude http*://docs.google.com/*
// @exclude http*://viewer.zoho.com/*
// @exclude http*://office.live.com/*
// @exclude http*://*.mediafire.com/*
// ==/UserScript==
// based on Deekshith Allamaneni's Docs Online Viewer script
var docLinks = document.links;
var fileTypes = ["doc","docx","xls","xlsx","ppt","pps","pptx","pdf","eps","ps","tif","tiff","ai","psd","pages","dxf","ttf","xps"];
checkLinks();
var check = true;
document.addEventListener('DOMNodeInserted',function(e){
if (check){
setTimeout(function(){
checkLinks();
check = true;
}, 1000);
check = false;
}
},true);
function checkLinks(){
console.log("run");
for (var i = 0; i < docLinks.length; ++i) {
if (docLinks[i].host != "docs.google.com" && docLinks[i].charset != "checked"){
docLinks[i].charset="checked"; // no browsers use this attribute, should be safe to use it to flag the link as checked
for (var i2 = 0; i2 < fileTypes.length; i2++) {
var url = stripQuery(docLinks[i]);
if (endsWith(url, '.' + fileTypes[i2])) {
changeLink(docLinks[i]);
break;
}
}
}
}
}
function stripQuery(link) {
return link.protocol + '//' + link.hostname + link.pathname; // remove any ?query in the URL
}
function endsWith(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
function changeLink(link) {
link.href = stripQuery(link);
link.href = "https://docs.google.com/viewer?url="+encodeURI(link)+"&embedded=true&chrome=false";
}