PDF Background Color Controller

This script can change the backgroud color for the pdf files opened by your brower. It is applicable for Chrome build-in PDF viewer, pdf.js, overleaf's pdf reviewer and your local files (Please allow the Tampermonkey to access file urls in browser's setting by going to chrome://extensions/, and set Allow access to file URLs.).

  1. // ==UserScript==
  2. // @name PDF Background Color Controller
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.7
  5. // @description This script can change the backgroud color for the pdf files opened by your brower. It is applicable for Chrome build-in PDF viewer, pdf.js, overleaf's pdf reviewer and your local files (Please allow the Tampermonkey to access file urls in browser's setting by going to chrome://extensions/, and set Allow access to file URLs.).
  6. // @author Maple
  7. // @match *://*/*
  8. // @icon https://icon-icons.com/downloadimage.php?id=130274&root=2107/ICO/64/&file=file_type_pdf_icon_130274.ico
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. function get_cover() {
  14. let css = `
  15. position: absolute;
  16. pointer-events: none;
  17. top: 0;
  18. left: 0;
  19. width: 100%;
  20. height: 100%;
  21. background-color: #FDF6E3;
  22. mix-blend-mode: multiply;
  23. z-index: 100000;
  24. `;
  25. var cover = document.createElement("div");
  26. cover.setAttribute("style", css);
  27. return cover;
  28. }
  29.  
  30. function renderPage(url, panelSelector) {
  31. if (window.location.href.includes(url)) {
  32. var cover = get_cover();
  33. var scope = document.querySelector(panelSelector);
  34. //console.log(scope);
  35. if (scope === null) {
  36. setTimeout(() => renderPage(url, panelSelector), 1000); // Retry after 1 second
  37. } else {
  38. scope.appendChild(cover);
  39. }
  40. }
  41. }
  42.  
  43. (function() {
  44. 'use strict';
  45.  
  46. renderPage("https://www.overleaf.com/project/", "div.ide-react-panel[data-panel-id='panel-pdf']"); // Call renderPage for possible Overleaf pages
  47. renderPage("https://docs.google.com/", "div.kix-scrollareadocumentplugin.docs-ui-hit-region-surface"); // Call renderPage for possible Overleaf pages
  48.  
  49. var embed = document.querySelector("embed");
  50. if ((embed !== null && embed.type == "application/pdf") || (typeof pdfjsLib !== "undefined") || (window.location.href.includes(".pdf"))) {
  51. var cover = get_cover();
  52. document.body.appendChild(cover);
  53. }
  54. })();