Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Reddit Extras
- // @namespace http://example.com
- // @author gavin19
- // @description Additional functionality for reddit users
- // @include http://*.reddit.com/*
- // @include https://*.reddit.com/*
- // @exclude http://*.reddit.com/*/*/comments/*
- // @exclude http://*.reddit.com/*/*/comments/*
- // @exclude http://*.reddit.com/user/*
- // @exclude http://*.reddit.com/user/*
- // @version 0.1
- // ==/UserScript==
- (function () {
- var rExt = {
- hideDelay: 2000, /* Delay in ms for hiding posts. Default is 2000 (2 secs) */
- expSelfDelay: 3000, /* Delay in ms for expanding self posts. Default is 3000 (3 secs) */
- expVidsDelay: 4000, /* Delay in ms for expanding self posts. Default is 4000 (4 secs) */
- expImgsDelay: 3000, /* Delay in ms for expanding image posts. Default is 3000 (3 secs) */
- offLeft: 0,
- offTop: 0,
- nsfwState: 'OFF',
- selfState: 'OFF',
- expSelf: 'OFF',
- expVids: 'OFF',
- expImgs: 'OFF',
- createMenu: function () {
- var rExtRule = document.createElement('hr'),
- rExtClose = document.createElement('a'),
- rExtBox = document.createElement('div'),
- rExtLink = document.createElement('a'),
- rExtLi = document.createElement('li'),
- rExtRow = document.createElement('span'),
- rExtRowLink1 = document.createElement('a'),
- rExtRowLink2 = document.createElement('a'),
- rExtRowLink3 = document.createElement('a'),
- rExtRowLink4 = document.createElement('a'),
- rExtRowLink5 = document.createElement('a'),
- rExtRowLink6 = document.createElement('a'),
- rExtRowLink7 = document.createElement('a'),
- content = document.querySelector('div.content'),
- isRES = document.querySelector('#RESShortcutsViewport'),
- notifyDiv = document.createElement('div'),
- f, g, r, posts, tabmenu, menuHandler,
- nsfwListener = function (event) {
- if (event.target.classList && event.target.classList.contains('linklisting')) {
- rExt.setMode('nsfwMode', event.target.querySelectorAll('.link:not(.RESFiltered)'));
- }
- },
- selfListener = function (event) {
- if (event.target.classList && event.target.classList.contains('linklisting')) {
- rExt.setMode('selfMode', event.target.querySelectorAll('.link:not(.RESFiltered)'));
- }
- },
- selfAutoListener = function (event) {
- if (event.target.classList && event.target.classList.contains('linklisting')) {
- rExt.expExp('selfMode', event.target.querySelectorAll('.link:not(.RESFiltered) .selftext.collapsed'));
- }
- },
- vidsAutoListener = function (event) {
- if (event.target.classList && event.target.classList.contains('linklisting')) {
- rExt.expExp('vidsMode', event.target.querySelectorAll('.link:not(.RESFiltered) .video.collapsed'));
- }
- },
- imgsAutoListener = function (event) {
- if (event.target.classList && event.target.classList.contains('linklisting')) {
- rExt.expExp(event.target.querySelectorAll('.link:not(.RESFiltered) .image.collapsed'));
- }
- };
- !!isRES ? tabmenu = document.querySelector('#RESStaticShortcuts') : tabmenu = document.querySelector('.sr-bar');
- notifyDiv.id = 'rExtNotify';
- notifyDiv.style.visibility = 'hidden';
- document.body.appendChild(notifyDiv);
- rExtClose.id = 'rExtClose';
- rExtClose.textContent = 'X';
- rExtBox.id = 'rExtBox';
- rExtBox.textContent = 'reddit tools';
- rExtBox.style.visibility = 'hidden';
- rExtBox.appendChild(rExtClose);
- rExtBox.appendChild(rExtRule);
- rExtLink.href = 'javascript:void(0)';
- rExtLink.textContent = ' - EXTRA';
- rExtLink.id = 'rExtLink';
- rExtRow.classList.add('rExtRow');
- rExtRowLink1.textContent = "1. Hide all clicked posts";
- rExtRowLink1.classList.add('rExtRowLink');
- rExtRowLink1.id = 'rExtRowLink1';
- rExtBox.appendChild(rExtRow);
- rExtRow.appendChild(rExtRowLink1);
- rExtRowLink2.textContent = "2. NSFW mode: " + rExt.nsfwState;
- rExtRowLink2.classList.add('rExtRowLink');
- rExtRowLink2.id = 'rExtRowLink2';
- rExtBox.appendChild(rExtRow);
- rExtRow.appendChild(rExtRowLink2);
- rExtRowLink3.textContent = "3. Self-post mode: " + rExt.selfState;
- rExtRowLink3.classList.add('rExtRowLink');
- rExtRowLink3.id = 'rExtRowLink3';
- rExtBox.appendChild(rExtRow);
- rExtRow.appendChild(rExtRowLink3);
- rExtRowLink4.textContent = "4. Sort posts by comments";
- rExtRowLink4.classList.add('rExtRowLink');
- rExtRowLink4.id = 'rExtRowLink4';
- rExtBox.appendChild(rExtRow);
- rExtRow.appendChild(rExtRowLink4);
- rExtRowLink5.textContent = "5. Auto-expand self posts: " + rExt.expSelf;
- rExtRowLink5.classList.add('rExtRowLink');
- rExtRowLink5.id = 'rExtRowLink5';
- rExtBox.appendChild(rExtRow);
- rExtRow.appendChild(rExtRowLink5);
- rExtRowLink6.textContent = "6. Auto-expand video posts: " + rExt.expVids;
- rExtRowLink6.classList.add('rExtRowLink');
- rExtRowLink6.id = 'rExtRowLink6';
- rExtBox.appendChild(rExtRow);
- rExtRow.appendChild(rExtRowLink6);
- if (!!isRES) {
- rExtRowLink7.textContent = "7. Auto-expand image posts: " + rExt.expImgs;
- rExtRowLink7.classList.add('rExtRowLink');
- rExtRowLink7.id = 'rExtRowLink7';
- rExtBox.appendChild(rExtRow);
- rExtRow.appendChild(rExtRowLink7);
- }
- document.body.appendChild(rExtBox);
- tabmenu.appendChild(rExtLink);
- f = document.querySelector('#rExtBox');
- g = document.querySelector('#rExtLink');
- rExt.offLeft = g.offsetLeft + 'px';
- rExt.offTop = (tabmenu.offsetTop + 20) + 'px';
- /* Listeners */
- menuHandler = function (e) {
- e.preventDefault();
- e.stopPropagation();
- if (window.getComputedStyle(f, null).getPropertyValue('visibility') === 'hidden') {
- f.classList.remove('hidden');
- f.classList.add('visible');
- } else {
- f.classList.add('hidden');
- f.classList.remove('visible');
- }
- };
- rExtLink.addEventListener('click', menuHandler);
- rExtClose.addEventListener('click', menuHandler);
- rExtRowLink1.addEventListener('click', function (e) {
- e.preventDefault();
- e.stopPropagation();
- f.classList.add('hidden');
- f.classList.remove('visible');
- rExt.hideAllClicked();
- });
- rExtRowLink2.addEventListener('click', function (e) {
- e.preventDefault();
- e.stopPropagation();
- f.classList.add('hidden');
- f.classList.remove('visible');
- posts = document.querySelectorAll('.linklisting .link:not(.RESFiltered)');
- if (rExt.nsfwState === 'OFF') {
- rExt.nsfwState = 'ON';
- rExt.setMode('nsfwMode', posts);
- content.addEventListener('DOMNodeInserted', nsfwListener);
- document.querySelector('#rExtRowLink2').textContent = "2. NSFW mode: ON";
- } else {
- rExt.nsfwState = 'OFF';
- rExt.setMode('nsfwMode', posts);
- content.removeEventListener('DOMNodeInserted', selfListener);
- document.querySelector('#rExtRowLink2').textContent = "2. NSFW mode: OFF";
- }
- });
- rExtRowLink3.addEventListener('click', function (e) {
- e.preventDefault();
- e.stopPropagation();
- f.classList.add('hidden');
- f.classList.remove('visible');
- posts = document.querySelectorAll('.linklisting .link:not(.RESFiltered)');
- if (rExt.selfState === 'OFF') {
- rExt.selfState = 'ON';
- rExt.setMode('selfMode', posts);
- content.addEventListener('DOMNodeInserted', selfListener);
- document.querySelector('#rExtRowLink3').textContent = "3. Self mode: ON";
- } else {
- rExt.selfState = 'OFF';
- rExt.setMode('selfMode', posts);
- content.removeEventListener('DOMNodeInserted', selfListener);
- document.querySelector('#rExtRowLink3').textContent = "3. Self mode: OFF";
- }
- });
- rExtRowLink4.addEventListener('click', function (e) {
- e.preventDefault();
- e.stopPropagation();
- f.classList.add('hidden');
- f.classList.remove('visible');
- rExt.sortByComments(e);
- });
- rExtRowLink5.addEventListener('click', function (e) {
- e.preventDefault();
- e.stopPropagation();
- f.classList.add('hidden');
- f.classList.remove('visible');
- if (rExt.expSelf === 'OFF') {
- rExt.expSelf = 'ON';
- rExt.expExp('selfMode', document.querySelectorAll('.linklisting .link .collapsed.selftext'), 'self');
- content.addEventListener('DOMNodeInserted', selfAutoListener);
- document.querySelector('#rExtRowLink5').textContent = "5. Auto expand self posts: ON";
- } else {
- rExt.expSelf = 'OFF';
- content.removeEventListener('DOMNodeInserted', selfAutoListener);
- document.querySelector('#rExtRowLink5').textContent = "5. Auto expand self posts: OFF";
- }
- });
- rExtRowLink6.addEventListener('click', function (e) {
- e.preventDefault();
- e.stopPropagation();
- f.classList.add('hidden');
- f.classList.remove('visible');
- if (rExt.expVids === 'OFF') {
- rExt.expVids = 'ON';
- rExt.expExp('vidsMode', document.querySelectorAll('.linklisting .link .collapsed.video'), 'video');
- content.addEventListener('DOMNodeInserted', vidsAutoListener);
- document.querySelector('#rExtRowLink6').textContent = "6. Auto expand video posts: ON";
- } else {
- rExt.expVids = 'OFF';
- content.removeEventListener('DOMNodeInserted', vidsAutoListener);
- document.querySelector('#rExtRowLink6').textContent = "6. Auto expand video posts: OFF";
- }
- });
- if (!!isRES) {
- rExtRowLink7.addEventListener('click', function (e) {
- e.preventDefault();
- e.stopPropagation();
- f.classList.add('hidden');
- f.classList.remove('visible');
- if (rExt.expImgs === 'OFF') {
- rExt.expImgs = 'ON';
- rExt.expExp('imgMode', document.querySelectorAll('.linklisting .link:not(.RESFiltered) .collapsed.image'), 'images');
- content.addEventListener('DOMNodeInserted', imgsAutoListener);
- document.querySelector('#rExtRowLink7').textContent = "7. Auto expand image posts: ON";
- } else {
- rExt.expImgs = 'OFF';
- content.removeEventListener('DOMNodeInserted', imgsAutoListener);
- document.querySelector('#rExtRowLink7').textContent = "7. Auto expand image posts: OFF";
- }
- });
- }
- },
- notify: function (type) {
- var notifyTimer,
- notifyDiv = document.querySelector('#rExtNotify'),
- clearNotify = function () {
- document.querySelector('#rExtNotify').classList.add('hidden');
- document.querySelector('#rExtNotify').classList.remove('visible');
- clearInterval(notifyTimer);
- };
- notifyDiv.textContent = 'All ' + type + ' posts expanded';
- notifyDiv.classList.remove('hidden');
- notifyDiv.classList.add('visible');
- notifyTimer = setInterval(clearNotify, 5000);
- },
- expExp: function (mode, posts, type) {
- var cEvt = document.createEvent('MouseEvents'),
- i = 0,
- timer,
- expPosts;
- cEvt.initMouseEvent('click', false, true, window, 0, 1, 1, 1, 1, false, false, false, false, 0, null);
- expPosts = function () {
- if (posts[i]) {
- if (type === 'self') {
- posts[i].onclick();
- } else {
- posts[i].dispatchEvent(cEvt);
- }
- i += 1;
- } else {
- rExt.notify(type);
- clearInterval(timer);
- }
- };
- if (mode === 'selfVids') {
- timer = setInterval(expPosts(), rExt.expSelfDelay);
- } else if (mode === 'vidsMode') {
- timer = setInterval(expPosts, rExt.expVidsDelay);
- } else {
- timer = setInterval(expPosts, rExt.expImgsDelay);
- }
- },
- hideAllClicked: function () {
- var i = 0,
- hidePosts,
- timer,
- cEvt = document.createEvent('MouseEvents'),
- posts = document.querySelectorAll('.linklisting .click');
- cEvt.initMouseEvent('click', false, true, window, 0, 1, 1, 1, 1, false, false, false, false, 0, null);
- hidePosts = function () {
- if (posts[i]) {
- posts[i].parentNode.parentNode.querySelector('.hide-button a').dispatchEvent(cEvt);
- i += 1;
- } else {
- clearInterval(timer);
- }
- };
- timer = setInterval(hidePosts, rExt.hideDelay);
- },
- setMode: function (mode, posts) {
- var i, l, e, t,
- nsfwState = rExt.nsfwState,
- selfState = rExt.selfState;
- if ((mode === 'nsfwMode' && nsfwState === 'ON') || (mode === 'selfMode' && selfState === 'ON')) {
- for (i = 0, l = posts.length; i < l; i += 1) {
- if (mode === 'nsfwMode' && !posts[i].classList.contains('over18')) {
- posts[i].style.display = 'none';
- } else if (mode === 'nsfwMode' && posts[i].classList.contains('over18')) {
- posts[i].classList.add('rExtNSFW');
- }
- if (mode === 'selfMode' && !posts[i].classList.contains('self')) {
- posts[i].style.display = 'none';
- } else if (mode === 'selfMode' && posts[i].classList.contains('self')) {
- posts[i].classList.add('rExtSELF');
- }
- }
- } else {
- for (i = 0, l = posts.length; i < l; i += 1) {
- if (mode === 'nsfwMode' && !posts[i].classList.contains('over18')) {
- posts[i].style.display = 'block';
- } else if (mode === 'nsfwMode' && posts[i].classList.contains('over18')) {
- posts[i].classList.remove('rExtNSFW');
- }
- if (mode === 'selfMode' && !posts[i].classList.contains('self')) {
- posts[i].style.display = 'block';
- } else if (mode === 'selfMode' && posts[i].classList.contains('self')) {
- posts[i].classList.remove('rExtSELF');
- }
- }
- }
- },
- sortByComments: function () {
- var i, l,
- sortTop,
- posts = document.querySelectorAll('.linklisting .thing'),
- clears = document.querySelectorAll('.linklisting .clearleft'),
- postsActive = [],
- postsNull = [],
- postClear,
- postsSorted,
- linkListing,
- currPost = document.querySelector('.keyHighlight'),
- pageMarkers = document.querySelectorAll('.NERPageMarker');
- posts = Array.prototype.slice.call(posts, 0);
- posts.forEach(function (e, i) {
- if (e.querySelector('.comments').textContent === 'comment') {
- postsNull.push(e);
- } else {
- postsActive.push(e);
- }
- });
- sortTop = function (a, b) {
- return +(b.querySelector('.comments').textContent.split(' ')[0]) - +(a.querySelector('.comments').textContent.split(' ')[0]);
- };
- postsSorted = postsActive.sort(sortTop);
- if (!!currPost) {
- currPost.classList.add('keyHighlight');
- }
- if (!!pageMarkers) {
- for (i = 0, l = pageMarkers.length; i < l; i += 1) {
- pageMarkers[i].setAttribute('style', 'display:none!important');
- }
- }
- for (i = 0, l = clears.length; i < l; i += 1) {
- clears[i].parentNode.removeChild(clears[i]);
- }
- for (i = 0, l = postsSorted.length; i < l; i += 1) {
- postClear = document.createElement('div');
- postClear.classList.add('clearleft');
- postsSorted[i].parentNode.appendChild(postsSorted[i]);
- postsSorted[i].parentNode.appendChild(postClear);
- }
- for (i = 0, l = postsNull.length; i < l; i += 1) {
- postClear = document.createElement('div');
- postClear.classList.add('clearleft');
- postsNull[i].parentNode.appendChild(postsNull[i]);
- postsNull[i].parentNode.appendChild(postClear);
- }
- linkListing = document.querySelectorAll('.linklisting .link');
- for (i = 0, l = linkListing.length; i < l; i += 1) {
- linkListing[i].querySelector('.entry').classList.remove('keyHighlight');
- if (linkListing[i].querySelector('.thumbnail')) {
- linkListing[i].querySelector('.thumbnail').setAttribute('style', 'margin-bottom: 8px!important');
- }
- }
- },
- addStyle: function (sheet) {
- var style = document.createElement('style');
- style.type = 'text/css';
- style.textContent = sheet;
- document.querySelector('head').appendChild(style);
- },
- init: function () {
- var css = '';
- this.createMenu();
- css += '\
- #rExtBox{\
- position:absolute;\
- z-index: 999999999;\
- left:' + this.offLeft + ';\
- top:' + this.offTop + ';\
- text-align:center;\
- font-weight:bold;\
- padding-top:2px;\
- width:185px;\
- background-color:aliceBlue;\
- color:#000;\
- border:2px solid navy;\
- border-radius:5px;\
- -moz-box-shadow:1px 1px 1px cornflowerblue;\
- -webkit-box-shadow:1px 1px 1px cornflowerblue;\
- box-shadow:1px 1px 1px cornflowerblue;\
- visibility: hidden;\
- opacity: 0;\
- -webkit-transition: visibility 0s .4s, opacity .4s linear;\
- }\
- .visible {\
- visibility: visible!important;\
- opacity: 1!important;\
- -webkit-transition: opacity .4s linear!important;\
- }\
- .hidden {\
- visibility: hidden!important;\
- opacity: 0!important;\
- -webkit-transition: visibility 0s .4s, opacity .4s linear!important;\
- }\
- #rExtNotify{\
- background-color: #f0f3fc;\
- border: 2px solid cornflowerblue;\
- border-radius: 3px;\
- color: navy;\
- font-size: 24px;\
- padding: 15px 10px!important;\
- width: 300px;\
- height: 30px;\
- text-align: center;\
- position: fixed;\
- top: 50px;\
- right: 10px;\
- z-index: 999999999;\
- -moz-box-shadow:1px 1px 1px cornflowerblue;\
- -webkit-box-shadow:1px 1px 1px cornflowerblue;\
- box-shadow:1px 1px 1px cornflowerblue;\
- visibility: hidden;\
- opacity: 0;\
- -webkit-transition: visibility 0s .4s, opacity .4s linear;\
- }\
- #rExtClose{\
- display: inline-block;\
- height: auto;\
- line-height: 10px;\
- background-color: #fff;\
- border: 1px solid cornflowerblue;\
- float: right;\
- padding:1px!important;\
- margin-right:4px!important;\
- cursor:pointer;\
- }\
- #rExtClose:hover{cursor:crosshair;}\
- .rExtRowLink{\
- font-weight:normal;\
- display:block!important;\
- width:auto!important;\
- height:auto!important;\
- background-color:white!important;\
- font-size:9px!important;\
- line-height:11px!important;\
- border:1px solid navy!important;\
- border-radius:2px!important;\
- margin:3px!important;\
- padding:1px!important; \
- text-align:left!important;\
- color:#000!important;\
- cursor:pointer;\
- }\
- .rExtRowLink:hover{\
- background-color:cornflowerblue!important;\
- color:white!important;\
- }';
- this.addStyle(css);
- //this.debug();
- }
- };
- if (document.body.classList.length && document.body.classList.contains('listing-page')) {
- setTimeout(function () {
- rExt.init();
- }, 1000);
- }
- }());
Add Comment
Please, Sign In to add comment