// ==UserScript==
// @name Reddit Fix
// @namespace http://tampermonkey.net/
// @version 1.3.5.1
// @description Fix of the infinite scroll, remove background effects, copy video adress
// @author Bum
// @require http://code.jquery.com/jquery-3.4.1.min.js
// @match https://www.reddit.com/*
// @grant GM_addStyle
// @downloadURL none
// ==/UserScript==
var holdTopicsInMemory = "false";
var commentEffects = "false";
var originalBehavior = "false";
var topicLimitInDom = 50;
var lastScrollTop = 0;
var scrollTopWhenREmoved = -1;
var lastTopicRemovedTime = new Date().getTime();
if (localStorage.getItem("holdTopicsInMemory") != null) {
holdTopicsInMemory = localStorage.getItem("holdTopicsInMemory");
}
if (localStorage.getItem("commentEffects") != null) {
commentEffects = localStorage.getItem("commentEffects");
}
if (localStorage.getItem("originalBehavior") != null) {
originalBehavior = localStorage.getItem("originalBehavior");
}
var topicsInMemory = [];
var isAPop = false;
var maxOffset = 0;
(function() {
'use strict';
function GM_addStyle(css) {
const style = document.getElementById("GM_addStyle") || (function() {
const style = document.createElement('style');
style.type = 'text/css';
style.id = "GM_addStyle";
document.head.appendChild(style);
return style;
})();
const sheet = style.sheet;
sheet.insertRule(css, (sheet.rules || sheet.cssRules || []).length);
}
if (commentEffects == "false")
{
GM_addStyle ( 'img[src*="flame"]{display:none;}' );
GM_addStyle ( 'div[aria-role="presentation"]{box-shadow:none !important;background : transparent !important;}' );
}
window.addEventListener('scroll', throttle(callback, 300));
function throttle(fn, wait) {
var time = Date.now();
return function() {
if ((time + wait - Date.now()) < 0) {
fn();
time = Date.now();
}
}
}
function getMenuItem(id, display)
{
return '';
}
function getCheckBoxItem(checked, id, display){
if (checked == 'true')
return ''+display+'
';
else
return ''+display+'
';
}
function RemoveMenu(){
$("#redditFixReloadAll").remove();
$("#redditFixReload25").remove();
}
function AddMenu(){
var menu = $("._2pUO1Sfe7WlIHvq6goN3Pz");
if (menu.find(".customRedditFixMenu").length > 0 )
return;
menu.append(getMenuItem('redditFixReloadAll', 'Reload All'));
$("#redditFixReloadAll").click(function(){
for (var i = topicsInMemory.length - 1; i >= 0; --i) {
var el = topicsInMemory.pop(i);
$(".rpBJOHq2PR60pnwJlUyP0").prepend(el);
}
maxOffset = $(document).height();
});
menu.append(getMenuItem('redditFixReload25', 'Reload 25'));
$("#redditFixReload25").click(function(){
var reloadTill = topicsInMemory.length;
if (reloadTill > 25)
reloadTill = 25;
for (var i = 0; i < 25; ++i) {
var el = topicsInMemory.pop(i);
$(".rpBJOHq2PR60pnwJlUyP0").prepend(el);
}
maxOffset = $(document).height();
});
}
$("#email-collection-tooltip-id").click(function(){
function _appendMenu() {
var menuMain = $('div[role="menu"]');
var isReady = menuMain.length > 0;
if (!isReady) {
setTimeout(_appendMenu, 100);
return;
}
if (menuMain.find("#fixRedditCustomMenuB").length == 0){
menuMain.find('a:last').before(getCheckBoxItem(commentEffects, "redditFixCheckBoxBackground", "Comment effects"));
$("#redditFixCheckBoxBackground").click(function(){
var btnBackgroundsChecked = $(this);
if (btnBackgroundsChecked.hasClass("_1L5kUnhRYhUJ4TkMbOTKkI")){
btnBackgroundsChecked.removeClass("_1L5kUnhRYhUJ4TkMbOTKkI");
localStorage.setItem("commentEffects", false);
commentEffects = "false";
}
else{
btnBackgroundsChecked.addClass("_1L5kUnhRYhUJ4TkMbOTKkI");
localStorage.setItem("commentEffects", true);
commentEffects = "true";
}
});
menuMain.find('a:last').before(getCheckBoxItem(holdTopicsInMemory, "fixRedditKeepTopicsInMemory", "Save topics in ram"));
$("#fixRedditKeepTopicsInMemory").click(function(){
var btnTopicsChecked = $(this);
if (btnTopicsChecked.hasClass("_1L5kUnhRYhUJ4TkMbOTKkI")){
btnTopicsChecked.removeClass("_1L5kUnhRYhUJ4TkMbOTKkI");
localStorage.setItem("holdTopicsInMemory", false);
holdTopicsInMemory = "false";
RemoveMenu();
}
else{
btnTopicsChecked.addClass("_1L5kUnhRYhUJ4TkMbOTKkI");
localStorage.setItem("holdTopicsInMemory", true);
holdTopicsInMemory = "true";
AddMenu();
}
});
menuMain.find('a:last').before(getCheckBoxItem(originalBehavior, "fixRedditoriginalBehavior", "Original behavior"));
$("#fixRedditoriginalBehavior").click(function(){
var btnTopicsChecked = $(this);
if (btnTopicsChecked.hasClass("_1L5kUnhRYhUJ4TkMbOTKkI")){
btnTopicsChecked.removeClass("_1L5kUnhRYhUJ4TkMbOTKkI");
localStorage.setItem("originalBehavior", false);
originalBehavior = "false";
}
else{
btnTopicsChecked.addClass("_1L5kUnhRYhUJ4TkMbOTKkI");
localStorage.setItem("originalBehavior", true);
originalBehavior = "true";
}
});
menuMain.find("a:last").before('
');
}
}
_appendMenu();
});
function attachObserver(){
if ($(".observerIsAttached").length > 0)
return;
$(".rpBJOHq2PR60pnwJlUyP0").addClass("observerIsAttached");
var config = { attributes: false, childList: true, subtree: true };
var targetNodeRoot = $(".rpBJOHq2PR60pnwJlUyP0").first().get(0);
var configRoot = { attributes: false, childList: true, subtree: true };
var callbackRoot = function(mutationsList, observer) {
for(var mutation of mutationsList) {
mutation.addedNodes.forEach(function(node) {
var currentTime = new Date().getTime();
var time = currentTime - lastTopicRemovedTime;
if (time > 30000 && $(node).find(".scrollerItem")){
if ($(".scrollerItem").length > topicLimitInDom && scrollTopWhenREmoved < lastScrollTop){
$(".scrollerItem").slice(0,29).each(function(){
var currElHeight = 300;
if ($(this).parent().parent().attr("style") != ""){
var el = $(this);
if (el.offset().top == 0){
if (holdTopicsInMemory)
topicsInMemory.push(el.parent().parent());
currElHeight = el.parent().parent().height();
el.parent().parent().remove();
}
}
$(window).scrollTop($(window).scrollTop() - currElHeight);
lastTopicRemovedTime = new Date().getTime();
scrollTopWhenREmoved = lastScrollTop+20000;
});
console.log("removed top 20 elements");
}
}
});
}
};
var observerroot = new MutationObserver(callbackRoot);
observerroot.observe(targetNodeRoot, config);
}
function copyToClipboard(element) {
var $temp = $("");
$("body").append($temp);
$temp.val($(element).text()).select();
document.execCommand("copy");
$temp.remove();
}
function attachTopicObserver(){
var config = { attributes: false, childList: true, subtree: true };
var targetNodeTopics = $("#2x-container").first().get(0);
var configRoot = { attributes: false, childList: true, subtree: true };
var callbackTopic = function(mutationsList, observer) {
for(var mutation of mutationsList) {
mutation.addedNodes.forEach(function(node) {
if (($('div[data-test-id="post-content"]:not(.ObserverAttached)').length > 0)){
var content = $('div[data-test-id="post-content"]');
content.addClass("ObserverAttached");
content.find("._3-miAEojrCvx_4FQ8x3P-s").append(' Get Direct Link');
$("#fixDirectLink").css("cursor","pointer");
var urlThisWindow = $(content).find("._3jOxDPIQ0KaOWpzvSQo-1s").attr("href");
if (urlThisWindow.substring(urlThisWindow.length-1) == "/")
{
urlThisWindow = urlThisWindow.substring(0, urlThisWindow.length-1);
}
urlThisWindow = urlThisWindow+".json";
$("#fixDirectLink").click(function(){
$.getJSON( urlThisWindow, function( data ) {
try {
var urlFallback = data[0].data.children[0].data.secure_media.reddit_video.fallback_url;
copyToClipboard($(""+urlFallback+"
"));
var popupCopy = '';
$("#POPUP_CONTAINER").append(popupCopy);
setTimeout(function(){ $(".fixObserverpopup").remove(); }, 3000);
}
catch(err) {
var popupCopyfailed = '';
$("#POPUP_CONTAINER").append(popupCopyfailed);
setTimeout(function(){ $(".fixObserverpopup").remove(); }, 3000);
}
});
});
}
});
}
};
var observerTopic = new MutationObserver(callbackTopic);
observerTopic.observe(targetNodeTopics, config);
}
attachTopicObserver();
function callback() {
if (originalBehavior == "true")
return;
if (holdTopicsInMemory == "true")
AddMenu();
var st = $(document).scrollTop();
if (st > maxOffset)
{
if (st > lastScrollTop){
attachObserver();
}
lastScrollTop = st;
console.log(lastScrollTop);
maxOffset = 0;
}
}
})();