Skip to content

Instantly share code, notes, and snippets.

@Ramblurr
Created November 24, 2017 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ramblurr/7e61a7b488ec30290a7dbb79f00804e9 to your computer and use it in GitHub Desktop.
Save Ramblurr/7e61a7b488ec30290a7dbb79f00804e9 to your computer and use it in GitHub Desktop.
Imgur: Mobile Cleanup
// ==UserScript==
// @name Imgur: Mobile Cleanup
// @namespace https://github.com/Zren/
// @description Cleanup m.imgur.com and always load all images in the album.
// @icon https://imgur.com/favicon.ico
// @author Zren
// @version 2
// @include https://m.imgur.com/*
// @grant GM_addStyle
// ==/UserScript==
(function() {
'use strict';
let css = ".FloatingOIA-container { display: none; }";
css += ".PostLoader { display: none; }";
css += ".AppBanner { display: none; }";
css += ".Navbar { display: none; }";
css += ".Post-contentContainer { max-width: unset; }";
css += ".PostImage-imageContainer { height: auto !important; }";
if (window.location.pathname.startsWith('/gallery/') ||window.location.pathname.startsWith('/t/') ) {
css += ".GalleryHandler-postContainer { display: none; }";
css += ".GalleryInfiniteScroll > div > div:not(.GalleryHandler-postContainer) + .GalleryHandler-postContainer { display: block !important; }";
css += '.GalleryInfiniteScroll > div > div:not(.GalleryHandler-postContainer)[style="width: 100%; height: 9000px;"] { display: none; }';
}
GM_addStyle(css);
/** The css selector to only select items in the first post, which is the main one */
const firstPost = '.GalleryHandler-postContainer .Post:first-child';
/** Converts an imgur img url to the high-res .png version */
function imgSrcToOrig(imgSrc) {
const regex = /.*i.imgur.com\/(.*)_d.jpg.*/g;
const m = regex.exec(imgSrc);
if(m === null)
return false;
return `https://i.imgur.com/${m[1]}.png`;
}
/** Overwrite all imgur images (in the main post) with the high-res png versions */
function enhanceImages() {
document.querySelectorAll(firstPost + ' .Image')
.forEach((img) => {
const newSrc = imgSrcToOrig(img.src);
if(newSrc) img.src = newSrc;
});
}
/** On mobile the page can load slower, so we try 10 times, once a second to load the next page */
let attempts = 0;
function tick() {
enhanceImages();
var loadMoreButton = document.querySelector(firstPost + ' .Post-descriptionContainer~.Post-albumSeeMore.Button');
if (loadMoreButton) {
loadMoreButton.click();
setTimeout(tick, 500);
} else if( attempts < 10 ){
attempts += 1;
setTimeout(tick, 1000);
}
}
setTimeout(tick, 400);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment