Trim Old Reddit

Trimmer for Old Reddit

目前為 2021-05-06 提交的版本,檢視 最新版本

// ==UserScript==
// @name          Trim Old Reddit
// @namespace     stgeorge
// @description   Trimmer for Old Reddit
// @require       http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @match         *://old.reddit.com/*
// @match         *://www.reddit.com/*
// @grant         GM.xmlHttpRequest
// @grant         GM.getValue
// @grant         GM.setValue
// @grant         GM.deleteValue
// @version       1.8
// @run-at        document-start
// ==/UserScript==

(() => {
  let side = null;

  const OPT_SIMPLIFY_HEADER = false;
  const OPT_NEW_LINK = true;

  const HEADER_KEEP = [
    'news','worldnews','Jokes','books'
  ];
  const HEADER_ADD = {
    ysk:'youshouldknow',
    cool:'coolguides',
    next:'nextfuckinglevel',
    hbros:'humansbeingbros',
    abros:'animalsbeingbros',
  };
  const HEADER_REPLACE = {
    explainlikeimfive:'eli5',
    todayilearned:'til',
    LifeProTips:'lpt',
    AskReddit:'ask',
  };

  function trim() {
    $(`<style>
      .selected {
        color: blue !important;
      }
      #search {
        text-align: center;
        padding: 5px;
        position: revert !important;
        margin-top: 0 !important;
      }
      #new {
        color: red !important;
      }
      #header {
        height: revert !important;
        position: initial !important;
      }
      .thing {
        background-color:white;
      }
      .link {
        margin-right: revert !important;
      }
      body div.content {
        margin: 5px !important;
      }
      .sitetable {
        border-bottom: 0 !important;
        border-left: 0 !important;
        border-right:solid 1px #eeeeff !important;
        margin-right: revert !important;
      }
      .listing-page #siteTable {
        margin-right: 0;
      }
      .side * {
        font-size: 16px !important;
      }
      .side {
        background: url('') !important;
        background-color: transparent;
        box-shadow: revert;
        width:66%;
        margin:5px;
        height:1000px;
        overflow-y:auto;
        padding-top: 0;
      }
      div.md>blockquote>p {
        position: inherit !important;
      }
      .side .commentarea {
        margin-right: revert !important;
      }
      .side:before {
        content: revert !important;
        background: revert !important;
      }
      .content:before {
        background-color: revert !important;
        background-image: revert !important;
      }
      .side:after {
        content: '';
      }
      .clicked {
        background-color: #eeeeff;
        border: solid 1px black;
        border-radius: 10px;
      }
      .ui-resizable-e { 
        cursor: e-resize; 
        width: 2px; 
        right: -1px; 
        top: 0; 
        bottom: 0; 
        background-color: blue;
      }
    </style>`).appendTo('head');

    $('.infobar').remove();
    $('.footer-parent').remove();
    $('#sr-header-area').siblings().remove();
    let content = $('.content[role="main"]');

    content.css({
      width:'33%',
      height: '1000px',
      'overflow-y': 'auto',
      resize:'horizontal',
      margin: '0',
    });

    //
    // We make the right-hand side sidebar (.side) bigger and empty its
    // contents. Then, we make the main pane (.content) thinner and
    // use it show the list of articles. (We mark it as resizable horizontally
    // so that it can be widened.) When an article is clicked on in
    // the .content pane, we show its threaded comments in the .side pane.
    //
    // +----------------------------------------------------+
    // |                      #header                       |
    // +----------------------------------------------------+
    // |                #body-wrapper (new)                 |
    // | +-------------+----------------------------------+ |
    // | |   .content  |          .side                   | |
    // | |+-----------+|                                  | |
    // | ||  #search  ||     Posting and comments         | |
    // | |+-----------+|                                  | |
    // | || Post list ||                                  | |
    // | ||  ...      ||                                  | |
    // | |+-----------+|                                  | |
    // | +-------------+----------------------------------+ |
    // +----------------------------------------------------+
    //
    let wrapper = $('<div id="body-wrapper" style="display:flex"></div>');
    $('#header').after(wrapper);
    wrapper.append(content);
    content.prepend($('#search').detach());
    side = $('.side');
    side.empty();
    wrapper.append(side);

    let curbg = 'white';
    let first = null;
    $('.entry a').add('.search-result a').add('a.title')
      .each(function(k,v) {
      let t = $(this).closest('.thing');
      if (t.length == 0)
          t = $(this).closest('.search-result');
      let a = $(this);
      let href = a.attr('href');
      if (href.indexOf('/comments/') != -1) {
        a.on('click', function(e) {
          e.preventDefault();
          e.stopPropagation();
          t.css({'background-color':'#eeeeff'});
          t.addClass('clicked');
          t.siblings().css({'background-color':curbg});
          t.siblings().removeClass('clicked');
          href = href.replace('www.reddit.com', 'old.reddit.com').
            replace('//reddit.com', '//www.reddit.com');
          (async () => {await GM.setValue('lastseen', href)})();
          loadComment(href);
        });
        if (first === null && !t.hasClass('stickied')) {
          first = a;
        }
      }
    });
    (async() => {
      let last_seen = await GM.getValue('lastseen', null);
      GM.deleteValue('lastseen');
      let l = null;
      if (last_seen) {
        l = $('a[href="'+last_seen+'"]');
      }
      let to_show = (l && l.length > 0) ? l : first;
      if (to_show) {
        to_show.click();
      }
    })();
  }
  
  function loadComment(u) {
    GM.xmlHttpRequest({
      method: "GET",
      url: u+'?sort=new',
      onload: function(response) {
        side.html($.parseHTML(response.responseText));
        let c = side.find('.content[role="main"]').detach();
        side.empty();
        side.append(c);
        c.css({margin:'10px'});
        side.find('.infobar').detach();
        side.find('.thing .child .expand').click(); // Collapse sub-comments.
        setTimeout(function() {
          $(document).scrollTop(0);
          side.scrollTop(0);
        }, 500);
      }
    });
  }

  function fixHeader() {
    //
    // If OPT_SIMPLIFY_HEADER is set, we trim the links
    // in the header to a chosen few :-). 
    // If OPT_NEW_LINK is set, we add a 'new' link to to the header
    // to toggle between old and new reddits.
    //
    let template = null;
    let i = 0;
    let bars = $('ul.sr-bar');
    bars.find('li').each(function(k,v) {
      let li = $(this);
      if (template == null && i > 0) {
        template = li.clone(true);
        template.removeClass('selected');
        if (!OPT_SIMPLIFY_HEADER)
          return false;
      }
      ++i;
      let a = li.find('a:first');
      let t = a.text();
      if (HEADER_KEEP.indexOf(t) == -1 && !HEADER_REPLACE[t]) {
        li.remove();
      } else {
        a.attr('title', t);
        if (HEADER_REPLACE[t]) {
          a.text(HEADER_REPLACE[t]);
          a.attr('title',t);
        }
      }
    });

    let bar = bars.last();
    add_headers = OPT_SIMPLIFY_HEADER ? HEADER_ADD : {};
    if (OPT_NEW_LINK)
      add_headers['new'] = top.location.href.replace('old','www');

    $.each(add_headers, function(k,v) {
      let x = template.clone(true);
      let a = x.find('a');
      x.prepend(a);
      a.attr('id', k);
      a.text(k+' ');
      a.attr('href',v.indexOf('/') != -1 ? v : '/r/'+v);
      a.attr('title',v);
      x.prependTo(bar);
    });
    if (OPT_SIMPLIFY_HEADER)
      $('#header *').css({'background-color':'white','color':'black'});
  }

  function addOldButton() {
    //
    // If we're in the 'new' reddit, add an 'Old' button to help
    // us switch to old reddit.
    //
    if (document.URL.indexOf('www.reddit.com') != -1) {
      $('#email-verification-tooltip-id').
        before($('<button title="Old Reddit" id="old-button" type="button">OLD</button>'));
      let old = $('#old-button');
      old.css({
        margin: '10px',
        border: 'solid 1px #0079d3',
        padding: '5px',
        'border-radius': '4px',
        color: '#0079d3',
        'font-weight': 'bold'
      });
      old.on('click', function() {
        top.location.hostname = 'old.reddit.com';
      });
      return;
    }
  }

  // Main.
  // The setTimeout is there to handle race conditions to run our
  // script after reddit loads all its scripts. Doesn't seem to work
  // always :-(
  // 
  setTimeout(function() {
    addOldButton();
    // If we're already in the comments page, nothing more to do.
    if (document.URL.indexOf('/comments/') == -1) {
      trim();
      fixHeader();
    }
  }, 1000);

})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址