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.
 
 
 

105 lines
3.4 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. document.head.insertAdjacentHTML('beforeend', '<link rel="stylesheet" type="text/css" href="' + browser.runtime.getURL("medium.css") + '">');
  28. };
  29. var hideHighlightMenu = function() {
  30. document.head.insertAdjacentHTML('beforeend', '<style type="text/css">.highlightMenu { display: none; }</style>');
  31. };
  32. var hideDickbar = function() {
  33. var dickbar = document.querySelector('.js-postShareWidget');
  34. if (dickbar) {
  35. dickbar.style.display = 'none';
  36. }
  37. var footerDickbar = document.querySelector('footer > .container:first-child');
  38. if (footerDickbar) {
  39. footerDickbar.style.display = 'none';
  40. }
  41. };
  42. var disableLazyLoading = function() {
  43. // Get all <noscript> tags accompanying dynamically-loading <img>s
  44. var hiddenMedia = document.querySelectorAll('noscript.js-progressiveMedia-inner');
  45. if (hiddenMedia.length === 0) {
  46. return;
  47. }
  48. for (var i=0; i<hiddenMedia.length; i++) {
  49. // Create new <img> element from the one in <noscript> and add it in.
  50. // This is certainly a roundabout way of doing things, but I didn't want to
  51. // spend more time reverse-engineering Medium's lazy-loading code.
  52. var img = new Image();
  53. var srcMatch = hiddenMedia[i].textContent.match(/src="(https:\/\/[^"]+)"/);
  54. if (srcMatch != null) {
  55. img.src = srcMatch[1];
  56. img.className = hiddenMedia[i].textContent.match(/class="([^"]+)"/)[1];
  57. hiddenMedia[i].parentNode.appendChild(img);
  58. }
  59. }
  60. };
  61. var shrinkHeaderImages = function() {
  62. var ridiculousHeaderImage = document.querySelector('figure.graf--layoutFillWidth');
  63. if (ridiculousHeaderImage) {
  64. ridiculousHeaderImage.style.maxWidth = '700px';
  65. ridiculousHeaderImage.style.margin = '0 auto';
  66. }
  67. }
  68. var observer = new MutationObserver(function(mutations){
  69. mutations.forEach(function(){
  70. makeReadable();
  71. shrinkHeaderImages();
  72. });
  73. });
  74. var config = {attributes: true};
  75. // This extension runs on all domains so it can Make Medium Readable Again even for publications on custom domains.
  76. // Here we make sure the code only runs on Medium sites.
  77. if (document.querySelector('head meta[property="al:ios:app_name"][content="medium" i]')) {
  78. makeReadable();
  79. shrinkHeaderImages();
  80. chrome.storage.sync.get(null, function(items) {
  81. if (items.hideDickbar) {
  82. hideDickbar();
  83. }
  84. if (items.disableLazyImages) {
  85. disableLazyLoading();
  86. }
  87. if (items.hideHighlightMenu) {
  88. hideHighlightMenu();
  89. }
  90. });
  91. observer.observe(document.body, config);
  92. }