office_online_viewer

View office documents in your browser with Microsoft Office Online

  1. // ==UserScript==
  2. // @name office_online_viewer
  3. // @description View office documents in your browser with Microsoft Office Online
  4. // @namespace liudonghua123
  5. // @version 0.0.1
  6. // @include *
  7. // @exclude http*://view.officeapps.live.com/*
  8. // @exclude http*://docs.google.com/*
  9. // @exclude http*://mail.google.com/*
  10. // @exclude http*://viewer.zoho.com/*
  11. // @exclude http*://office.live.com/*
  12. // ==/UserScript==
  13.  
  14. (function process() {
  15. const pageLinks = [...document.links];
  16. const officeWordExtension = ["doc", "docx"],
  17. officeExcelExtension = ["xls", "xlsx"],
  18. officePowerpointExtension = ["ppt", "pps", "pptx"];
  19. const officeExtension = [...officeWordExtension, ...officeExcelExtension, ...officePowerpointExtension];
  20. const viewOfficeUrl = "https://view.officeapps.live.com/op/view.aspx";
  21. const isOfficeLink = (link) => {
  22. const { pathname: path, text } = link;
  23. const pathExtension = path.lastIndexOf('.') > 0 ? path.substr(path.lastIndexOf('.') + 1).toLowerCase() : '';
  24. const textExtension = text.lastIndexOf('.') > 0 ? text.substr(text.lastIndexOf('.') + 1).toLowerCase() : '';
  25. return officeExtension.includes(pathExtension) || officeExtension.includes(textExtension);
  26. };
  27. const linkIcon = (link) => {
  28. const { pathname: path, text } = link;
  29. const pathExtension = path.lastIndexOf('.') > 0 ? path.substr(path.lastIndexOf('.') + 1).toLowerCase() : '';
  30. const textExtension = text.lastIndexOf('.') > 0 ? text.substr(text.lastIndexOf('.') + 1).toLowerCase() : '';
  31. if (officeWordExtension.includes(pathExtension) || officeWordExtension.includes(textExtension)) return "https://docs.microsoft.com/zh-cn/javascript/api/overview/images/logo-word.svg";
  32. else if (officeExcelExtension.includes(pathExtension) || officeExcelExtension.includes(textExtension)) return "https://docs.microsoft.com/zh-cn/javascript/api/overview/images/logo-excel.svg";
  33. else if (officePowerpointExtension.includes(pathExtension) || officePowerpointExtension.includes(textExtension)) return "https://docs.microsoft.com/zh-cn/javascript/api/overview/images/logo-powerpoint.svg";
  34. else return '';
  35. };
  36. const addOfficeBadge = (link) => {
  37. const officeLink = document.createElement('a');
  38. officeLink.href = `${viewOfficeUrl}?src=${encodeURIComponent(link.href)}`;
  39. officeLink.isParsed = true;
  40. officeLink.target = "_blank";
  41. const ico = document.createElement("img");
  42. ico.src = linkIcon(link);
  43. ico.style.marginLeft = "5px";
  44. ico.style.height = "25px";
  45. officeLink.appendChild(ico);
  46. link.parentNode.insertBefore(officeLink, link.nextSibling);
  47. }
  48. const officeLinks = pageLinks.filter(isOfficeLink);
  49. for (const link of officeLinks) {
  50. console.info(`process ${link.href}`);
  51. addOfficeBadge(link);
  52. }
  53. })();