Skip to content

Instantly share code, notes, and snippets.

@cauerego
Last active April 24, 2017 01:22
Show Gist options
  • Select an option

  • Save cauerego/c970f7921f906ddf895cda05d30a7a93 to your computer and use it in GitHub Desktop.

Select an option

Save cauerego/c970f7921f906ddf895cda05d30a7a93 to your computer and use it in GitHub Desktop.

Revisions

  1. cauerego revised this gist Apr 24, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion print-reddit.js
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    // @namespace reddit
    // @include http*//*reddit.com/*
    // @version 0.1
    // @description create a button in reddit to prepare page for printing (or saving) in a better reading streamable format
    // @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==
  2. cauerego revised this gist Apr 24, 2017. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions print-reddit.js
    Original file line number Diff line number Diff line change
    @@ -38,9 +38,12 @@ div.md {max-width: none;}
    }
    openAllComments();
    function openAllComments () {
    function countComments () {
    return $('.commentarea .comment:visible').length;
    }
    var delay = 500;
    var itemsOpened = 0;
    var button = createButton('open all comments');
    var button = createButton('open all comments (' + countComments() + ')');
    $(button).click(function() {
    button.disabled = true;
    var item;
    @@ -79,14 +82,13 @@ div.md {max-width: none;}
    itemsOpened += 1;
    button.textContent = 'openning ' + itemsOpened;
    item.click();
    console.log(itemsOpened, item);
    checkItem();
    } else {
    finish();
    }
    }, delay);
    function finish () {
    button.textContent = 'open all comments';
    button.textContent = 'open all comments (' + countComments() + ')';
    button.disabled = false;
    clearInterval(interval);
    }
  3. cauerego revised this gist Apr 24, 2017. 1 changed file with 47 additions and 19 deletions.
    66 changes: 47 additions & 19 deletions print-reddit.js
    Original file line number Diff line number Diff line change
    @@ -29,8 +29,6 @@ div.md {max-width: none;}
    button.textContent = caption;
    return button;
    };
    var nextDelay = 0;
    let itemsOpenned = 0;
    $('.commentarea').addClass('print');
    formatForPrinting();
    function formatForPrinting () {
    @@ -40,28 +38,58 @@ div.md {max-width: none;}
    }
    openAllComments();
    function openAllComments () {
    var delay = 500;
    var itemsOpened = 0;
    var button = createButton('open all comments');
    var openSelector = function(sel, delay) {
    $(sel).each(function (i, collapsed) {
    if (collapsed) {
    nextDelay += delay;
    setTimeout(function (item) {
    itemsOpenned += 1;
    button.textContent = 'openning ' + itemsOpenned;
    item.click();
    }, nextDelay, collapsed);
    }
    });
    };
    $(button).click(function() {
    button.disabled = true;
    openSelector('span.morecomments a.button', 500);
    openSelector('div.collapsed>div>p>a.expand', 100);
    nextDelay += 69;
    setTimeout(function() {
    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();
    console.log(itemsOpened, item);
    checkItem();
    } else {
    finish();
    }
    }, delay);
    function finish () {
    button.textContent = 'open all comments';
    button.disabled = false;
    }, nextDelay);
    clearInterval(interval);
    }
    });
    }
    removeThisSide();
  4. cauerego revised this gist Apr 23, 2017. 1 changed file with 23 additions and 25 deletions.
    48 changes: 23 additions & 25 deletions print-reddit.js
    Original file line number Diff line number Diff line change
    @@ -20,56 +20,54 @@ div.md {max-width: none;}
    `;
    $('head').append(`<style type="text/css">@media print {${css}}</style>`);
    var shortlink = document.querySelector('div.shortlink');
    var createButton = function () {
    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;
    };
    var printbutton = createButton();
    var nextDelay = 0;
    let itemsOpenned = 0;
    var openSelector = function(sel, delay) {
    $(sel).each(function (i, collapsed) {
    if (collapsed) {
    nextDelay += delay;
    setTimeout(function (item) {
    itemsOpenned += 1;
    printbutton.textContent = 'openning ' + itemsOpenned;
    item.click();
    }, nextDelay, collapsed);
    }
    });
    };
    $('.commentarea').addClass('print');
    formatForPrinting();
    function formatForPrinting () {
    printbutton.textContent = 'format for printing';
    $(printbutton).click(function() {
    $(createButton('format for printing')).click(function() {
    $('head').append(`<style type="text/css">${css}</style>`);
    openAllComments();
    });
    }
    openAllComments();
    function openAllComments () {
    printbutton.textContent = 'open all comments';
    $(printbutton).click(function() {
    printbutton.disabled = true;
    var button = createButton('open all comments');
    var openSelector = function(sel, delay) {
    $(sel).each(function (i, collapsed) {
    if (collapsed) {
    nextDelay += delay;
    setTimeout(function (item) {
    itemsOpenned += 1;
    button.textContent = 'openning ' + itemsOpenned;
    item.click();
    }, nextDelay, collapsed);
    }
    });
    };
    $(button).click(function() {
    button.disabled = true;
    openSelector('span.morecomments a.button', 500);
    openSelector('div.collapsed>div>p>a.expand', 100);
    nextDelay += 69;
    setTimeout(function() {
    removeThisSide();
    printbutton.disabled = false;
    button.textContent = 'open all comments';
    button.disabled = false;
    }, nextDelay);
    });
    }
    removeThisSide();
    function removeThisSide () {
    printbutton.textContent = 'remove this side';
    $(printbutton).click(function() {
    $(createButton('remove this side')).click(function() {
    $('.side').toggle();
    //$('div.content').prepend(printbutton.parentNode);
    });
    }
    };
  5. cauerego revised this gist Apr 23, 2017. 1 changed file with 10 additions and 6 deletions.
    16 changes: 10 additions & 6 deletions print-reddit.js
    Original file line number Diff line number Diff line change
    @@ -20,11 +20,15 @@ div.md {max-width: none;}
    `;
    $('head').append(`<style type="text/css">@media print {${css}}</style>`);
    var shortlink = document.querySelector('div.shortlink');
    var printdiv = document.createElement('div');
    printdiv.class = 'printbutton';
    var printbutton = document.createElement('button');
    printdiv.appendChild(printbutton);
    shortlink.parentNode.appendChild(printdiv);
    var createButton = function () {
    var div = document.createElement('div');
    div.class = 'printbutton';
    var button = document.createElement('button');
    div.appendChild(button);
    shortlink.parentNode.appendChild(div);
    return button;
    };
    var printbutton = createButton();
    var nextDelay = 0;
    let itemsOpenned = 0;
    var openSelector = function(sel, delay) {
    @@ -65,7 +69,7 @@ div.md {max-width: none;}
    printbutton.textContent = 'remove this side';
    $(printbutton).click(function() {
    $('.side').toggle();
    //$('div.content').prepend(printdiv);
    //$('div.content').prepend(printbutton.parentNode);
    });
    }
    };
  6. cauerego revised this gist Apr 23, 2017. 1 changed file with 30 additions and 24 deletions.
    54 changes: 30 additions & 24 deletions print-reddit.js
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,13 @@
    '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 printdiv = document.createElement('div');
    printdiv.class = 'printbutton';
    @@ -32,35 +39,34 @@
    }
    });
    };
    printbutton.textContent = 'format for printing';
    $(printbutton).click(formatForPrinting);
    $('.commentarea').addClass('print');
    formatForPrinting();
    function formatForPrinting () {
    $('.commentarea').addClass('print');
    $('head').append(`
    <style type="text/css">
    .side {float: none;}
    #siteTable .link.self {margin-right: 0;}
    .commentarea.print div {margin-right: 0 !important;}
    div.md {max-width: none;}
    </style>
    `);
    printbutton.textContent = 'open all comments';
    $(printbutton).click(openAllComments);
    printbutton.textContent = 'format for printing';
    $(printbutton).click(function() {
    $('head').append(`<style type="text/css">${css}</style>`);
    openAllComments();
    });
    }
    function openAllComments () {
    printbutton.disabled = true;
    openSelector('span.morecomments a.button', 500);
    openSelector('div.collapsed>div>p>a.expand', 100);
    nextDelay += 69;
    setTimeout(function() {
    printbutton.textContent = 'remove this side';
    printbutton.disabled = false;
    }, nextDelay);
    $(printbutton).click(removeThisSide);
    printbutton.textContent = 'open all comments';
    $(printbutton).click(function() {
    printbutton.disabled = true;
    openSelector('span.morecomments a.button', 500);
    openSelector('div.collapsed>div>p>a.expand', 100);
    nextDelay += 69;
    setTimeout(function() {
    removeThisSide();
    printbutton.disabled = false;
    }, nextDelay);
    });
    }
    function removeThisSide () {
    $('.side').toggle();
    //$('div.content').prepend(printdiv);
    printbutton.textContent = 'remove this side';
    $(printbutton).click(function() {
    $('.side').toggle();
    //$('div.content').prepend(printdiv);
    });
    }
    };

  7. cauerego revised this gist Apr 23, 2017. 1 changed file with 42 additions and 28 deletions.
    70 changes: 42 additions & 28 deletions print-reddit.js
    Original file line number Diff line number Diff line change
    @@ -6,48 +6,62 @@
    // @description create a button in reddit to prepare page for printing (or saving) in a better reading streamable format
    // @author [email protected]
    // @grant none
    // @todo use css instead of javascript to format
    // ==/UserScript==

    (function() {
    'use strict';

    var start = function() {
    var delay = 100; //ms

    var shortlink = document.querySelector('div.shortlink');
    var printdiv = document.createElement('div');
    printdiv.class = 'printbutton';
    var printbutton = document.createElement('button');
    printbutton.textContent = 'open all comments';
    printdiv.appendChild(printbutton);
    shortlink.parentNode.appendChild(printdiv);
    var nextDelay = delay;
    var itemDelay = function (i, divcollapsed) {
    var aexpand = $('a.expand', divcollapsed).first();
    if (aexpand) {
    setTimeout(function (item) {
    item.click();
    }, nextDelay, aexpand);
    nextDelay += delay;
    }
    var nextDelay = 0;
    let itemsOpenned = 0;
    var openSelector = function(sel, delay) {
    $(sel).each(function (i, collapsed) {
    if (collapsed) {
    nextDelay += delay;
    setTimeout(function (item) {
    itemsOpenned += 1;
    printbutton.textContent = 'openning ' + itemsOpenned;
    item.click();
    }, nextDelay, collapsed);
    }
    });
    };
    $(printbutton).click(function() {
    $('span.morecomments').each(itemDelay);
    $('div.collapsed').each(itemDelay);
    printbutton.textContent = 'format for printing';
    $(printbutton).click(function() {
    $('.side').css({'float':'none'});
    $('#siteTable .link.self').css({'margin-right':0});
    $('.commentarea .comment').attr('style', function(i,s){ return s + 'margin-right: 0 !important;';});
    $('div.md').css({'max-width':'none'});
    printbutton.textContent = 'format for printing';
    $(printbutton).click(formatForPrinting);
    function formatForPrinting () {
    $('.commentarea').addClass('print');
    $('head').append(`
    <style type="text/css">
    .side {float: none;}
    #siteTable .link.self {margin-right: 0;}
    .commentarea.print div {margin-right: 0 !important;}
    div.md {max-width: none;}
    </style>
    `);
    printbutton.textContent = 'open all comments';
    $(printbutton).click(openAllComments);
    }
    function openAllComments () {
    printbutton.disabled = true;
    openSelector('span.morecomments a.button', 500);
    openSelector('div.collapsed>div>p>a.expand', 100);
    nextDelay += 69;
    setTimeout(function() {
    printbutton.textContent = 'remove this side';
    $(printbutton).click(function() {
    $('.side').toggle();
    $('div.content').prepend(printdiv);
    });
    });
    });
    printbutton.disabled = false;
    }, nextDelay);
    $(printbutton).click(removeThisSide);
    }
    function removeThisSide () {
    $('.side').toggle();
    //$('div.content').prepend(printdiv);
    }
    };

    var script = document.createElement('script');
  8. cauerego revised this gist Apr 23, 2017. 1 changed file with 6 additions and 12 deletions.
    18 changes: 6 additions & 12 deletions print-reddit.js
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@
    (function() {
    'use strict';

    var start = function(){
    var start = function() {
    var delay = 100; //ms

    var shortlink = document.querySelector('div.shortlink');
    @@ -32,25 +32,19 @@
    nextDelay += delay;
    }
    };
    $(printbutton).click(function(){
    $(printbutton).click(function() {
    $('span.morecomments').each(itemDelay);
    $('div.collapsed').each(itemDelay);
    printbutton.textContent = 'format for printing';
    $(printbutton).click(function(){
    $(printbutton).click(function() {
    $('.side').css({'float':'none'});
    $('.nestedlisting>.comment').css({'margin-right':0});
    $('#siteTable .linkflair').css({'margin-right':0});
    $('#siteTable .link.self').css({'margin-right':0});
    $('.commentarea .comment').attr('style', function(i,s){ return s + 'margin-right: 0 !important;';});
    $('div.md').css({'max-width':'none'});
    printbutton.textContent = 'remove this side';
    $(printbutton).click(function(){
    $(printbutton).click(function() {
    $('.side').toggle();
    $('div.content').prepend(printdiv);
    printbutton.textContent = 'back to normal';
    $(printbutton).click(function(){
    $('.side').toggle();
    printdiv.remove();
    start();
    });
    });
    });
    });
  9. cauerego revised this gist Apr 23, 2017. 1 changed file with 39 additions and 29 deletions.
    68 changes: 39 additions & 29 deletions print-reddit.js
    Original file line number Diff line number Diff line change
    @@ -5,50 +5,60 @@
    // @version 0.1
    // @description create a button in reddit to prepare page for printing (or saving) in a better reading streamable format
    // @author [email protected]
    // @require https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.18.2/babel.js
    // @require https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/6.16.0/polyfill.js
    // @grant none
    // @todo use css instead of javascript to format
    // ==/UserScript==

    /* jshint ignore:start */
    var inline_src = (<><![CDATA[
    /* jshint ignore:end */
    /* jshint esnext: false */
    /* jshint esversion: 6 */
    (function() {
    'use strict';

    const delay = 200; //ms
    var start = function(){
    var delay = 100; //ms

    let start = function(){
    let shortlink = document.querySelector('div.shortlink');
    let printdiv = document.createElement('div');
    var shortlink = document.querySelector('div.shortlink');
    var printdiv = document.createElement('div');
    printdiv.class = 'printbutton';
    let printbutton = document.createElement('button');
    printbutton.value = 'format for printing';
    var printbutton = document.createElement('button');
    printbutton.textContent = 'open all comments';
    printdiv.appendChild(printbutton);
    shortlink.parentNode.appendChild(printdiv);
    let nextDelay = delay;
    let itemDelay = function (item) {
    setTimeout(function (item) {
    item.click();
    var nextDelay = delay;
    var itemDelay = function (i, divcollapsed) {
    var aexpand = $('a.expand', divcollapsed).first();
    if (aexpand) {
    setTimeout(function (item) {
    item.click();
    }, nextDelay, aexpand);
    nextDelay += delay;
    }, nextDelay);
    }
    };
    $(printbutton).click(function(){
    $('.side').css({'float':''});
    $('#siteTable .linkflair').css({'margin-right':0});
    $('div.md').css({'max-width':'none'});
    $('span.morecomments').each(itemDelay);
    $('div.collapsed a.expand').each(itemDelay);
    $('div.collapsed').each(itemDelay);
    printbutton.textContent = 'format for printing';
    $(printbutton).click(function(){
    $('.side').css({'float':'none'});
    $('.nestedlisting>.comment').css({'margin-right':0});
    $('#siteTable .linkflair').css({'margin-right':0});
    $('div.md').css({'max-width':'none'});
    printbutton.textContent = 'remove this side';
    $(printbutton).click(function(){
    $('.side').toggle();
    $('div.content').prepend(printdiv);
    printbutton.textContent = 'back to normal';
    $(printbutton).click(function(){
    $('.side').toggle();
    printdiv.remove();
    start();
    });
    });
    });
    });
    };

    let script = document.createElement('script');
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.textContent = `(${start})()`;
    script.textContent = '(' + start + ')()';
    document.body.appendChild(script);

    /* jshint ignore:start */
    ]]></>).toString();
    var c = Babel.transform(inline_src, { presets: [ "es2015", "es2016" ] });
    eval(c.code);
    /* jshint ignore:end */
    })();
  10. cauerego revised this gist Apr 23, 2017. 1 changed file with 39 additions and 24 deletions.
    63 changes: 39 additions & 24 deletions print-reddit.js
    Original file line number Diff line number Diff line change
    @@ -5,35 +5,50 @@
    // @version 0.1
    // @description create a button in reddit to prepare page for printing (or saving) in a better reading streamable format
    // @author [email protected]
    // @require https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.18.2/babel.js
    // @require https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/6.16.0/polyfill.js
    // @grant none
    // ==/UserScript==

    (function(){
    'use strict';
    /* jshint ignore:start */
    var inline_src = (<><![CDATA[
    /* jshint ignore:end */
    /* jshint esnext: false */
    /* jshint esversion: 6 */

    const delay = 200 //ms
    const delay = 200; //ms

    let start = function(){
    let shortlink = document.querySelector('div.shortlink')
    let printdiv = document.createElement('div')
    printdiv.class = 'printbutton'
    let printbutton = document.createElement('button')
    printbutton.value = 'format for printing'
    printdiv.appendChild(printbutton)
    shortlink.parentNode.appendChild(printdiv)
    let nextDelay = delay
    let itemDelay = (item => {setTimeout(item => item.click(), nextDelay); nextDelay += delay})
    let shortlink = document.querySelector('div.shortlink');
    let printdiv = document.createElement('div');
    printdiv.class = 'printbutton';
    let printbutton = document.createElement('button');
    printbutton.value = 'format for printing';
    printdiv.appendChild(printbutton);
    shortlink.parentNode.appendChild(printdiv);
    let nextDelay = delay;
    let itemDelay = function (item) {
    setTimeout(function (item) {
    item.click();
    nextDelay += delay;
    }, nextDelay);
    };
    $(printbutton).click(function(){
    $('.side').css({'float':''})
    $('#siteTable .linkflair').css({'margin-right':0})
    $('div.md').css({'max-width':'none'})
    $('span.morecomments').each(itemDelay)
    $('div.collapsed a.expand').each(itemDelay)
    })
    }
    $('.side').css({'float':''});
    $('#siteTable .linkflair').css({'margin-right':0});
    $('div.md').css({'max-width':'none'});
    $('span.morecomments').each(itemDelay);
    $('div.collapsed a.expand').each(itemDelay);
    });
    };

    let script = document.createElement('script')
    script.type = 'text/javascript'
    script.textContent = `(${start})()`
    document.body.appendChild(script)
    })()
    let script = document.createElement('script');
    script.type = 'text/javascript';
    script.textContent = `(${start})()`;
    document.body.appendChild(script);

    /* jshint ignore:start */
    ]]></>).toString();
    var c = Babel.transform(inline_src, { presets: [ "es2015", "es2016" ] });
    eval(c.code);
    /* jshint ignore:end */
  11. cauerego created this gist Apr 23, 2017.
    39 changes: 39 additions & 0 deletions print-reddit.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    // ==UserScript==
    // @name print reddit
    // @namespace reddit
    // @include http*//*reddit.com/*
    // @version 0.1
    // @description create a button in reddit to prepare page for printing (or saving) in a better reading streamable format
    // @author [email protected]
    // @grant none
    // ==/UserScript==

    (function(){
    'use strict';

    const delay = 200 //ms

    let start = function(){
    let shortlink = document.querySelector('div.shortlink')
    let printdiv = document.createElement('div')
    printdiv.class = 'printbutton'
    let printbutton = document.createElement('button')
    printbutton.value = 'format for printing'
    printdiv.appendChild(printbutton)
    shortlink.parentNode.appendChild(printdiv)
    let nextDelay = delay
    let itemDelay = (item => {setTimeout(item => item.click(), nextDelay); nextDelay += delay})
    $(printbutton).click(function(){
    $('.side').css({'float':''})
    $('#siteTable .linkflair').css({'margin-right':0})
    $('div.md').css({'max-width':'none'})
    $('span.morecomments').each(itemDelay)
    $('div.collapsed a.expand').each(itemDelay)
    })
    }

    let script = document.createElement('script')
    script.type = 'text/javascript'
    script.textContent = `(${start})()`
    document.body.appendChild(script)
    })()