gavin19

Reddit extras

Jul 7th, 2012
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name            Reddit Extras
  3. // @namespace       http://example.com
  4. // @author          gavin19
  5. // @description     Additional functionality for reddit users
  6. // @include         http://*.reddit.com/*
  7. // @include         https://*.reddit.com/*
  8. // @exclude         http://*.reddit.com/*/*/comments/*
  9. // @exclude         http://*.reddit.com/*/*/comments/*
  10. // @exclude         http://*.reddit.com/user/*
  11. // @exclude         http://*.reddit.com/user/*
  12. // @version         0.1
  13. // ==/UserScript==
  14. (function () {
  15.  
  16.     var rExt = {
  17.         hideDelay: 2000, /* Delay in ms for hiding posts. Default is 2000 (2 secs) */
  18.         expSelfDelay: 3000, /* Delay in ms for expanding self posts. Default is 3000 (3 secs) */
  19.         expVidsDelay: 4000, /* Delay in ms for expanding self posts. Default is 4000 (4 secs) */
  20.         expImgsDelay: 3000, /* Delay in ms for expanding image posts. Default is 3000 (3 secs) */
  21.         offLeft: 0,
  22.         offTop: 0,
  23.         nsfwState: 'OFF',
  24.         selfState: 'OFF',
  25.         expSelf: 'OFF',
  26.         expVids: 'OFF',
  27.         expImgs: 'OFF',
  28.         createMenu: function () {
  29.             var rExtRule = document.createElement('hr'),
  30.                 rExtClose = document.createElement('a'),
  31.                 rExtBox = document.createElement('div'),
  32.                 rExtLink = document.createElement('a'),
  33.                 rExtLi = document.createElement('li'),
  34.                 rExtRow = document.createElement('span'),
  35.                 rExtRowLink1 = document.createElement('a'),
  36.                 rExtRowLink2 = document.createElement('a'),
  37.                 rExtRowLink3 = document.createElement('a'),
  38.                 rExtRowLink4 = document.createElement('a'),
  39.                 rExtRowLink5 = document.createElement('a'),
  40.                 rExtRowLink6 = document.createElement('a'),
  41.                 rExtRowLink7 = document.createElement('a'),
  42.                 content = document.querySelector('div.content'),
  43.                 isRES = document.querySelector('#RESShortcutsViewport'),
  44.                 notifyDiv = document.createElement('div'),
  45.                 f, g, r, posts, tabmenu, menuHandler,
  46.                 nsfwListener = function (event) {
  47.                     if (event.target.classList && event.target.classList.contains('linklisting')) {
  48.                         rExt.setMode('nsfwMode', event.target.querySelectorAll('.link:not(.RESFiltered)'));
  49.                     }
  50.                 },
  51.                 selfListener = function (event) {
  52.                     if (event.target.classList && event.target.classList.contains('linklisting')) {
  53.                         rExt.setMode('selfMode', event.target.querySelectorAll('.link:not(.RESFiltered)'));
  54.                     }
  55.                 },
  56.                 selfAutoListener = function (event) {
  57.                     if (event.target.classList && event.target.classList.contains('linklisting')) {
  58.                         rExt.expExp('selfMode', event.target.querySelectorAll('.link:not(.RESFiltered) .selftext.collapsed'));
  59.                     }
  60.                 },
  61.                 vidsAutoListener = function (event) {
  62.                     if (event.target.classList && event.target.classList.contains('linklisting')) {
  63.                         rExt.expExp('vidsMode', event.target.querySelectorAll('.link:not(.RESFiltered) .video.collapsed'));
  64.                     }
  65.                 },
  66.                 imgsAutoListener = function (event) {
  67.                     if (event.target.classList && event.target.classList.contains('linklisting')) {
  68.                         rExt.expExp(event.target.querySelectorAll('.link:not(.RESFiltered) .image.collapsed'));
  69.                     }
  70.                 };
  71.             !!isRES ? tabmenu = document.querySelector('#RESStaticShortcuts') : tabmenu = document.querySelector('.sr-bar');
  72.  
  73.             notifyDiv.id = 'rExtNotify';
  74.             notifyDiv.style.visibility = 'hidden';
  75.             document.body.appendChild(notifyDiv);
  76.             rExtClose.id = 'rExtClose';
  77.             rExtClose.textContent = 'X';
  78.             rExtBox.id = 'rExtBox';
  79.             rExtBox.textContent = 'reddit tools';
  80.             rExtBox.style.visibility = 'hidden';
  81.             rExtBox.appendChild(rExtClose);
  82.             rExtBox.appendChild(rExtRule);
  83.             rExtLink.href = 'javascript:void(0)';
  84.             rExtLink.textContent = '  - EXTRA';
  85.             rExtLink.id = 'rExtLink';
  86.             rExtRow.classList.add('rExtRow');
  87.  
  88.             rExtRowLink1.textContent = "1. Hide all clicked posts";
  89.             rExtRowLink1.classList.add('rExtRowLink');
  90.             rExtRowLink1.id = 'rExtRowLink1';
  91.             rExtBox.appendChild(rExtRow);
  92.             rExtRow.appendChild(rExtRowLink1);
  93.  
  94.             rExtRowLink2.textContent = "2. NSFW mode: " + rExt.nsfwState;
  95.             rExtRowLink2.classList.add('rExtRowLink');
  96.             rExtRowLink2.id = 'rExtRowLink2';
  97.             rExtBox.appendChild(rExtRow);
  98.             rExtRow.appendChild(rExtRowLink2);
  99.  
  100.             rExtRowLink3.textContent = "3. Self-post mode: " + rExt.selfState;
  101.             rExtRowLink3.classList.add('rExtRowLink');
  102.             rExtRowLink3.id = 'rExtRowLink3';
  103.             rExtBox.appendChild(rExtRow);
  104.             rExtRow.appendChild(rExtRowLink3);
  105.            
  106.             rExtRowLink4.textContent = "4. Sort posts by comments";
  107.             rExtRowLink4.classList.add('rExtRowLink');
  108.             rExtRowLink4.id = 'rExtRowLink4';
  109.             rExtBox.appendChild(rExtRow);
  110.             rExtRow.appendChild(rExtRowLink4);
  111.  
  112.             rExtRowLink5.textContent = "5. Auto-expand self posts: " + rExt.expSelf;
  113.             rExtRowLink5.classList.add('rExtRowLink');
  114.             rExtRowLink5.id = 'rExtRowLink5';
  115.             rExtBox.appendChild(rExtRow);
  116.             rExtRow.appendChild(rExtRowLink5);
  117.  
  118.             rExtRowLink6.textContent = "6. Auto-expand video posts: " + rExt.expVids;
  119.             rExtRowLink6.classList.add('rExtRowLink');
  120.             rExtRowLink6.id = 'rExtRowLink6';
  121.             rExtBox.appendChild(rExtRow);
  122.             rExtRow.appendChild(rExtRowLink6);
  123.  
  124.             if (!!isRES) {
  125.                 rExtRowLink7.textContent = "7. Auto-expand image posts: " + rExt.expImgs;
  126.                 rExtRowLink7.classList.add('rExtRowLink');
  127.                 rExtRowLink7.id = 'rExtRowLink7';
  128.                 rExtBox.appendChild(rExtRow);
  129.                 rExtRow.appendChild(rExtRowLink7);
  130.             }
  131.            
  132.             document.body.appendChild(rExtBox);
  133.             tabmenu.appendChild(rExtLink);
  134.  
  135.             f = document.querySelector('#rExtBox');
  136.             g = document.querySelector('#rExtLink');
  137.             rExt.offLeft = g.offsetLeft + 'px';
  138.             rExt.offTop = (tabmenu.offsetTop + 20) + 'px';
  139.  
  140.             /* Listeners */
  141.             menuHandler = function (e) {
  142.                 e.preventDefault();
  143.                 e.stopPropagation();
  144.                 if (window.getComputedStyle(f, null).getPropertyValue('visibility') === 'hidden') {
  145.                     f.classList.remove('hidden');
  146.                     f.classList.add('visible');
  147.                 } else {
  148.                     f.classList.add('hidden');
  149.                     f.classList.remove('visible');
  150.                 }
  151.             };
  152.             rExtLink.addEventListener('click', menuHandler);
  153.             rExtClose.addEventListener('click', menuHandler);
  154.             rExtRowLink1.addEventListener('click', function (e) {
  155.                 e.preventDefault();
  156.                 e.stopPropagation();
  157.                 f.classList.add('hidden');
  158.                 f.classList.remove('visible');
  159.                 rExt.hideAllClicked();
  160.             });
  161.             rExtRowLink2.addEventListener('click', function (e) {
  162.                 e.preventDefault();
  163.                 e.stopPropagation();
  164.                 f.classList.add('hidden');
  165.                 f.classList.remove('visible');
  166.                 posts = document.querySelectorAll('.linklisting .link:not(.RESFiltered)');
  167.                 if (rExt.nsfwState === 'OFF') {
  168.                     rExt.nsfwState = 'ON';
  169.                     rExt.setMode('nsfwMode', posts);
  170.                     content.addEventListener('DOMNodeInserted', nsfwListener);
  171.                     document.querySelector('#rExtRowLink2').textContent = "2. NSFW mode: ON";
  172.                 } else {
  173.                     rExt.nsfwState = 'OFF';
  174.                     rExt.setMode('nsfwMode', posts);
  175.                     content.removeEventListener('DOMNodeInserted', selfListener);
  176.                     document.querySelector('#rExtRowLink2').textContent = "2. NSFW mode: OFF";
  177.                 }
  178.             });
  179.             rExtRowLink3.addEventListener('click', function (e) {
  180.                 e.preventDefault();
  181.                 e.stopPropagation();
  182.                 f.classList.add('hidden');
  183.                 f.classList.remove('visible');
  184.                 posts = document.querySelectorAll('.linklisting .link:not(.RESFiltered)');
  185.                 if (rExt.selfState === 'OFF') {
  186.                     rExt.selfState = 'ON';
  187.                     rExt.setMode('selfMode', posts);
  188.                     content.addEventListener('DOMNodeInserted', selfListener);
  189.                     document.querySelector('#rExtRowLink3').textContent = "3. Self mode: ON";
  190.                 } else {
  191.                     rExt.selfState = 'OFF';
  192.                     rExt.setMode('selfMode', posts);
  193.                     content.removeEventListener('DOMNodeInserted', selfListener);
  194.                     document.querySelector('#rExtRowLink3').textContent = "3. Self mode: OFF";
  195.                 }
  196.             });
  197.             rExtRowLink4.addEventListener('click', function (e) {
  198.                 e.preventDefault();
  199.                 e.stopPropagation();
  200.                 f.classList.add('hidden');
  201.                 f.classList.remove('visible');
  202.                 rExt.sortByComments(e);
  203.             });
  204.             rExtRowLink5.addEventListener('click', function (e) {
  205.                 e.preventDefault();
  206.                 e.stopPropagation();
  207.                 f.classList.add('hidden');
  208.                 f.classList.remove('visible');
  209.                 if (rExt.expSelf === 'OFF') {
  210.                     rExt.expSelf = 'ON';
  211.                     rExt.expExp('selfMode', document.querySelectorAll('.linklisting .link .collapsed.selftext'), 'self');
  212.                     content.addEventListener('DOMNodeInserted', selfAutoListener);
  213.                     document.querySelector('#rExtRowLink5').textContent = "5. Auto expand self posts: ON";
  214.                 } else {
  215.                     rExt.expSelf = 'OFF';
  216.                     content.removeEventListener('DOMNodeInserted', selfAutoListener);
  217.                     document.querySelector('#rExtRowLink5').textContent = "5. Auto expand self posts: OFF";
  218.                 }
  219.             });
  220.             rExtRowLink6.addEventListener('click', function (e) {
  221.                 e.preventDefault();
  222.                 e.stopPropagation();
  223.                 f.classList.add('hidden');
  224.                 f.classList.remove('visible');
  225.                 if (rExt.expVids === 'OFF') {
  226.                     rExt.expVids = 'ON';
  227.                     rExt.expExp('vidsMode', document.querySelectorAll('.linklisting .link .collapsed.video'), 'video');
  228.                     content.addEventListener('DOMNodeInserted', vidsAutoListener);
  229.                     document.querySelector('#rExtRowLink6').textContent = "6. Auto expand video posts: ON";
  230.                 } else {
  231.                     rExt.expVids = 'OFF';
  232.                     content.removeEventListener('DOMNodeInserted', vidsAutoListener);
  233.                     document.querySelector('#rExtRowLink6').textContent = "6. Auto expand video posts: OFF";
  234.                 }
  235.             });
  236.             if (!!isRES) {
  237.                 rExtRowLink7.addEventListener('click', function (e) {
  238.                     e.preventDefault();
  239.                     e.stopPropagation();
  240.                     f.classList.add('hidden');
  241.                     f.classList.remove('visible');
  242.                     if (rExt.expImgs === 'OFF') {
  243.                         rExt.expImgs = 'ON';
  244.                         rExt.expExp('imgMode', document.querySelectorAll('.linklisting .link:not(.RESFiltered) .collapsed.image'), 'images');
  245.                         content.addEventListener('DOMNodeInserted', imgsAutoListener);
  246.                         document.querySelector('#rExtRowLink7').textContent = "7. Auto expand image posts: ON";
  247.                     } else {
  248.                         rExt.expImgs = 'OFF';
  249.                         content.removeEventListener('DOMNodeInserted', imgsAutoListener);
  250.                         document.querySelector('#rExtRowLink7').textContent = "7. Auto expand image posts: OFF";
  251.                     }
  252.                 });
  253.             }
  254.         },
  255.         notify: function (type) {
  256.             var notifyTimer,
  257.                 notifyDiv = document.querySelector('#rExtNotify'),
  258.                 clearNotify = function () {
  259.                     document.querySelector('#rExtNotify').classList.add('hidden');
  260.                     document.querySelector('#rExtNotify').classList.remove('visible');
  261.                     clearInterval(notifyTimer);
  262.                 };
  263.             notifyDiv.textContent = 'All ' + type + ' posts expanded';
  264.             notifyDiv.classList.remove('hidden');
  265.             notifyDiv.classList.add('visible');
  266.             notifyTimer = setInterval(clearNotify, 5000);
  267.  
  268.         },
  269.         expExp: function (mode, posts, type) {
  270.             var cEvt = document.createEvent('MouseEvents'),
  271.                 i = 0,
  272.                 timer,
  273.                 expPosts;
  274.             cEvt.initMouseEvent('click', false, true, window, 0, 1, 1, 1, 1, false, false, false, false, 0, null);
  275.             expPosts = function () {
  276.                 if (posts[i]) {
  277.                     if (type === 'self') {
  278.                         posts[i].onclick();
  279.                     } else {
  280.                         posts[i].dispatchEvent(cEvt);
  281.                     }
  282.                     i += 1;
  283.                 } else {
  284.                     rExt.notify(type);
  285.                     clearInterval(timer);
  286.                 }
  287.             };
  288.             if (mode === 'selfVids') {
  289.                 timer = setInterval(expPosts(), rExt.expSelfDelay);
  290.             } else if (mode === 'vidsMode') {
  291.                 timer = setInterval(expPosts, rExt.expVidsDelay);
  292.             } else {
  293.                 timer = setInterval(expPosts, rExt.expImgsDelay);
  294.             }
  295.         },
  296.         hideAllClicked: function () {
  297.             var i = 0,
  298.                 hidePosts,
  299.                 timer,
  300.                 cEvt = document.createEvent('MouseEvents'),
  301.                 posts = document.querySelectorAll('.linklisting .click');
  302.             cEvt.initMouseEvent('click', false, true, window, 0, 1, 1, 1, 1, false, false, false, false, 0, null);
  303.             hidePosts = function () {
  304.                 if (posts[i]) {
  305.                     posts[i].parentNode.parentNode.querySelector('.hide-button a').dispatchEvent(cEvt);
  306.                     i += 1;
  307.                 } else {
  308.                     clearInterval(timer);
  309.                 }
  310.             };
  311.             timer = setInterval(hidePosts, rExt.hideDelay);
  312.         },
  313.         setMode: function (mode, posts) {
  314.             var i, l, e, t,
  315.                 nsfwState = rExt.nsfwState,
  316.                 selfState = rExt.selfState;
  317.             if ((mode === 'nsfwMode' && nsfwState === 'ON') || (mode === 'selfMode' && selfState === 'ON')) {
  318.                 for (i = 0, l = posts.length; i < l; i += 1) {
  319.                     if (mode === 'nsfwMode' && !posts[i].classList.contains('over18')) {
  320.                         posts[i].style.display = 'none';
  321.                     } else if (mode === 'nsfwMode' && posts[i].classList.contains('over18')) {
  322.                         posts[i].classList.add('rExtNSFW');
  323.                     }
  324.                     if (mode === 'selfMode' && !posts[i].classList.contains('self')) {
  325.                         posts[i].style.display = 'none';
  326.                     } else if (mode === 'selfMode' && posts[i].classList.contains('self')) {
  327.                         posts[i].classList.add('rExtSELF');
  328.                     }
  329.                 }
  330.             } else {
  331.                 for (i = 0, l = posts.length; i < l; i += 1) {
  332.                     if (mode === 'nsfwMode' && !posts[i].classList.contains('over18')) {
  333.                         posts[i].style.display = 'block';
  334.                     } else if (mode === 'nsfwMode' && posts[i].classList.contains('over18')) {
  335.                         posts[i].classList.remove('rExtNSFW');
  336.                     }
  337.                     if (mode === 'selfMode' && !posts[i].classList.contains('self')) {
  338.                         posts[i].style.display = 'block';
  339.                     } else if (mode === 'selfMode' && posts[i].classList.contains('self')) {
  340.                         posts[i].classList.remove('rExtSELF');
  341.                     }
  342.                 }
  343.             }
  344.         },
  345.         sortByComments: function () {
  346.             var i, l,
  347.                 sortTop,
  348.                 posts = document.querySelectorAll('.linklisting .thing'),
  349.                 clears = document.querySelectorAll('.linklisting .clearleft'),
  350.                 postsActive = [],
  351.                 postsNull = [],
  352.                 postClear,
  353.                 postsSorted,
  354.                 linkListing,
  355.                 currPost = document.querySelector('.keyHighlight'),
  356.                 pageMarkers = document.querySelectorAll('.NERPageMarker');
  357.             posts = Array.prototype.slice.call(posts, 0);
  358.             posts.forEach(function (e, i) {
  359.                 if (e.querySelector('.comments').textContent === 'comment') {
  360.                     postsNull.push(e);
  361.                 } else {
  362.                     postsActive.push(e);
  363.                 }
  364.             });
  365.             sortTop = function (a, b) {
  366.                 return +(b.querySelector('.comments').textContent.split(' ')[0]) - +(a.querySelector('.comments').textContent.split(' ')[0]);
  367.             };
  368.             postsSorted = postsActive.sort(sortTop);
  369.             if (!!currPost) {
  370.                 currPost.classList.add('keyHighlight');
  371.             }
  372.             if (!!pageMarkers) {
  373.                 for (i = 0, l = pageMarkers.length; i < l; i += 1) {
  374.                     pageMarkers[i].setAttribute('style', 'display:none!important');
  375.                 }
  376.             }
  377.             for (i = 0, l = clears.length; i < l; i += 1) {
  378.                 clears[i].parentNode.removeChild(clears[i]);
  379.             }
  380.             for (i = 0, l = postsSorted.length; i < l; i += 1) {
  381.                 postClear = document.createElement('div');
  382.                 postClear.classList.add('clearleft');
  383.                 postsSorted[i].parentNode.appendChild(postsSorted[i]);
  384.                 postsSorted[i].parentNode.appendChild(postClear);
  385.             }
  386.             for (i = 0, l = postsNull.length; i < l; i += 1) {
  387.                 postClear = document.createElement('div');
  388.                 postClear.classList.add('clearleft');
  389.                 postsNull[i].parentNode.appendChild(postsNull[i]);
  390.                 postsNull[i].parentNode.appendChild(postClear);
  391.             }
  392.             linkListing = document.querySelectorAll('.linklisting .link');
  393.             for (i = 0, l = linkListing.length; i < l; i += 1) {
  394.                 linkListing[i].querySelector('.entry').classList.remove('keyHighlight');
  395.                 if (linkListing[i].querySelector('.thumbnail')) {
  396.                     linkListing[i].querySelector('.thumbnail').setAttribute('style', 'margin-bottom: 8px!important');
  397.                 }
  398.             }
  399.         },
  400.         addStyle: function (sheet) {
  401.             var style = document.createElement('style');
  402.             style.type = 'text/css';
  403.             style.textContent = sheet;
  404.             document.querySelector('head').appendChild(style);
  405.         },
  406.         init: function () {
  407.             var css = '';
  408.             this.createMenu();
  409.             css += '\
  410.         #rExtBox{\
  411.             position:absolute;\
  412.             z-index: 999999999;\
  413.             left:' + this.offLeft + ';\
  414.             top:' + this.offTop + ';\
  415.             text-align:center;\
  416.             font-weight:bold;\
  417.             padding-top:2px;\
  418.             width:185px;\
  419.             background-color:aliceBlue;\
  420.             color:#000;\
  421.             border:2px solid navy;\
  422.             border-radius:5px;\
  423.             -moz-box-shadow:1px 1px 1px cornflowerblue;\
  424.             -webkit-box-shadow:1px 1px 1px cornflowerblue;\
  425.             box-shadow:1px 1px 1px cornflowerblue;\
  426.             visibility: hidden;\
  427.             opacity: 0;\
  428.             -webkit-transition: visibility 0s .4s, opacity .4s linear;\
  429.         }\
  430.         .visible {\
  431.             visibility: visible!important;\
  432.             opacity: 1!important;\
  433.             -webkit-transition: opacity .4s linear!important;\
  434.         }\
  435.         .hidden {\
  436.             visibility: hidden!important;\
  437.             opacity: 0!important;\
  438.             -webkit-transition: visibility 0s .4s, opacity .4s linear!important;\
  439.         }\
  440.         #rExtNotify{\
  441.             background-color: #f0f3fc;\
  442.             border: 2px solid cornflowerblue;\
  443.             border-radius: 3px;\
  444.             color: navy;\
  445.             font-size: 24px;\
  446.             padding: 15px 10px!important;\
  447.             width: 300px;\
  448.             height: 30px;\
  449.             text-align: center;\
  450.             position: fixed;\
  451.             top: 50px;\
  452.             right: 10px;\
  453.             z-index: 999999999;\
  454.             -moz-box-shadow:1px 1px 1px cornflowerblue;\
  455.             -webkit-box-shadow:1px 1px 1px cornflowerblue;\
  456.             box-shadow:1px 1px 1px cornflowerblue;\
  457.             visibility: hidden;\
  458.             opacity: 0;\
  459.             -webkit-transition: visibility 0s .4s, opacity .4s linear;\
  460.         }\
  461.         #rExtClose{\
  462.             display: inline-block;\
  463.             height: auto;\
  464.             line-height: 10px;\
  465.             background-color: #fff;\
  466.             border: 1px solid cornflowerblue;\
  467.             float: right;\
  468.             padding:1px!important;\
  469.             margin-right:4px!important;\
  470.             cursor:pointer;\
  471.         }\
  472.         #rExtClose:hover{cursor:crosshair;}\
  473.         .rExtRowLink{\
  474.             font-weight:normal;\
  475.             display:block!important;\
  476.             width:auto!important;\
  477.             height:auto!important;\
  478.             background-color:white!important;\
  479.             font-size:9px!important;\
  480.             line-height:11px!important;\
  481.             border:1px solid navy!important;\
  482.             border-radius:2px!important;\
  483.             margin:3px!important;\
  484.             padding:1px!important; \
  485.             text-align:left!important;\
  486.             color:#000!important;\
  487.             cursor:pointer;\
  488.         }\
  489.         .rExtRowLink:hover{\
  490.             background-color:cornflowerblue!important;\
  491.             color:white!important;\
  492.         }';
  493.             this.addStyle(css);
  494.             //this.debug();
  495.         }
  496.     };
  497.     if (document.body.classList.length && document.body.classList.contains('listing-page')) {
  498.         setTimeout(function () {
  499.             rExt.init();
  500.         }, 1000);
  501.     }
  502. }());
Add Comment
Please, Sign In to add comment