Dynasty Thingifier

Adds post links and quote stuff to Dynasty forums

Pada tanggal 26 Januari 2016. Lihat %(latest_version_link).

// ==UserScript==
// @name        Dynasty Thingifier
// @namespace   Alice Cheshire
// @include     http://dynasty-scans.com/*
// @exclude     http://dynasty-scans.com/system/images_images/*
// @version     2.1.0.9
// @require     http://code.jquery.com/jquery-2.1.4.min.js
// @description Adds post links and quote stuff to Dynasty forums
// @grant		GM_setValue
// @grant		GM_getValue
// @grant		GM_deleteValue
// @grant		GM_listValues
// @run-at document-end
// ==/UserScript==
(function() {
  "use strict";
  var pageurl = document.location.toString().replace(/(#.+)/, ""), //Stores page url and removes any anchors from the stored url so we don't get issues with multiple anchors showing up
      isuserpostsurl = document.location.toString(), //Stores the address variable a second time for use in a different function
      postids = [], //Initializes a blank array for the postids
      quote = [], //Initializes blank array for quotes
      postcount = 0, //Counter to keep track of how many posts are on the current page
      counter = 0,
      configmenustate = GM_getValue("configmenustate", true), //Init our menu state's varaiable
      yourid = GM_getValue("youruserid"), //Set our user id variable
      fontsize = [3, "one", "two", "three", "four", "five"],
      bbcode_menu = `
<div id="thingifier-bbcode">
    <div class="thingifier-bbcode-first-row"><input type="button" id="thingifier-bbcode-quote" value="Quote">
        <input type="button" id="thingifier-bbcode-link" value="Link">
        <input type="button" id="thingifier-bbcode-image" value="Image">
        <input type="button" id="thingifier-bbcode-spoiler" value="Spoiler">
        <input type="button" id="thingifier-bbcode-ul" value="List">
        <input type="button" id="thingifier-bbcode-ol" value="Numbered List">
        <input type="button" id="thingifier-bbcode-italics" value="Italics">
        <input type="button" id="thingifier-bbcode-bold" value="Bold">
    </div>
    <div class="thingifier-bbcode-second-row">
        <input type="button" id="thingifier-bbcode-tag" value="Tags">
        <input type="button" id="thingifier-bbcode-hr" value="Horizontal Rule">
        <input type="button" id="thingifier-bbcode-codeblock" value="Code Block">
        <input type="button" id="thingifier-bbcode-h1" value="H1">
        <input type="button" id="thingifier-bbcode-h2" value="H2">
        <input type="button" id="thingifier-bbcode-h3" value="H3">
        <input type="button" id="thingifier-bbcode-h4" value="H4">
        <input type="button" id="thingifier-bbcode-h5" value="H5">
        <input type="button" id="thingifier-bbcode-h6" value="H6">
    </div>
</div>`, //The html code for our bbcode buttons
      quickreply;

  init(); 

  //Initialize Script
  function init() {
    //Load our config
    configload();

    //Populate Menu
    $('body').append(`
    <style>
        #thingifier {
	float: left;
	z-index: 1000 !important;
    max-width: 340px;
    max-height: 255px !important;
}
#thingifier, #magnifier-settings, #magnifier-submenu-toggle {
    top: 25%;
    position: fixed;
}
#thingifier-options {
	border: 1px solid black;
	padding: 8px;
	background: aliceblue;
	border-bottom-right-radius: 6px;
	border-left-width: 0;
}
#thingifier-options ul { list-style-type: none; margin-left: -4px;}
#thingifier-options ul > li {  vertical-align: middle; }
#thingifier ul li input { padding-right: 4px; }
#thingifier-font-size { width: 96px; }
#thingifier-toggle-button {
	position: absolute;
	top: 0;
	left: calc(100% - 1px);
	width: 24px;
	height: 24px;
	border: 1px solid black;
	background-color: aliceblue;
	color: red;
	border-top-right-radius: 6px;
	border-bottom-right-radius: 6px;
	border-left-width: 0;
}
.spoilers-disabled {
	background: #666 none repeat scroll 0% 0%;
	color: #fff;
}
.navbar-fixed {
	position: fixed;
	z-index: 1000;
	width: 1210px;
}
.pull-right-fixed, .nav-collapse-fixed {
	float: right;
}
.nav-padding {
	height: 60px;
	width: 1210px;
}
.forum_post_one {
	font-size: 10px !important;
	line-height: 12px !important;
}
.forum_post_two {
	font-size: 12px !important;
	line-height: 15px !important;
}
.forum_post_three {
	font-size: 16px !important;
	line-height: 19px !important;
}
.forum_post_four {
	font-size: 20px !important;
	line-height: 23px !important;
}
.forum_post_five {
	font-size: 25px !important;
	line-height: 28px !important;
}
#thingifier-bbcode {
	margin-bottom: 6px;
}
#thingifier-quickreply {
	width: 100% !important;
	background-color: aliceblue !important;
	display: inline-block;
	padding: 4px;
	padding-bottom: 8px;
	border-radius: 8px;
	margin-bottom: 6px;
}
#magnifier {
	visibility: hidden;
	position: absolute;
	border: 1px solid grey;
	background-color: rgba(0, 0, 0, 0.1);
	background-repeat: no-repeat;
	pointer-events: none;
	z-index: 100000000;
}
#thingifier-magnifier-menu, #magnifier-tooltip {
    display: inline-block;
    border: 1px solid black;
    padding: 8px;
    background: aliceblue;
    border-radius: 6px;
    margin-left: 0px;
    vertical-align: top;
}
#thingifier-magnifier-menu input {
    display: inline-block;
}
#sizenum, #minsizenum {
    width: 96px;
}
#sizemeasure, #minsizemeasure {
    margin-left: 8px;
}
#sizemeasure, #minsizemeasure, #zoomfactor {
    width: 64px;
}
#minsizenum, #minsizemeasure, #zoomfactor, #squareborder, #circularborder {
    margin-top: 7px;
}
#magnifier-menu-submit {
    margin-left: 178px;
    margin-right: 8px;
}
#magnifier-buttons {
    margin-top: -24px;
    float: right;
}
#thingifier-magnifier-menu h3, #magnifier-tooltip h3 {
    text-align: center;
    text-decoration: underline;
    padding-bottom: 8px;
}
#thingifier-magnifier-menu h3 i {
    padding-bottom: 4px;
    border-bottom: 2px solid black;
}
#thingifier-magnifier-menu input {
    padding: 2px;
    margin: 2px;
}
#thingifier-magnifier-menu input[type="radio"], #thingifier-magnifier-menu label {
    display: inline;
}
#magnifier-tooltip h3 {
    padding-bottom: 10px;
    border-bottom: 1px solid black;
}
#magnifier-tooltip ul {
    padding: 0px;
    margin: 0px 10px;
}
#magnifier-tooltip li {
    margin: 0 8px;
    list-style: none;
    margin-bottom: 8px;
    line-height: 16px;
    font-size: 13px;
}
#magnifier-tooltip li {
    border-bottom: 1px solid black;
    padding-bottom: 8px;
}
#magnifier-tooltip li:last-of-type {
    border-bottom: 0px solid black;
}
#magnifier-tooltip li div {
    font-weight: 900;
    text-align: center;
    margin-bottom: -16px;
    margin-top: 8px;
}
#magnifier-tooltip li div::before, #magnifier-tooltip li div::after {
    content: " — ";
}
#thingifier-magnifier-menu {
    position: fixed;
    margin-left: 300px;
    border-radius: 6px 0 0 6px;
    margin-top: -108px !important;
    z-index: 1003 !important;
    width: 276px;
    border-right-width: 0;
}
#magnifier-tooltip {
    min-width: 320px;
    margin-left: 592px;
    z-index: 1000;
    top: 2% !important;
    position: fixed;
    max-width: 550px;
}
#magnifier-tooltip, #thingifier-magnifier-menu {
    margin-top: 148px;
}
#magnifier-settings, #magnifier-submenu-toggle {
    display: inline;
    z-index: 1001;
}
.thingifier-icon {
    background-image: url(http://dynasty-scans.com//assets/twitter/bootstrap/glyphicons-halflings-2851b489e8c39f8fad44fc10efb99c3e.png);
    display: inline-block;
    width: 14px;
    height: 14px;
    line-height: 14px;
    vertical-align: text-top;
    background-repeat: no-repeat;
    margin-top: 1px;
    background-position: -48px 0px;
    z-index: 1005 !important;
}
#magnifier-submenu-toggle {
    margin-left: 272px;
    margin-top: 132px;
}
#magnifier-submenu-toggle::after {
    margin-left: 18px;
    font-size: 12px;
    font-style: normal;
    content: "Settings";
}
#magnifier-submenu-toggle:hover {
    text-decoration: underline;
    cursor: pointer;
}
#thingifier-magnifier-menu input[type="number"], #thingifier-magnifier-menu input[type="text"] {
    text-align: center;
}
#magnifier-settings {
    pointer-events: none;
}
#magnifier-settings * {
    pointer-events: auto;
}
#thingifier-cancel {
    background-color: rgba(0,0,0,0.2) !important;
    background-image: linear-gradient(to bottom, rgb(196, 0, 0), rgb(96, 0, 0));
    color: white;
    margin-left: 4px;
    background-position: 0 !important;
}
#thingifier-cancel:hover {
    background-image: linear-gradient(to bottom, rgb(224, 0, 0), rgb(124, 0, 0));
}
input.btn {
    cursor: pointer !important;
}
    </style>
    <div id="thingifier">
    <div id="thingifier-options">
        <ul>
            <li><input type="checkbox" id="thingifier-unhide-spoilers"> Unhide spoilers</li>
            <li><input type="checkbox" id="thingifier-fixed-navbar"> Fixed navbar</li>
            <li><input type="checkbox" id="thingifier-pagination"> Add page selector to top of page</li>
            <li><input type="checkbox" id="thingifier-bbcode-buttons"> Add quick reply and post page bbcode buttons</li>
            <li><input type="checkbox" id="thingifier-quote-to-quickreply"> Quote to quick reply instead of new post page</li>
            <li><input type="checkbox" id="thingifier-quote-move-quickreply"> Move quick reply to under quoted post</li>
            <li><input type="checkbox" id="thingifier-magnifier" tooltip="Press Z or middle mouse click"> Magnifier on reader and image pages</li>
            <li><input type="range" id="thingifier-font-size" min="1" max="5"> Change font size <input type="button" id="thingifier-reset-font" value="Reset Font Size"></li>
            <li><a href="http://dynasty-scans.com/forum/posts?user_id=${yourid}" id="thingifier-ownposts"> Your posts</a></li>
            <li><input type="text" id="useridinput"><input type="button" value="Submit user id" id="useridsubmit"></li>
            <li><input type="button" id="thingifier-clear" value="Clear stored data"></li>
        </ul>
    </div>
    <div id="thingified-toggle"><input type="button" id="thingifier-toggle-button" value="X"></div>
    <i class="thingifier-icon" id="magnifier-submenu-toggle"></i>
    <div id="thingifier-magnifier-menu">
        <h3><i class="thingifier-icon"></i> Magnifier Settings</h3>
        Size: <input type="number" id="sizenum"><input type="text" list="measurements" id="sizemeasure" pattern="vh|vw|vmin|vmax|%|px"><br>
        Minimum Size: <input type="number" id="minsizenum"><input type="text" list="measurements" id="minsizemeasure" pattern="vh|vw|vmin|vmax|%|px"><br>
        Zoom factor: <input type="number" id="zoomfactor">%<br>
        Shape: <input type="radio" id="squareborder" val="square" name="magnifier-shape"><label for="square" id="forsquare">Square </label><input type="radio" id="circularborder" val="circle" name="magnifier-shape"><label for="circle" id="forcircle">Circle</label><br>
        <span id="magnifier-buttons"><input type="button" id="magnifier-menu-submit" value="Save"><input type="button" id="magnifier-menu-cancel" value="Cancel"></span>
        <datalist id="measurements">
            <option value="vh">
            <option value="vw">
            <option value="vmin">
            <option value="vmax">
            <option value="%">
            <option value="px">
        </datalist>
    </div>
    <div id="magnifier-tooltip">
        <h3>Valid Size Types</h3>
        <ul>
            <li><div>vh</div><br>This is the vertical size of what you can see on the web page. It's measured in a percentage. (1-100% of that size. Ie: 1vh or 100vh.)</li>
            <li><div>vw</div><br>This is the horizontal size of what you can see on the web page. It's measured in a percentage. (1-100% of that size. Ie: 1vw or 100vw.)</li>
            <li><div>vmin</div><br>This is the smallest (horizontal or vertical) size of what you can see on the web page. It's measured in a percentage. (1-100% of that size. Ie: 1vmin or 100vmin.) On a desktop pc this will generally be the same as vh if you're browsing using a maximized window.</li>
            <li><div>vmin</div><br>This is the largest (horizontal or vertical) size of what you can see on the web page. It's measured in a percentage. (1-100% of that size. Ie: 1vmax or 100vmax.) On a desktop pc this will generally be the same as vw if you're browsing using a maximized window.</li>
            <li><div>%</div><br>This is a percent of the container size. In this case the size of the page itself. Note: Most pages on Dynasty that the magnifier works on aren't perfectly square so this could result in an elongated shape.</li>
            <li><div>px</div><br>A straight pixel size. (Ie: 256px results in  a static 256 pixels in size regardless of the size of the page.)</li>
        </ul>
    </div>
</div>

    `);

    //Setup own posts link stuff
    $('#useridinput').hide();
    $('#useridsubmit').hide();
    if (GM_getValue("youruserid", "Not Set") == "Not Set") {
      $('#thingifier-ownposts').hide();
      $('#useridinput').show();
      $('#useridsubmit').show();
      setuserid();
    }

    //Check we're viewing a thread
    if (pageurl.match(/forum\/topics/)) {
      $('.forum_post').each(function() {
        postids.push(this.id); //For each element of the class forum_post push the element's id to our postids array
      });
      $('.time').each(function(i, obj) {
        postcount++; //This is where we actually count how many posts are on the page
      });

      //Retrieve your user id
      if (GM_getValue("youruserid", "Not set").match(/\d+/)) {
        yourid = "http://dynasty-scans.com/forum/posts?user_id=" + GM_getValue("youruserid");
      } else {
        yourid = "Your user id isn't set!";
      }
    }
  }

  //Set user ID for own posts link
  function setuserid() {
    $('input#useridsubmit').click(function () {
      if($("input#useridinput").val().match(/^\d+$/)) {
        GM_setValue("youruserid", $('input#useridinput').val());
        $('#useridinput').hide();
        $('#useridsubmit').hide();
        $('#thingifier-ownposts').show();
        $('#thingifier-ownposts').attr('href', "http://dynasty-scans.com/forum/posts?user_id=" + GM_getValue("youruserid"));
      } else {
        GM_deleteValue("youruserid");
        $("input#useridinput").val();
        $('input#useridinput').val("Invalid user id!");
      }
    });
  }


  //Menu close/open
  $('input#thingifier-toggle-button').click(function() {
    menuclose("click");
  });

  //Unhide spoilers option
  $('#thingifier-unhide-spoilers').click(function() {
    if ($('#thingifier-unhide-spoilers').is(":checked")) {
      $('.spoilers').addClass('spoilers-disabled');
    } else {
      $('.spoilers').removeClass('spoilers-disabled');
    }
    GM_setValue("spoilers", $('#thingifier-unhide-spoilers').is(":checked"));
  });

  //Fixed navbar option
  $('#thingifier-fixed-navbar').click(function() {
    if ($('#thingifier-fixed-navbar').is(":checked")) {
      GM_setValue("navbar", $('#thingifier-fixed-navbar').is(":checked"));
      $('.navbar').addClass('navbar-fixed');
      $('div.forum_post').css("padding-top", 40);
      $("<div class=\"nav-padding\"></div>").insertAfter(".navbar");
    } else {
      GM_setValue("navbar", $('#thingifier-fixed-navbar').is(":checked"));
      $('.navbar').removeClass('navbar-fixed');
      $('div.forum_post').css("padding-top", 0);
      $('div.nav-padding').remove();
    }
  });

  //Pagination option
  $('#thingifier-pagination').click(function() {
    if ($('#thingifier-pagination').is(":checked")) {
      GM_setValue("pagination", $('#thingifier-pagination').is(":checked"));
      $("div.pagination").wrap('<div class=\"tmp\">').parent().html();
      var tmp = $('div.tmp').html();
      $("div.pagination").unwrap();
      $('#main').prepend(tmp);
    } else {
      GM_setValue("pagination", $('#thingifier-pagination').is(":checked"));
      $("div.pagination").first().remove();
    }
  });

  //Add bbcode buttons to post page and quick reply
  $('#thingifier-bbcode-buttons').click(function() {
    if ($('#thingifier-bbcode-buttons').is(":checked")) {
      GM_setValue("bbcode", $('#thingifier-bbcode-buttons').is(":checked"));
      $("#forum_post_message").parent().prepend(bbcode_menu);
    } else {
      GM_setValue("bbcode", $('#thingifier-bbcode-buttons').is(":checked"));
      $("div#thingifier-bbcode").remove();
    }
  });

  //Move the quick reply box to the current post
  $('#thingifier-quote-move-quickreply').click(function() {
    GM_setValue('movequickreply', $('#thingifier-quote-move-quickreply').is(":checked"));
    quickreply = $('#thingifier-quote-move-quickreply').is(":checked");
  });
  
  
  $('#thingifier-magnifier').click(function() {
    GM_setValue('magnifier', $('#thingifier-magnifier').is(":checked"));
    if (pageurl.match(/chapters/) || pageurl.match(/images/) && GM_getValue("magnifier", false)) {
      $('body').append('<div id="magnifier"></div>');
    } else {
      $('#magnifier').remove();
    }
    
  });

  //Font size slider
  $('#thingifier-font-size').on('input', function() {
    fontsize[0] = parseInt($(this).val());
    $('.message *').removeClass('forum_post_one');
    $('.message *').removeClass('forum_post_two');
    $('.message *').removeClass('forum_post_three');
    $('.message *').removeClass('forum_post_four');
    $('.message *').removeClass('forum_post_five');
    $('.message *').addClass('forum_post_' + fontsize[fontsize[0]]);
    GM_setValue('fontsize', fontsize[0]);
  });

  //Reset font size
  $('#thingifier-reset-font').click(function() {
    $('.message *').removeClass('forum_post_one');
    $('.message *').removeClass('forum_post_two');
    $('.message *').removeClass('forum_post_three');
    $('.message *').removeClass('forum_post_four');
    $('.message *').removeClass('forum_post_five');
    $('#thingifier-font-size').val(3);
    GM_deleteValue('fontsize');
  });

  //Clear saved data
  $('#thingifier-clear').click(function() {
    var x = window.confirm("Are you sure you want to clear your stored data?");
    if (x) {
      GM_deleteValue("youruserid");
      GM_deleteValue("quoteid");
      GM_deleteValue("quotename");
      GM_deleteValue("spoilers");
      GM_deleteValue("navbar");
      GM_deleteValue("pagination");
      GM_deleteValue("bbcode");
      GM_deleteValue("quote2quickreply");
      GM_deleteValue('movequickreply');
      GM_deleteValue('fontsize');
      GM_deleteValue("magnifier");
      GM_deleteValue('magSizeRes');
      GM_deleteValue('magSizeMeasure');
      GM_deleteValue('magMinSizeRes');
      GM_deleteValue('magMinSizeMeasure');
      GM_deleteValue('magZoomFactor');
      GM_deleteValue('magBorder');
      console.log(GM_getValue("youruserid", "Not set"));
      console.log(GM_getValue("quoteid", "Not set"));
      console.log(GM_getValue("quotename", "Not set"));
      console.log(GM_getValue("spoilers", "Not set"));
      console.log(GM_getValue("navbar", "Not set"));
      console.log(GM_getValue("pagination", "Not set"));
      console.log(GM_getValue("bbcode", "Not set"));
      console.log(GM_getValue("quote2quickreply", "Not set"));
      console.log(GM_getValue("movequickreply", "Not set"));
      console.log(GM_getValue("fontsize", "Not set"));
      console.log(GM_getValue("magnifier", "Not set"));
      console.log(GM_getValue('magSizeRes', "Not set"));
      console.log(GM_getValue('magSizeMeasure', "Not set"));
      console.log(GM_getValue('magMinSizeRes', "Not set"));
      console.log(GM_getValue('magMinSizeMeasure', "Not set"));
      console.log(GM_getValue('magZoomFactor', "Not set"));
      console.log(GM_getValue('magBorder', "Not set"));
      console.log(document.location.reload(true));
    } else {
      console.log("Decided against it");
    }
  });

  //Load our config
  function configload() {
    //Only run once the page is loaded
    $(document).ready(function() {
      //Deal with our current menu state
      menuclose("load");

      //Check if spoilers are unhidden
      if (!!GM_getValue("spoilers", false)) {
        $('#thingifier-unhide-spoilers').click();
      }

      //Check if the fixed navbar is enabled
      if (!!GM_getValue("navbar", false)) {
        $('#thingifier-fixed-navbar').click();
      }

      //Check if pagination option is enabled
      if (!!GM_getValue("pagination", false)) {
        $('#thingifier-pagination').click();
      }

      //Check if we've changed the font size and retrieve it
      fontsize[0] = GM_getValue('fontsize', null);
      if (fontsize[0] !== null) {
        $('#thingifier-font-size').val(fontsize[0]);
        $('.message *').addClass('forum_post_' + fontsize[fontsize[0]]);
      }

      //Check if bbcode buttons are enabled
      if (!!GM_getValue('bbcode', null)) {
        $('#thingifier-bbcode-buttons').click();
      }

      //Check if quote to quick reply option is enabled
      if (!!GM_getValue('quote2quickreply', null)) {
        $('#thingifier-quote-to-quickreply').click();
      }

      //Check if the move quick reply box option is enabled
      if (!!GM_getValue('movequickreply', null)) {
        $('#thingifier-quote-move-quickreply').click();
      }
      
      //Check if the magnifier option is enabled
      if (!!GM_getValue('magnifier', null)) {
        $('#thingifier-magnifier').click();
      }

      bbcode();
    });
  }

    function menuclose(sender) {
        //Only runs when loading a page
        if (sender === "load") {
            configmenustate = GM_getValue("configmenustate", true); //Load our menu state
            if (!configmenustate) { //If it's true collapse the menu
                $("#thingifier-options").animate({width:'toggle', height:'toggle'},0);
                $("#magnifier-submenu-toggle").fadeToggle(0);
            }
            $("#thingifier-magnifier-menu").fadeToggle(0);
            $("#magnifier-tooltip").fadeToggle(0);

            //Runs when clicking the button
        } else if (sender === "click") {
            configmenustate = !!configmenustate ? false : true; //XOR our menu state, can also use ^=
            $("#thingifier-options").animate({width:'toggle', height:'toggle'},350); //Toggle the menu
            GM_setValue("configmenustate", !!configmenustate); //Store the value
            if ($('#thingifier-magnifier-menu').is( ":visible" )) {
                $('#thingifier-magnifier-menu').fadeToggle(0);
                $('#magnifier-tooltip').fadeToggle(0);
            }
            if ($("#magnifier-submenu-toggle").is( ":visible" )) {
                $("#magnifier-submenu-toggle").fadeToggle(0);
            } else {
                $("#magnifier-submenu-toggle").fadeToggle(500);
            }
        }

        //Controls the button's icon
        if (!configmenustate) {
            $('#thingifier-toggle-button').val('▶');
        } else {
            $('#thingifier-toggle-button').val('◀');
        }
    }

  function bbcode() {
    var texttmp,
        sel,
        posttmp,
        regextmp,
        txtbegin,
        txtend;
    $('#forum_post_message').mousedown(function() {
      $('body').mouseup(function() {
        getSel();
        texttmp = sel;
        posttmp = $('#forum_post_message').val();
        regextmp = new RegExp("("+texttmp.replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&")+")");
        posttmp = posttmp.replace(regextmp, "[BBCODE-HERE]");
      });
    });
    function getSel() // javascript
    {
      // obtain the object reference for the <textarea>
      var txtarea = document.getElementById("forum_post_message");
      // obtain the index of the first selected character
      var start = txtarea.selectionStart;
      // obtain the index of the last selected character
      var finish = txtarea.selectionEnd;
      // obtain the selected text
      sel = txtarea.value.substring(start, finish);
      txtbegin = txtarea.value.substring(0, start);
      txtend = txtarea.value.substring(finish);
      // do something with the selected content
    }
    $('#thingifier-bbcode-quote').click(function() {
      texttmp = texttmp.replace(/(^\S)/gm, "> $1");
      bbcode_format();
    });
    $('#thingifier-bbcode-link').click(function() {
      if (sel.length === 0) {
        texttmp = texttmp.replace(/(.*)/gm, "[]($1)");
      } else {
        texttmp = texttmp.replace(/(.+)/gm, "[]($1)");
      }
      bbcode_format();
    });
    $('#thingifier-bbcode-image').click(function() {
      if (sel.length === 0) {
        texttmp = texttmp.replace(/(.*)/gm, "![]($1)");
      } else {
        texttmp = texttmp.replace(/(.+)/gm, "![]($1)");
      }
      bbcode_format();
    });
    $('#thingifier-bbcode-spoiler').click(function() {
      if (sel.length === 0) {
        texttmp = texttmp.replace(/(.*)/gm, "==$1==");
      } else {
        texttmp = texttmp.replace(/(.+)/gm, "==$1==");
      }
      bbcode_format();
    });
    $('#thingifier-bbcode-hr').click(function() {
      texttmp = texttmp.replace(/(^\S)/gm, "\n***\n $1");
      bbcode_format();
    });
    $('#thingifier-bbcode-ul').click(function() {
      texttmp = texttmp.replace(/(^\S)/gm, " * $1");
      bbcode_format();
    });
    $('#thingifier-bbcode-ol').click(function() {
      texttmp = texttmp.replace(/(^\S)/gm, " 1. $1");
      bbcode_format();
    });
    $('#thingifier-bbcode-italics').click(function() {
      if (sel.length === 0) {
        texttmp = texttmp.replace(/(.*)/gm, "*$1*");
      } else {
        texttmp = texttmp.replace(/(.+)/gm, "*$1*");
      }
      bbcode_format();
    });
    $('#thingifier-bbcode-bold').click(function() {
      if (sel.length === 0) {
        texttmp = texttmp.replace(/(.*)/gm, "**$1**");
      } else {
        texttmp = texttmp.replace(/(.+)/gm, "**$1**");
      }
      bbcode_format();
    });
    $('#thingifier-bbcode-tag').click(function() {
      if (sel.length === 0) {
        texttmp = texttmp.replace(/(.*)/gm, "`$1`");
      } else {
        texttmp = texttmp.replace(/(.+)/gm, "`$1`");
      }
      bbcode_format();
    });
    $('#thingifier-bbcode-codeblock').click(function() {
      if (sel.length === 0) {
        texttmp = texttmp.replace(/(.*)/gm, "    $1    ");
      } else {
        texttmp = texttmp.replace(/(.+)/gm, "    $1    ");
      }
      bbcode_format();
    });
    $('#thingifier-bbcode-h1').click(function() {
      if (sel.length === 0) {
        texttmp = texttmp.replace(/(.*)/gm, "# $1 #");
      } else {
        texttmp = texttmp.replace(/(.+)/gm, "# $1 #");
      }
      bbcode_format();
    });
    $('#thingifier-bbcode-h2').click(function() {
      if (sel.length === 0) {
        texttmp = texttmp.replace(/(.*)/gm, "## $1 ##");
      } else {
        texttmp = texttmp.replace(/(.+)/gm, "## $1 ##");
      }
      bbcode_format();
    });
    $('#thingifier-bbcode-h3').click(function() {
      if (sel.length === 0) {
        texttmp = texttmp.replace(/(.*)/gm, "### $1 ###");
      } else {
        texttmp = texttmp.replace(/(.+)/gm, "### $1 ###");
      }
      bbcode_format();
    });
    $('#thingifier-bbcode-h4').click(function() {
      if (sel.length === 0) {
        texttmp = texttmp.replace(/(.*)/gm, "#### $1 ####");
      } else {
        texttmp = texttmp.replace(/(.+)/gm, "#### $1 ####");
      }
      bbcode_format();
    });
    $('#thingifier-bbcode-h5').click(function() {
      if (sel.length === 0) {
        texttmp = texttmp.replace(/(.*)/gm, "##### $1 #####");
      } else {
        texttmp = texttmp.replace(/(.+)/gm, "##### $1 #####");
      }
      bbcode_format();
    });
    $('#thingifier-bbcode-h6').click(function() {
      if (sel.length === 0) {
        texttmp = texttmp.replace(/(.*)/gm, "###### $1 ######");
      } else {
        texttmp = texttmp.replace(/(.+)/gm, "###### $1 ######");
      }
      bbcode_format();
    });
    function bbcode_format() {
      var tmp = txtbegin + texttmp.replace(/\[BBCODE-HERE\]/, tmp) + txtend;
      $('#forum_post_message').val(tmp);
    }
  }
  $(document).ready(function() {
    if (isuserpostsurl.match(/http:\/\/dynasty-scans.com\/forum\/posts\?user_id=\d+/)) {
      isuserpostsurl = isuserpostsurl.replace(/\d+/, ""); //Replaces the user id in the url
    }
    for (var i = 0; i < postcount; i++){
      counter = i;
      var id = postids[i].toString(); //Temporarily store the post id under the key of 'i' into a variable to use in our next bit
      if ($('#thingifier-quote-to-quickreply').is(":checked")) {
        GM_setValue('quote2quickreply', $('#thingifier-quote-to-quickreply').is(":checked"));
        var tmp = $('.forum_post .info .row .actions').find("span:first-child a");
        var tmphref = tmp.attr('href');
        var urltmp = document.location.toString();
        urltmp = urltmp.replace(/(http:\/\/dynasty-scans\.com\/forum\/topics\/)(\d+)(.+)/, "$2");
        $('.forum_post .info .row .actions:eq(' + counter + ')').prepend("<input type=\"button\" class=\"postquote\" id=\"" + tmphref + "\" value=\"Quick Quote\" name=\"post_" + counter + "\">");
        id = id.replace(/forum_post_/, "");
      }
      if(isuserpostsurl !== "http://dynasty-scans.com/forum/posts?user_id=") { 
        //If we're not on the user posts page then we turn all post timestamps on a page into an anchor link
        //".time").eq(i).replaceWith("<div class=\"span5 time\"><a class=\"timelink\" href=\"" + pageurl + "#forum_post_" + id + "\">" + $(".time").eq(i).text() + "</a></div>");
      }
      counter++;
    }
    $('#thingifier-quote-to-quickreply').click(function() {
      GM_setValue('quote2quickreply', $('#thingifier-quote-to-quickreply').is(":checked"));
    });
    $('input.postquote').mouseup(function(e) {
      document.getElementById('forum_post_message').value = "";
      var postid = $($(this).parents()[3]).attr('id');
      var quoteid = postid; //Gets the id of the .forum_post parent
      quoteid = "#" + quoteid; //Adds a url anchor sign to the id
      quoteid = quoteid.toString(); //Converts it to a string to make sure it cooperates
      GM_setValue("quoteid", pageurl + quoteid);
      var quotename = $.trim($(quoteid).find(".user").text().replace(/Staff|Moderator|Uploader/, "")); //Retrieve the quoted user's name
      //For staff, mods, and uploaders find and remove their title, then trim the whitespace/newlines off the beginning and end
      GM_setValue("quotename", quotename);
      postid = postid.replace(/forum_post_/, "");
      var threadid = document.location.toString();
      threadid = threadid.replace(/(http:\/\/dynasty-scans\.com\/forum\/topics\/)(\d+)(\S+)/, "$2");
      var postpath = "http://dynasty-scans.com/forum/posts/new?quote_id=\"" + postid +"\"&topic_id=\"" + threadid + "\"";
      postpath = postpath.replace(/"/g, "");
      postid = postid.replace(/post_/, "");
      getpost(postpath, postid);
      if (quickreply) {
        var replybox;
        if ($("#thingifier-quickreply").length < 1) {
          $("#new_forum_post").wrap("<div id=\"thingifier-quickreply\"></div>");
          $('<input class="btn" id="thingifier-cancel" type="button" value="Cancel Post">').insertAfter("#new_forum_post input.btn:last");
        }
        if (!replybox) {
          replybox = $("#thingifier-quickreply").detach();
          replybox.appendTo(quoteid);
          replybox = null;
        }
      }
      if ($("#thingifier-quickreply").offset().top > window.pageYOffset + document.documentElement.clientHeight || $("#thingifier-quickreply").offset().top < window.pageYOffset) {
       $(document).scrollTop($("#thingifier-quickreply").offset().top - $('div.nav-padding').height());
      }
    });
    
    $(document).on("click", "input#thingifier-cancel", function(){
      console.log("Clicked");
      var replybox = $('#new_forum_post').detach();
      $("#thingifier-quickreply").remove();
      replybox.appendTo('div.row:last');
      $('#thingifier-cancel').remove();
    });

    //Do this if we click the quote button
    $('a').mouseup(function(e) { //When we click a link run this code
      if ($(e.target).text() == "Quote") { //Make sure that the clicked link is the quote button
        var quoteid = $(e.target).parents(); //Gets all the parent elements of our link
        quoteid = quoteid[4]; //Selects the fourth parent which is the .forum_post parent of the link
        quoteid = $(quoteid).attr('id'); //Gets the id of the .forum_post parent
        quoteid = "#" + quoteid; //Adds a url anchor sign to the id
        quoteid = quoteid.toString(); //Converts it to a string to make sure it cooperates
        GM_setValue("quoteid", pageurl + quoteid);
        var quotename = $(quoteid).find(".user").text(); //Retrieve the quoted user's name
        quotename = quotename.replace(/Staff|Moderator|Uploader/, ""); //For staff, mods, and uploaders find and remove their title
        quotename = $.trim(quotename); //Trim the whitespace/newlines off the beginning and end
        GM_setValue("quotename", quotename);
      } else { /*This is where code would run if we were doing anything for clicking other links*/ }
    });

    //Check that the current page is the new posts page
    if (pageurl.match(/posts\/new/)) {
      var post = GM_getValue("quoteid", false);
      var username = GM_getValue("quotename", false);
      if (!!post && !!username) {
        quote = "> [**" + username + "** posted:](" + post + ") \n> ";
        var message = "\n" + $('#forum_post_message').val();
        $('#forum_post_message').val(quote + message);
      }
    }
    
    //Reset our quoteid and quotename values if we preview our post.
    $('input[value="Preview Post"]').click(function(e) {
      GM_deleteValue("quoteid");
      GM_deleteValue("quotename");
    });
    
    //Reset our quoteid and quotename values if we create our post.
    $('input[value="Create Post"]').click(function(e) {
      GM_deleteValue("quoteid");
      GM_deleteValue("quotename");
    });
    
    //Insert "unread posts" button after thread link in manga viewer
    if (pageurl.match(/chapters/)) {
      var mangathread = '<a class="btn btn-mini" title="View latest unread post in thread"href="' + $('div.btn-toolbar div.btn-group:first-child a.btn:first-child').attr('href') + '/unread"><i class="icon-comment"></i>Unread</a>';
      $(mangathread).insertAfter('div.btn-toolbar div.btn-group:first-child a.btn:first-child');
    }
    
    //Insert last post link to read threads
    if (pageurl.match(/\/forum(?!\/topics)/)) {
      var elements = $('div.forum_topic').length;
      for (i = 0; i < elements; i++) {
          //Retrieve the last page link or the base thread link if the thread is only one page
          var linktype = $('div.forum_topic:eq(' + i + ') span.pages a:last').attr('href') || $('div.forum_topic:eq(' + i + ') a.subject').attr('href');
          //Create our last post link
          var link = '<a class="thingifier-lastlink" href="' + linktype + '#lastpost" style="margin-left:10px;"><i class="icon-comment"></i>Last Post</a>';
          //Check if thread has multiple pages. If so place link after pagination, otherwise place it right after the thread link
          if ($('div.forum_topic:eq(' + i + ')').find('span.pages').length !== 0) {
            $(link).insertAfter('div.forum_topic:eq(' + i + ') span.pages');
          } else {
            $(link).insertAfter('div.forum_topic:eq(' + i + ') a.subject');
          }
      }
    }
    
    //Check if we clicked the last post button on the forum view
    $('a.thingifier-lastlink').mouseup(function(e) {
      if (e.which === 1 || e.which === 2) {
        var threadnum = $(this).attr('href').match(/(\d+)/)[0]; //Retrieve the clicked thread's ID
        GM_setValue('lastlink_' + threadnum, true); //If we left or middle clicked it then set our lastlink value for that thread to true
      }
    });

    //Check if we're viewing a topic
    if (pageurl.match(/\/forum\/topics/) && window.location.toString().match(/#lastpost/)) {
       window.location = `${pageurl}#${$('div.time a:last').parents()[3].id}`;
    }
      
      
    //Magnifier Function
    var magY, magX, magShown = 0, imgPath = "none", zoom = GM_getValue('magZoomFactor', '500'), zoomFactor = zoom / 100, pWidth = 0, pHeight = 0, pOffset = 0, magZ = false;
    var updater = window.setInterval(update, 100);
    

    $(document).mousemove(function(e) {
      if (GM_getValue("magnifier", false)) {
        zoomFactor = zoom / 100;
        magY = e.pageY;
        magX = e.pageX;
        $('#magnifier').offset({ top: magY - ($('#magnifier').height() / 2), left: magX - ($('#magnifier').width() / 2) });
        var backLeft = ((-magX + pOffset.left) * zoomFactor) + 130;
        var backTop = ((-magY + pOffset.top) * zoomFactor) + 130;
        $('#magnifier').css("background-position", backLeft + "px " + backTop + "px");
      }
    });
    $(document).mousedown(function(e) {
      if (GM_getValue("magnifier", false)) {
        var tmp = $(e.target).parent()[0];
        if (tmp.className === "image" && pageurl.match(/images/)) {
          tmp = tmp.className;
        } else if (tmp.id === "image" && pageurl.match(/chapters/)) {
          tmp = tmp.id;
        } else {
          tmp = false;
        }
        tmp = !!tmp;
        if (e.which === 2 && tmp !== false) {
          e.preventDefault();
        }
        magnifier(tmp, e.which);
      }
    });
    $(document).keydown(function(e) {
      if (e.which == 90) {
        if (GM_getValue("magnifier", false) && pageurl.match(/chapters/) || pageurl.match(/images/)) {
          if (!$('#forum_post_message').is(":focus") && !$('input#q').is(":focus")) {
            e.preventDefault();
            magZ = true;
          } else {
            magZ = false;
          }
          if (magZ === true) {
            magnifier(magZ, 2);
          }
        }
      }
    });

    function magnifier(check, which) {
      if (GM_getValue("magnifier", false)) {
        if (!!check) {
         magShown = magShown ? 0 : 1;
        }
        if (!!magShown && !!check && which === 2) {
          $('#magnifier').css("visibility", "visible");
          $('*').css("cursor", "none");
          $('#magnifier').css({
            'min-width' : GM_getValue('magMinSizeRes', "256") + GM_getValue('magMinSizeMeasure', "px"),
            'min-height' : GM_getValue('magMinSizeRes', "256") + GM_getValue('magMinSizeMeasure', "px"),
            'width' :  GM_getValue('magSizeRes', "25") + GM_getValue('magSizeMeasure', "25"),
            'height' :  GM_getValue('magSizeRes', "25") + GM_getValue('magSizeMeasure', "25"),
            'border-radius' : GM_getValue('magBorder', "50%")
          });
          zoom = GM_getValue('magZoomFactor', "500");
          $('#magnifier').offset({ top: magY - ($('#magnifier').height() / 2), left: magX - ($('#magnifier').width() / 2) });
        } else {
          $('#magnifier').css("visibility", "hidden");
          $('*').css("cursor", "auto");
          $('a').css("cursor", "pointer");
          $('a *').css("cursor", "pointer");
          $('span.left').css("cursor", "pointer");
          $('span.right').css("cursor", "pointer");
        }
      }
    }
    function update() {
      if (GM_getValue("magnifier", false)) {
        pOffset = $('#image img').offset() || $('div.image img').offset();
        if (pageurl.match(/chapters/)) {
          pWidth = $('div#image.thumbnail img').width() || $('div#image').width();
          pHeight = $('div#image.thumbnail img').height() || $('div#image').height();
          imgPath = $('div#image.thumbnail img').attr('src') || $('div#image img').attr('src') || "none";
        } else if (pageurl.match(/images/)) {
          pWidth = $('div.image img').width();
          pHeight = $('div.image img').height();
          imgPath = $('div.image img').attr('src') || "none";
        }
        if (imgPath !== "none") {
          //imgPath = imgPath.replace(/<img src=\"(.+)\">/, '$1');
          $('#magnifier').css("background-image", "url(" + imgPath + ")");
          //Zoom works correctly but the offset is WAY off
          /*var img = new Image;
          img.src = imgPath;
          pWidth = img.width;
          pHeight = img.height;*/
          $('#magnifier').css("background-size", (pWidth * zoomFactor) + "px " + (pHeight * zoomFactor) + "px");
        }
      }
    }    

    $('i#magnifier-submenu-toggle').click(function() {
      $("#thingifier-magnifier-menu").fadeToggle(350);
      $("#magnifier-tooltip").fadeToggle(350);
      $('#sizenum').val(GM_getValue('magSizeRes', '25'));
      $('#sizemeasure').val(GM_getValue('magSizeMeasure', 'vmin'));
      $('#minsizenum').val(GM_getValue('magMinSizeRes', '256'));
      $('#minsizemeasure').val(GM_getValue('magMinSizeMeasure', 'px'));
      $('#zoomfactor').val(GM_getValue('magZoomFactor', '500'));
      if (GM_getValue('magBorder', '50%') === '50%') {
        $('#circularborder').click();
      } else {
        $('#squareborder').click();
      }
    });
    $('#forcircle').click(function() {
      $('#circularborder').click();
    });
    $('#forsquare').click(function() {
      $('#squareborder').click();
    });
    $('#magnifier-menu-submit').click(function() {
      var ferror = "",
          border,
          sizemeasure,
          minsizemeasure,
          size,
          minsize;
      if ($('#circularborder').is(':checked')) {
        border = "50%";
      } else {
        border = "0";
      }
      //Size number
      if ($('#sizenum').val().toString().match(/\d/g) !== null) {
        size = $('#sizenum').val();
      } else {
        ferror += "Please use a number for the size!\n";
      }
      //Size measurement type
      if (measurecheck($('#sizemeasure').val())) {
        sizemeasure = $('#sizemeasure').val();
      } else {
        ferror += "Please use a valid type for the size!\n";
      }
      //Minimum size number
      if ($('#minsizenum').val().toString().match(/\d/g) !== null) {
        minsize = $('#minsizenum').val();
      } else {
        ferror += "Please use a number for the minimum size!\n";
      }
      //Minimum size measurement type
      if (measurecheck($('#minsizemeasure').val())) {
        minsizemeasure = $('#minsizemeasure').val();
      } else {
        ferror += "Please use a valid type for the minimum size!\n";
      }
      //Zoom factor
      if ($('#zoomfactor').val().toString().match(/\d/g) !== null) {
        zoom = $('#zoomfactor').val();
      } else {
        ferror += "Please use a number for the zoomfactor!\n";
      }
      //Check for errors or save
      if (ferror !== "") {
        alert(ferror);
      } else {
        GM_setValue('magSizeRes', size);
        GM_setValue('magSizeMeasure', sizemeasure);
        GM_setValue('magMinSizeRes', minsize);
        GM_setValue('magMinSizeMeasure', minsizemeasure);
        GM_setValue('magZoomFactor', zoom);
        GM_setValue('magBorder', border);
        $('i#magnifier-submenu-toggle').click();
      }
    });
    $('#magnifier-menu-cancel').click(function() {
      $("#thingifier-magnifier-menu").fadeToggle(350);
      $("#magnifier-tooltip").fadeToggle(350);
    });
    function measurecheck(measure) {
      //Should work just as well as an if loop using the || operator
      switch(measure) {
        case "vh":
        case "vw":
        case "vmin":
        case "vmax":
        case "%":
        case "px":
          return true;
          break;
        default:
          return false;
          break;
      }
    }
    
  });//$(document).ready() end
  
  function getpost(postpath, postid) {
    var message = "";
    $.ajax({
      type: "GET",
      url: postpath,
      dataType: "text",
      timeout: 10000,
      cache: false
    })
    .done(function(data) {
      quote[postid] = htmlDecode($(data).find('#forum_post_message'));
      $('#forum_post_message').val(quote[postid]);
      var post = GM_getValue("quoteid");
      var username = GM_getValue("quotename");
      quote = "> [**" + username + "** posted:](" + post + ") \n> ";
      message = htmlDecode(data.replace(/([\u0000-\uffff]+<textarea .+ id="forum_post_message".+>)([\u0000-\uffff]+)(<\/textarea>[\u0000-\uffff]+)/, "$2"));
      $('#forum_post_message').val(quote + message);
    })
    .fail(function() {
      console.log("error");
    });
  }
  
  function htmlDecode(input){
    var e = document.createElement('div');
    e.innerHTML = input;
    return e.childNodes.length === 0 ? "" : e.childNodes[0].nodeValue;
  }
})();