Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Reddit - Modmail hide threads
- // @namespace http://userscripts.org
- // @author gavin19
- // @description Add context to links that don't have any
- // @match http://*.reddit.com/message/moderator
- // @match https://*.reddit.com/message/moderator
- // @include http://*.reddit.com/message/moderator
- // @include https://*.reddit.com/message/moderator
- // @match http://*.reddit.com/message/moderator*
- // @match https://*.reddit.com/message/moderator*
- // @include http://*.reddit.com/message/moderator*
- // @include https://*.reddit.com/message/moderator*
- // @match http://*.reddit.com/message/moderator/*
- // @match https://*.reddit.com/message/moderator/*
- // @include http://*.reddit.com/message/moderator/*
- // @include https://*.reddit.com/message/moderator/*
- // @version 1.0
- // ==/UserScript==
- (function () {
- var hideModThreads = {
- opts: {
- hideInvites: false /** Hide 'moderator invited' mails by default **/
- },
- checkThreads: function ( threads ) {
- var i, curr, nodes, j, lenj, content,
- mailThreads = threads || document.querySelectorAll( '.message-parent' ),
- hiddenModmail = JSON.parse( localStorage.getItem( 'hiddenModmail' ) ) || [];
- for ( i = 0, len = mailThreads.length; i < len; i += 1 ) {
- if ( hideModThreads.opts.hideInvites ) {
- content = mailThreads[i].querySelector('.subject');
- if ( content && ( content.innerHTML.match( 'moderator invited' ) || ( content.innerHTML.match( 'moderator added' ) ) ) ) {
- hideModThreads.click( mailThreads[i] );
- mailThreads[i].setAttribute( 'style', 'display:none;' );
- }
- }
- curr = mailThreads[i].getAttribute( 'data-fullname' );
- if ( hiddenModmail.indexOf( curr ) !== -1 ) {
- nodes = mailThreads[i].querySelectorAll( '.subject ~ *' );
- for ( j = 0, lenj = nodes.length; j < lenj; j += 1) {
- nodes[j].setAttribute( 'style', 'display:none' );
- }
- hideModThreads.addLinks( 'show', curr );
- mailThreads[i].classList.add( 'reduced' );
- } else {
- hideModThreads.addLinks( 'hide', curr );
- }
- }
- this.checkNewMails();
- },
- checkNewMails: function () {
- var i, len, newMail,
- reduced = document.querySelectorAll( '.reduced' );
- for ( i = 0, len = reduced.length; i < len; i += 1 ) {
- newMail = reduced[i].querySelector( '.new' );
- if ( newMail ) {
- reduced[i].parentNode.parentNode.parentNode.classList.add( 'flag' );
- }
- }
- },
- click: function ( ele ) {
- var cEvt = document.createEvent( 'MouseEvents' );
- cEvt.initMouseEvent( 'click', false, true, window, 0, 1, 1, 1, 1, false, false, false, false, 0, null );
- ele.dispatchEvent( cEvt );
- return false;
- },
- addHandler: function () {
- var i, len,
- id = this.parentNode.parentNode.getAttribute( 'data-fullname' ),
- node = this.parentNode.parentNode.querySelectorAll( '.subject ~ *' ),
- hiddenModmail = JSON.parse( localStorage.getItem( 'hiddenModmail' ) ) || [];
- if ( this.textContent.match( 'show' ) ) {
- hiddenModmail.splice( hiddenModmail.indexOf( id ), 1 );
- localStorage.setItem( 'hiddenModmail', JSON.stringify( hiddenModmail ) );
- for ( i = 0, len = node.length; i < len; i += 1) {
- node[i].setAttribute( 'style', 'display:block' );
- }
- this.parentNode.parentNode.classList.remove( 'reduced' );
- this.textContent = ' hide thread';
- } else {
- hiddenModmail.push( id );
- localStorage.setItem( 'hiddenModmail', JSON.stringify( hiddenModmail ) );
- for ( i = 0, len = node.length; i < len; i += 1 ) {
- node[i].setAttribute( 'style', 'display:none' );
- }
- this.parentNode.parentNode.classList.add( 'reduced' );
- this.textContent = ' show thread';
- }
- },
- addLinks: function ( state, thread ) {
- var link = document.createElement( 'a' ),
- subj = '.id-' + thread,
- target;
- link.href = 'javascript:;';
- link.textContent = ' ' + state + ' thread';
- link.className += 'threadToggle';
- target = document.querySelector( subj );
- target.querySelector( '.subject' ).appendChild( link );
- link.addEventListener( 'click', hideModThreads.addHandler, false );
- },
- addStyle: function () {
- var sheet = '' + '\
- .threadToggle {\
- font-size:smaller;\
- color:#369!important;\
- }\
- .flag::before {\
- content:\'!\';\
- font-weight:bolder;\
- color:#f00;\
- }\
- .reduced {\
- margin:0 10px 0 5px;\
- }';
- style = document.createElement( 'style' );
- style.type = 'text/css';
- style.textContent = sheet;
- document.querySelector( 'head' ).appendChild( style );
- },
- init: function () {
- var t;
- document.body.addEventListener( 'DOMNodeInserted', function ( e ) {
- t = e.target;
- if ( t.localName === 'div' && t.id && t.id.indexOf( 'siteTable' ) !== -1 ) {
- hideModThreads.checkThreads( t.querySelectorAll( '.message-parent' ) );
- }
- }, true );
- hideModThreads.addStyle();
- hideModThreads.checkThreads();
- }
- };
- if ( document.body && document.querySelector( '.messages-page' ) && window.localStorage ) {
- setTimeout(function () {
- hideModThreads.init();
- }, 100);
- }
- }());
Add Comment
Please, Sign In to add comment