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