Raw Source
GamingWolf / BadReadrScriptRRD

// ==UserScript==
// @name         BadReadrScriptRRD
// @namespace    http://tampermonkey.net/
// @version      0.1.2
// @description  Makes reading texts on reddit esier. Hopefully. Now for the dreaded redesign!
// @author       /u/GamingWolfie
// @match        *.reddit.com/r/*
// @exclude      old.reddit.com/r/*/comments/*
// @exclude      *.reddit.com/r/*/wiki/*
// @updateURL    https://rawgit.com/GamingWolf/BadReadrScript/master/RDD/BadReadrScriptRRD.user.js
// @license      MIT; https://opensource.org/licenses/MIT
// @grant        none
// @require      http://code.jquery.com/jquery-1.12.4.min.js
// @require      https://gist.github.com/raw/2625891/waitForKeyElements.js
// ==/UserScript==

(function () {
  'use strict';

  var botBar = $("div[data-test-id='post-content']").children().last().children().first(),
    postContent = $("div[data-test-id='post-content']").children(":nth-child(4)"),
    postTitle = $('title').text(),
    postAuthor = $("div[data-test-id='post-content']").children(":nth-child(2)"),
    readBtn = $('<div> <a id="readBtn" title="Format for better reading!" style="cursor: pointer;"> Read Mode </a></div>'),
    reading = false,
    inPost = false,
    hotkey = 113; // 113 == F2. Use http://keycode.info/ to customise hotkey

  $(document).ready(function () {
    $("<link/>", {
      rel: "stylesheet",
      type: "text/css",
      href: "https://rawgit.com/GamingWolf/BadReadrScript/master/RDD/custom.css"
    }).prependTo("head");

    waitForKeyElements("div[data-test-id='post-content']", function () {
      botBar.prepend(readBtn);
      inPost = true;
      console.log("BadReadrScriptRRD read mode is available!");
    });
    waitForKeyElements(".scrollerItem", function () {
      inPost = false;
      console.log("BadReadrScriptRRD read mode is not available!");
    }, true);
  });

  function readMode() {
    reading = true;
    $('html').prepend('<div id="readHolder"><div class="wrapper" id="readWrapper"><div class="header" id="readHeader"></div><div class="content" id="readContent"></div><div class="footer" id="readFooter"></div></div></div>');
    postContent.contents().clone().appendTo('#readContent').children().addClass('readChild');
    $('body').hide();
    window.scrollTo(0, 0);
    $('#readFooter').append('<div><a class="fancyButton" id="doneBtn"> Done </a></div>');
    $("#readContent > p:contains('---')").replaceWith('<hr>');
    $('#readContent').children('hr').removeAttr('class');
    $('#readContent').append('<hr>');
    createHeader();
  };

  function restore() {
    reading = false
    $('body').show();
    $('#readHolder').remove();
    window.scrollTo(0, 0);
  };

  function createHeader() {
    var titleHandler, authorHandler;
    $('#readHeader').append('<div class="title"></div><div class="author"></div>');
    postAuthor.clone().appendTo('.author');
    titleHandler = postTitle.replace(': HFY', '').replace(/\[+\w+\]/, '');
    $('.title').append(titleHandler);
    $('.RESUserTagImage').remove();
    $('[data-click-id="timestamp"]').remove();
    authorHandler = $('.author:contains("Posted by")').text();
    $('.author').text(authorHandler.replace('Posted by', 'Posted by ').trim());
    $('.author').children().remove()
  }

  $(document).on('click', '#readBtn', function () {
    readMode();
  });

  $(document).on('click', '#doneBtn', function () {
    restore();
  });

  $(document).keydown(function (e) {
    if (e.keyCode == hotkey && !reading && inPost) {
      readMode();
    }
    else if (e.keyCode == hotkey && reading && inPost) {
      restore();
    }
  });
})();