Trim Old Reddit

Trimmer for Old Reddit

目前为 2021-04-22 提交的版本。查看 最新版本

// ==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.6
// @run-at        document-start
// ==/UserScript==

(() => {
  let side = null;

  const BETTER_HEADER = true;
  const HEADER_KEEP = [
    'news','worldnews','Jokes','books'
  ];
  const HEADER_ADD = {
    ysk:'youshouldknow',
    cool:'coolguides',
    next:'nextfuckinglevel',
    'new': top.location.href.replace('old','www'),
    hbros:'humansbeingbros',
    abros:'animalsbeingbros',
  };
  const HEADER_REPLACE = {
    explainlikeimfive:'eli5',
    todayilearned:'til',
    LifeProTips:'lpt',
    AskReddit:'ask',
  };

  function betterHeader() {
    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');
      }
      ++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();
    $.each(HEADER_ADD, function(k,v) {
      let x = template.clone(true);
      let a = x.find('a');
      a.attr('id', k);
      a.text(k);
      a.attr('href',v.indexOf('/') != -1 ? v : '/r/'+v);
      a.attr('title',v);
      x.appendTo(bar);
    });
    $('#header *').css({'background-color':'white','color':'black'});
  }

  function trim() {
    $(`<style>
      .selected {
        color: blue !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;
      }
    </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',
    });

    side = $('.side');
    side.empty();

    let w = $('<div id="body-wrapper" style="display:flex"></div>');
    $('#header').after(w);
    w.append(content);
    w.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');
          // console.log('Setting lastseen', href);
          (async () => {await GM.setValue('lastseen', href)})();
          loadComment(href);
        });
        if (first === null && !t.hasClass('stickied')) {
          // console.log('Setting first', a);
          first = a;
        }
      }
    });
    (async() => {
      let last_seen = await GM.getValue('lastseen', null);
      // console.log('Getting lastseen', last_seen);
      GM.deleteValue('lastseen');
      let l = null;
      if (last_seen) {
        l = $('a[href="'+last_seen+'"]');
      }
      // console.log('L is', l, l ? l.length: 'null');
      let to_show = (l && l.length > 0) ? l : first;
      if (to_show) {
        to_show.click();
      }
    })();
  }

  function loadComment(u) {
    GM.xmlHttpRequest({
      method: "GET",
      url: u,
      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();
        // Collapse sub-comments.
        side.find('.thing .child .expand').click();
        side.scrollTop(0);
      }
    });
  }

  setTimeout(function() {
    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;
    }

    if (document.URL.indexOf('/comments/') != -1)
      return;
    trim();
    betterHeader();
  }, 1000);

})();

QingJ © 2025

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