gavin19

Reddit - Hide mod threads

Jan 8th, 2013
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name            Reddit - Modmail hide threads
  3. // @namespace       http://userscripts.org
  4. // @author          gavin19
  5. // @description     Add context to links that don't have any
  6. // @match           http://*.reddit.com/message/moderator
  7. // @match           https://*.reddit.com/message/moderator
  8. // @include         http://*.reddit.com/message/moderator
  9. // @include         https://*.reddit.com/message/moderator
  10. // @match           http://*.reddit.com/message/moderator*
  11. // @match           https://*.reddit.com/message/moderator*
  12. // @include         http://*.reddit.com/message/moderator*
  13. // @include         https://*.reddit.com/message/moderator*
  14. // @match           http://*.reddit.com/message/moderator/*
  15. // @match           https://*.reddit.com/message/moderator/*
  16. // @include         http://*.reddit.com/message/moderator/*
  17. // @include         https://*.reddit.com/message/moderator/*
  18. // @version         1.0
  19. // ==/UserScript==
  20. (function () {
  21.     var hideModThreads = {
  22.         opts: {
  23.             hideInvites: false /** Hide 'moderator invited' mails by default **/
  24.         },
  25.         checkThreads: function ( threads ) {
  26.             var i, curr, nodes, j, lenj, content,
  27.                 mailThreads = threads || document.querySelectorAll( '.message-parent' ),
  28.                 hiddenModmail = JSON.parse( localStorage.getItem( 'hiddenModmail' ) ) || [];
  29.             for ( i = 0, len = mailThreads.length; i < len; i += 1 ) {
  30.                 if ( hideModThreads.opts.hideInvites ) {
  31.                     content = mailThreads[i].querySelector('.subject');
  32.                     if ( content && ( content.innerHTML.match( 'moderator invited' ) || ( content.innerHTML.match( 'moderator added' )  ) ) ) {
  33.                         hideModThreads.click( mailThreads[i] );
  34.                         mailThreads[i].setAttribute( 'style', 'display:none;' );
  35.                     }
  36.                 }
  37.                 curr = mailThreads[i].getAttribute( 'data-fullname' );
  38.                 if ( hiddenModmail.indexOf( curr ) !== -1 ) {
  39.                     nodes = mailThreads[i].querySelectorAll( '.subject ~ *' );
  40.                     for ( j = 0, lenj = nodes.length; j < lenj; j += 1) {
  41.                         nodes[j].setAttribute( 'style', 'display:none' );
  42.                     }
  43.                     hideModThreads.addLinks( 'show', curr );
  44.                     mailThreads[i].classList.add( 'reduced' );
  45.                 } else {
  46.                     hideModThreads.addLinks( 'hide', curr );
  47.                 }
  48.             }
  49.             this.checkNewMails();
  50.         },
  51.         checkNewMails: function () {
  52.             var i, len, newMail,
  53.                 reduced = document.querySelectorAll( '.reduced' );
  54.             for ( i = 0, len = reduced.length; i < len; i += 1 ) {
  55.                 newMail = reduced[i].querySelector( '.new' );
  56.                 if ( newMail ) {
  57.                     reduced[i].parentNode.parentNode.parentNode.classList.add( 'flag' );
  58.                 }
  59.             }
  60.         },
  61.         click: function ( ele ) {
  62.             var cEvt = document.createEvent( 'MouseEvents' );
  63.             cEvt.initMouseEvent( 'click', false, true, window, 0, 1, 1, 1, 1, false, false, false, false, 0, null );
  64.             ele.dispatchEvent( cEvt );
  65.             return false;
  66.         },
  67.         addHandler: function () {
  68.             var i, len,
  69.                 id = this.parentNode.parentNode.getAttribute( 'data-fullname' ),
  70.                 node = this.parentNode.parentNode.querySelectorAll( '.subject ~ *' ),
  71.                 hiddenModmail = JSON.parse( localStorage.getItem( 'hiddenModmail' ) ) || [];
  72.             if ( this.textContent.match( 'show' ) ) {
  73.                 hiddenModmail.splice( hiddenModmail.indexOf( id ), 1 );
  74.                 localStorage.setItem( 'hiddenModmail', JSON.stringify( hiddenModmail ) );
  75.                 for ( i = 0, len = node.length; i < len; i += 1) {
  76.                     node[i].setAttribute( 'style', 'display:block' );
  77.                 }
  78.                 this.parentNode.parentNode.classList.remove( 'reduced' );
  79.                 this.textContent = ' hide thread';
  80.             } else {
  81.                 hiddenModmail.push( id );
  82.                 localStorage.setItem( 'hiddenModmail', JSON.stringify( hiddenModmail ) );
  83.                 for ( i = 0, len = node.length; i < len; i += 1 ) {
  84.                     node[i].setAttribute( 'style', 'display:none' );
  85.                 }
  86.                 this.parentNode.parentNode.classList.add( 'reduced' );
  87.                 this.textContent = ' show thread';
  88.             }
  89.         },
  90.         addLinks: function ( state, thread ) {
  91.             var link = document.createElement( 'a' ),
  92.                 subj = '.id-' + thread,
  93.                 target;
  94.             link.href = 'javascript:;';
  95.             link.textContent = ' ' + state + ' thread';
  96.             link.className += 'threadToggle';
  97.             target = document.querySelector( subj );
  98.             target.querySelector( '.subject' ).appendChild( link );
  99.             link.addEventListener( 'click', hideModThreads.addHandler, false );
  100.         },
  101.         addStyle: function () {
  102.             var sheet = '' + '\
  103.             .threadToggle {\
  104.                 font-size:smaller;\
  105.                 color:#369!important;\
  106.             }\
  107.             .flag::before {\
  108.                 content:\'!\';\
  109.                 font-weight:bolder;\
  110.                 color:#f00;\
  111.             }\
  112.             .reduced {\
  113.                 margin:0 10px 0 5px;\
  114.             }';
  115.             style = document.createElement( 'style' );
  116.             style.type = 'text/css';
  117.             style.textContent = sheet;
  118.             document.querySelector( 'head' ).appendChild( style );
  119.         },
  120.         init: function () {
  121.             var t;
  122.             document.body.addEventListener( 'DOMNodeInserted', function ( e ) {
  123.                 t = e.target;
  124.                 if ( t.localName === 'div' && t.id && t.id.indexOf( 'siteTable' ) !== -1 ) {
  125.                     hideModThreads.checkThreads( t.querySelectorAll( '.message-parent' ) );
  126.                 }
  127.             }, true );
  128.             hideModThreads.addStyle();
  129.             hideModThreads.checkThreads();
  130.         }
  131.     };
  132.     if ( document.body && document.querySelector( '.messages-page' ) && window.localStorage ) {
  133.         setTimeout(function () {
  134.             hideModThreads.init();
  135.         }, 100);
  136.     }
  137. }());
Add Comment
Please, Sign In to add comment