gavin19

Reddit - Add Context

Jan 8th, 2013
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name           Reddit - Add context to links
  3. // @namespace      http://userscripts.org
  4. // @author         gavin19
  5. // @description    Add context to  links that don't have any
  6. // @match          http://*.reddit.com/*/*/comments/*
  7. // @match          https://*.reddit.com/*/*/comments/*
  8. // @include        http://*.reddit.com/*/*/comments/*
  9. // @include        https://*.reddit.com/*/*/comments/*
  10. // @version        1.0
  11. // ==/UserScript==
  12. (function () {
  13.     var addContext = {
  14.  
  15.         contextLevel: 3, /** Change this value to increase/decrease the context level **/
  16.         processLinks: function ( links ) {
  17.             var i, len,
  18.                 a = links || document.querySelectorAll( '.comment .md a[href*="/comments/"]' ),
  19.                 cx = '?context=';
  20.             for ( i = 0, len = a.length; i < len ; i +=1 ) {
  21.                 if ( a[i].href.indexOf( cx ) === -1 ) {
  22.                     a[i].href += cx + this.contextLevel;
  23.                 }
  24.             }
  25.         },
  26.         init: function () {
  27.             var t;
  28.             document.body.addEventListener( 'DOMNodeInserted', function ( e ) {
  29.                 t = e.target;
  30.                 if ( t.localName === 'div' && t.id && t.id.indexOf( 'siteTable' ) !== -1 ) {
  31.                     addContext.processLinks( t.querySelectorAll( '.comment .md a[href*="/comments/"]' ) );
  32.                 }
  33.             }, true );
  34.             addContext.processLinks( document.querySelectorAll( '.comment .md a[href*="/comments/"]' ) );
  35.         }
  36.     };
  37.     if ( document.body && document.querySelector( '.comments-page' ) ) {
  38.         setTimeout(function () {
  39.             addContext.init();
  40.         }, 300);
  41.     }
  42. }());
Add Comment
Please, Sign In to add comment