1
0
mirror of https://github.com/thebaer/MMRA synced 2024-11-13 10:11:01 +00:00
MMRA/content.js

82 lines
2.2 KiB
JavaScript
Raw Permalink Normal View History

2017-08-28 00:17:08 +00:00
//
// Make Medium Readable Again
//
var makeReadable = function() {
// Un-position:fixed the top nav bar
var topNav = document.querySelector('.metabar.u-fixed');
2017-12-16 09:39:40 +00:00
if (topNav) {
2017-08-28 00:17:08 +00:00
topNav.classList.remove('u-fixed');
}
// Remove the footer
var getUpdatesBar = document.querySelector('.js-stickyFooter');
2017-12-16 09:39:40 +00:00
if (getUpdatesBar) {
2017-08-28 00:17:08 +00:00
getUpdatesBar.style.display = 'none';
}
};
var hideDickbar = function() {
var dickbar = document.querySelector('.js-postShareWidget');
2017-12-16 09:39:40 +00:00
if (dickbar) {
dickbar.style.display = 'none';
}
var footerDickbar = document.querySelector('footer > .container:first-child');
2017-12-16 09:39:40 +00:00
if (footerDickbar) {
footerDickbar.style.display = 'none';
}
};
var disableLazyLoading = function() {
// Get all <noscript> tags accompanying dynamically-loading <img>s
var hiddenMedia = document.querySelectorAll('noscript.js-progressiveMedia-inner');
2017-12-16 09:39:40 +00:00
if (hiddenMedia.length === 0) {
return;
}
for (var i=0; i<hiddenMedia.length; i++) {
// Create new <img> element from the one in <noscript> and add it in.
// This is certainly a roundabout way of doing things, but I didn't want to
// spend more time reverse-engineering Medium's lazy-loading code.
2017-12-16 09:39:40 +00:00
var img = new Image();
var srcMatch = hiddenMedia[i].textContent.match(/src="(https:\/\/[^"]+)"/);
if (srcMatch != null) {
img.src = srcMatch[1];
img.className = hiddenMedia[i].textContent.match(/class="([^"]+)"/)[1];
hiddenMedia[i].parentNode.appendChild(img);
}
}
};
var shrinkHeaderImages = function() {
var ridiculousHeaderImage = document.querySelector('figure.graf--layoutFillWidth');
2017-12-16 09:39:40 +00:00
if (ridiculousHeaderImage) {
ridiculousHeaderImage.style.maxWidth = '700px';
ridiculousHeaderImage.style.margin = '0 auto';
}
}
var observer = new MutationObserver(function(mutations){
mutations.forEach(function(){
makeReadable();
shrinkHeaderImages();
});
});
var config = {attributes: true};
2017-08-28 00:17:08 +00:00
// Only run this on Medium sites.
2017-12-16 09:39:40 +00:00
if (document.querySelector('head meta[property="al:ios:app_name"][content="medium"]')) {
2017-08-28 00:17:08 +00:00
makeReadable();
shrinkHeaderImages();
chrome.storage.sync.get(null, function(items) {
if (items.hideDickbar) {
hideDickbar();
}
if (items.disableLazyImages) {
disableLazyLoading();
}
});
2017-12-16 09:39:40 +00:00
observer.observe(document.body, config);
2017-08-28 00:17:08 +00:00
}