Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 14th, 2012  |  syntax: JavaScript  |  size: 1.73 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // ==UserScript==
  2. // @name        Google Docs viewer
  3. // @namespace   mikecupcake
  4. // @version     0.1
  5. // @include        *
  6. // @exclude        http*://docs.google.com/*
  7. // @exclude        http*://viewer.zoho.com/*
  8. // @exclude        http*://office.live.com/*
  9. // @exclude        http*://*.mediafire.com/*
  10. // ==/UserScript==
  11.  
  12. // based on Deekshith Allamaneni's Docs Online Viewer script
  13.  
  14.  
  15. var docLinks = document.links;
  16. var fileTypes = ["doc","docx","xls","xlsx","ppt","pps","pptx","pdf","eps","ps","tif","tiff","ai","psd","pages","dxf","ttf","xps"];
  17.  
  18. checkLinks();
  19.  
  20.  
  21. var check = true;
  22.  document.addEventListener('DOMNodeInserted',function(e){
  23.     if (check){
  24.       setTimeout(function(){
  25.         checkLinks();
  26.         check = true;
  27.         }, 1000);
  28.       check = false;
  29.     }
  30.   },true);
  31.  
  32.  
  33. function checkLinks(){
  34. console.log("run");
  35.   for (var i = 0; i < docLinks.length; ++i) {
  36.        if (docLinks[i].host != "docs.google.com" && docLinks[i].charset != "checked"){
  37.             docLinks[i].charset="checked";  // no browsers use this attribute, should be safe to use it to flag the link as checked
  38.             for (var i2 = 0; i2 < fileTypes.length; i2++) {
  39.                 var url = stripQuery(docLinks[i]);
  40.                 if (endsWith(url, '.' + fileTypes[i2])) {
  41.                    changeLink(docLinks[i]);
  42.                    break;
  43.                 }
  44.             }
  45.         }
  46.    }
  47. }
  48.  
  49. function stripQuery(link) {
  50.  return link.protocol + '//' + link.hostname + link.pathname;  // remove any ?query in the URL     
  51. }
  52.  
  53.  
  54. function endsWith(str, suffix) {
  55.     return str.indexOf(suffix, str.length - suffix.length) !== -1;
  56. }
  57.  
  58.  
  59. function changeLink(link) {
  60.         link.href = stripQuery(link);
  61.         link.href = "https://docs.google.com/viewer?url="+encodeURI(link)+"&embedded=true&chrome=false";
  62. }