Last active
April 24, 2017 01:22
-
-
Save cauerego/c970f7921f906ddf895cda05d30a7a93 to your computer and use it in GitHub Desktop.
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. made for http://tampermonkey.net/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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 [email protected] | |
// @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(`<style type="text/css">@media print {${css}}</style>`); | |
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(`<style type="text/css">${css}</style>`); | |
}); | |
} | |
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); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment