// ==UserScript== // @name print reddit // @namespace reddit // @include http*//*reddit.com/* // @version 0.1 // @description a few new buttons for reddit: prepare page for printing (or saving) in a better reading streamable format, open all comments and remove side bar. // @author caue@cregox.com // @grant none // ==/UserScript== (function() { 'use strict'; var start = function() { var css = ` .side {float: none;} #siteTable .link.self {margin-right: 0;} .commentarea.print div {margin-right: 0 !important;} div.md {max-width: none;} `; $('head').append(``); var shortlink = document.querySelector('div.shortlink'); var createButton = function (caption) { var div = document.createElement('div'); div.class = 'printbutton'; var button = document.createElement('button'); div.appendChild(button); shortlink.parentNode.appendChild(div); button.textContent = caption; return button; }; $('.commentarea').addClass('print'); formatForPrinting(); function formatForPrinting () { $(createButton('format for printing')).click(function() { $('head').append(``); }); } openAllComments(); function openAllComments () { function countComments () { return $('.commentarea .comment:visible').length; } var delay = 500; var itemsOpened = 0; var button = createButton('open all comments (' + countComments() + ')'); $(button).click(function() { button.disabled = true; var item; var checkItem = function() {}; var stuck = 0; var interval = setInterval(function() { if (checkItem()) { // still loading if (stuck > 20) { finish(); $('span.morecomments a.button.opened:contains("loading")').first().focus(); } stuck += 1; return; } if (!item || item.length === 0) { item = $('span.morecomments a.button:visible:not(.opened)').first(); checkItem = function() { if (item && !item.is(':visible')){ item = null; return false; } return true; }; } if (!item || item.length === 0) { item = $('div.collapsed>div>p>a.expand:visible:not(.opened):contains("[+]")'); // all at once, why not? checkItem = function() { item = null; return false; }; } if (item && item.length > 0 && item.is(':visible') && !item.is('.opened')) { stuck = 0; item.addClass('opened'); itemsOpened += 1; button.textContent = 'openning ' + itemsOpened; item.click(); checkItem(); } else { finish(); } }, delay); function finish () { button.textContent = 'open all comments (' + countComments() + ')'; button.disabled = false; clearInterval(interval); } }); } removeThisSide(); function removeThisSide () { $(createButton('remove this side')).click(function() { $('.side').toggle(); }); } }; var script = document.createElement('script'); script.type = 'text/javascript'; script.textContent = '(' + start + ')()'; document.body.appendChild(script); })();