Make Medium Readable Again https://makemediumreadable.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

108 line
3.5 KiB

  1. //
  2. // Make Medium Readable Again
  3. //
  4. var makeReadable = function() {
  5. // Un-position:fixed the top nav bar
  6. var topNav = document.querySelector('.metabar.u-fixed');
  7. if (topNav) {
  8. topNav.classList.remove('u-fixed');
  9. }
  10. // Remove the "Pardon the interruption" popup.
  11. // We do this with JS because the .overlay.overlay--lighter element is used
  12. // for interactions we consent to, like the sign up / log in dialogs, so we
  13. // don't want to obliterate them too.
  14. // FIXME: prevent this from breaking signup/login dialogs when the popup
  15. // is removed (it works after changing pages).
  16. var headings = document.evaluate("//h1[contains(., 'Pardon the interruption.')]", document, null, XPathResult.ANY_TYPE, null );
  17. var thisHeading = headings.iterateNext();
  18. if (thisHeading != null) {
  19. var $overlay = thisHeading.parentNode.parentNode.parentNode.parentNode;
  20. $overlay.parentNode.removeChild($overlay);
  21. }
  22. // Inject remaining styles
  23. // This check makes sure the extension works on Chrome and Firefox.
  24. if (typeof browser === 'undefined') {
  25. browser = chrome;
  26. }
  27. var mediumCSS = browser.runtime.getURL("medium.css");
  28. if (document.querySelector('head link[href="' + mediumCSS + '"]') == null) {
  29. document.head.insertAdjacentHTML('beforeend', '<link rel="stylesheet" type="text/css" href="' + mediumCSS + '">');
  30. }
  31. };
  32. var hideHighlightMenu = function() {
  33. document.head.insertAdjacentHTML('beforeend', '<style type="text/css">.highlightMenu { display: none; }</style>');
  34. };
  35. var hideDickbar = function() {
  36. var dickbar = document.querySelector('.js-postShareWidget');
  37. if (dickbar) {
  38. dickbar.style.display = 'none';
  39. }
  40. var footerDickbar = document.querySelector('footer > .container:first-child');
  41. if (footerDickbar) {
  42. footerDickbar.style.display = 'none';
  43. }
  44. };
  45. var disableLazyLoading = function() {
  46. // Get all <noscript> tags accompanying dynamically-loading <img>s
  47. var hiddenMedia = document.querySelectorAll('noscript.js-progressiveMedia-inner');
  48. if (hiddenMedia.length === 0) {
  49. return;
  50. }
  51. for (var i=0; i<hiddenMedia.length; i++) {
  52. // Create new <img> element from the one in <noscript> and add it in.
  53. // This is certainly a roundabout way of doing things, but I didn't want to
  54. // spend more time reverse-engineering Medium's lazy-loading code.
  55. var img = new Image();
  56. var srcMatch = hiddenMedia[i].textContent.match(/src="(https:\/\/[^"]+)"/);
  57. if (srcMatch != null) {
  58. img.src = srcMatch[1];
  59. img.className = hiddenMedia[i].textContent.match(/class="([^"]+)"/)[1];
  60. hiddenMedia[i].parentNode.appendChild(img);
  61. }
  62. }
  63. };
  64. var shrinkHeaderImages = function() {
  65. var ridiculousHeaderImage = document.querySelector('figure.graf--layoutFillWidth');
  66. if (ridiculousHeaderImage) {
  67. ridiculousHeaderImage.style.maxWidth = '700px';
  68. ridiculousHeaderImage.style.margin = '0 auto';
  69. }
  70. }
  71. var observer = new MutationObserver(function(mutations){
  72. mutations.forEach(function(){
  73. makeReadable();
  74. shrinkHeaderImages();
  75. });
  76. });
  77. var config = {attributes: true};
  78. // This extension runs on all domains so it can Make Medium Readable Again even for publications on custom domains.
  79. // Here we make sure the code only runs on Medium sites.
  80. if (document.querySelector('head meta[property="al:ios:app_name"][content="medium" i]')) {
  81. makeReadable();
  82. shrinkHeaderImages();
  83. chrome.storage.sync.get(null, function(items) {
  84. if (items.hideDickbar) {
  85. hideDickbar();
  86. }
  87. if (items.disableLazyImages) {
  88. disableLazyLoading();
  89. }
  90. if (items.hideHighlightMenu) {
  91. hideHighlightMenu();
  92. }
  93. });
  94. observer.observe(document.body, config);
  95. }