Dynasty Thingifier

Adds post links and quote stuff to Dynasty forums

As of 23.08.2015. See ბოლო ვერსია.

// ==UserScript==
// @name        Dynasty Thingifier
// @namespace   Alice Cheshire
// @include     http://dynasty-scans.com/forum/topics
// @version     1.3.3
// @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_listValue
// @run-at document-end
// ==/UserScript==
console.log("1");
varinit();
console.log("2");
checkpostpage();
console.log("3");
genmenu();
console.log("4");
menuclicks();
console.log("5");
cleardata();
console.log("6");
configload();
console.log("7");
setuserid();
console.log("8");

if (window.isuserpostsurl.match(/http:\/\/dynasty-scans.com\/forum\/posts\?user_id=\d+/)) {
	window.isuserpostsurl = window.isuserpostsurl.replace(/\d+/, ""); //Replaces the user id in the url
}
for (i = 0; i < window.postcount; i++){
	var id = window.postids[i].toString(); //Temporarily store the post id under the key of 'i' into a variable to use in our next bit
	if(window.isuserpostsurl !== "http://dynasty-scans.com/forum/posts?user_id=") { 
		$(".time").eq(i).replaceWith("<div class=\"span5 time\"><a href=\"" + window.pageurl + "#" + id + "\">" + $(".time").eq(i).text() + "</a></div>"); //If we're not on the user posts page then we turn all post timestamps on a page into an anchor link
	}
}
$('a').hover(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", window.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);
		console.log("Quote id: " + GM_getValue("quoteid") + ", Quote name: " + GMgetValue("quotename"));
	} else { /*This is where code would run if we were doing anything for clicking other links*/ }
});

//Checks if we're on the posting page
function checkpostpage() {
	var quoting = pageurl.replace(/http:\/\/dynasty-scans.com\/forum\//, "");
	quoting = quoting.replace(/\?.+/, "");
	if (quoting === "posts/new") {
		var post = GM_getValue("quoteid");
		var username = GM_getValue("quotename");
		console.log(post + "\n" + username);
		quote = "[" + username + "](" + post + ")";
		message = "\n" + document.getElementById('forum_post_message').value;
		document.getElementById('forum_post_message').value = quote + message;
	}
}

//Initializes our global variables
function varinit() {
	window.pageurl = document.location.toString(); //Stores the entire value of the address bar into a variable
	window.isuserpostsurl = window.pageurl; //Stores the address variable a second time for use in a different function
	window.postids = []; //Initializes a blank array for the postids
	window.postcount = 0;  //Counter to keep track of how many posts are on the current page
	window.pageurl = window.pageurl.replace(/(#.+)/, ""); //Removes any anchors from the stored url so we don't get issues with multiple anchors showing up
	$('.forum_post').each(function() {
 		window.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) {
		window.postcount++; //This is where we actually count how many posts are on the page
	});
  	console.log("Line 81");
	var tmp;
	if (GM_getValue("youruserid", "Not set") === undefined) {
		tmp = "Not set";
	} else {
		tmp = GM_getValue("youruserid", "Not set");
	}
  	console.log("Your id: " + tmp);
	if (tmp.match(/\d+/)) {
		window.yourid = "http://dynasty-scans.com/forum/posts?user_id=" + GM_getValue("youruserid");
		window.useridset = true;
    	console.log("Your id: " + window.yourid + ", ID set? " + window.useridset);
	} else {
		window.yourid = "Your user id isn't set!";
		window.useridset = false;
		console.log(window.useridset);
  	}
	window.configmenustate = true;
}

function genmenu(){
	var menustructure = '\
		<style>\
			#thingifier * { padding: 0; margin: 0; }\
			#thingifier {\
				float: left;\
				position: fixed;\
				top: 45%;\
				z-index: 1000000;\
			}\
			#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;\
			}\
		</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\" disabled=\"true\"> Add quick reply and post page bbcode buttons</li>\
			<li><input type=\"checkbox\" id=\"thingifier-quote-to-quickreply\" disabled=\"true\"> Quote to quick reply instead of new post page</li>\
			<li><input type=\"checkbox\" id=\"thingifier-quote-move-quickreply\" disabled=\"true\"> Move quick reply to under quoted post</li>\
			<li><input type=\"range\" id=\"thingifier-font-size\" min=\"1\" max=\"5\" disabled=\"true\"> Change font size</li>\
			<li><a href=\"' + window.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></div>';
	$('body').prepend(menustructure);
	$('#useridinput').hide();
	$('#useridsubmit').hide();
	if (!window.useridset) {
		console.log("Checking if user id is set to prepare to replace menu, " + window.useridset);
		GM_deleteValue("youruserid");
		$('#thingifier-ownposts').hide();
		$('#useridinput').show();
		$('#useridsubmit').show();
		setuserid();
	} else {
		console.log("Checking if user id is set to prepare to replace menu, " + window.useridset);
	}
}
function setuserid() {
	GM_deleteValue("youruserid");
	console.log("setuserid function is being executed");
	$('input#useridsubmit').click(function () {
		console.log($("input#useridinput").val());
		if($("input#useridinput").val().match(/^\d+$/)) {
			console.log($("input#useridinput").val());
			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 {
			console.log($("input#useridinput").val());
			$("input#useridinput").val();
			$('input#useridinput').val("Invalid user id!");
		}
	});
}
function cleardata() {
	$('#thingifier-clear').click(function() {
		console.log("Clear button clicked");
		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");
			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"));
			document.location.reload(true);
		} else {
			console.log("Decided against it");
		}
	});
}
function menuclicks() {
	$('#thingifier-toggle-button').click(function() {
		$("#thingifier-options").animate({width:'toggle', height:'toggle'},350);
		window.configmenustate ^= true;
		GM_setValue("configmenustate", window.configmenustate);
	});
	$('#thingifier-unhide-spoilers').click(function() {
		if ($('#thingifier-unhide-spoilers').is(":checked")) {
			GM_setValue("spoilers", $('#thingifier-unhide-spoilers').is(":checked"));
			$('.spoilers').addClass('spoilers-disabled');
		} else {
			GM_setValue("spoilers", $('#thingifier-unhide-spoilers').is(":checked"));
			$('.spoilers').removeClass('spoilers-disabled');
		}
	});
	$('#thingifier-fixed-navbar').click(function() {
		if ($('#thingifier-fixed-navbar').is(":checked")) {
			GM_setValue("navbar", $('#thingifier-fixed-navbar').is(":checked"));
			$('.navbar').addClass('navbar-fixed');
			$("<div class=\"nav-padding\"></div>").insertAfter(".navbar");
		} else {
			GM_setValue("navbar", $('#thingifier-fixed-navbar').is(":checked"));
			$('.navbar').removeClass('navbar-fixed');
			$('div.nav-padding').remove();
		}
	});
	$('#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();
			$('.posts').prepend(tmp);
		} else {
			GM_setValue("pagination", $('#thingifier-pagination').is(":checked"));
			$("div.pagination").first().remove();
		}
	});
}

function configload() {
	window.configmenustate = GM_getValue("configmenustate", 1);
	if(!window.configmenustate) {
		$("#thingifier-options").animate({width:'toggle', height:'toggle'},0);
	} else {}
	window.configspoiler = GM_getValue("spoilers", false);
	if (window.configspoiler) {
		$('.spoilers').addClass('spoilers-disabled');
		$('#thingifier-unhide-spoilers').prop('checked', true);
	}
	window.confignavbar = GM_getValue("navbar", false);
	if (window.configspoiler) {
		$('.navbar').addClass('navbar-fixed');
		$("<div class=\"nav-padding\"></div>").insertAfter(".navbar");
		$('#thingifier-fixed-navbar').prop('checked', true);
	}
	window.configpagination = GM_getValue("pagination", false);
	if (window.configspoiler) {
		$("div.pagination").wrap('<div class=\"tmp\">').parent().html();
		var tmp = $('div.tmp').html();
		$("div.pagination").unwrap();
		$('.posts').prepend(tmp);
		$('#thingifier-pagination').prop('checked', true);
	}
}

/*////////////////////
///// To-do list /////
//////////////////////

Options menu with miscellaneous enhancements
  Unhide spoilers
         javascript:(function(){$('.spoilers').removeClass('spoilers').css('background','lightgray');})();
         This removes the black highlighting and replaces it with light gray highlighting.

         javascript:(function(){$('.spoilers').removeClass('spoilers');})();
         This only removes the black highlighting. (You won't be able to tell what used to be marked as spoilers.)
  Font size options
  Fixed navbar
  Link to own post history
  Add pagination to top of page

Quick reply and posting page bbcode buttons
Quote into the quick reply box instead of the new posts page
When above option is enabled move quick reply box to under quoted post
Bookmarked threads submenu

//////////////////////////
///// End To-do list /////
////////////////////////*/
/*///////////////////////////
///// Old Code Snippets /////
/////////////////////////////
document.cookie = "quote=" + window.pageurl + quoteid + ";path=/"; //Stores the full page url plus quoted post's anchor to a cookie
document.cookie = "username=" + quotename + ";path=/"; //Store the quoted user's name to a cookie

//This following code was taken from a Stack Exchange answer
//It allows us to retrieve the contents of a specific cookie
function getCookie(name) {
	var value = "; " + document.cookie;
	var parts = value.split("; " + name + "=");
	if (parts.length == 2) return parts.pop().split(";").shift();
}
http://dynasty-scans.com/forum/posts?user_id=17437

  .navbar 
  Fixes navbar at the top of the screen and shows in front of everything
  {
    position: fixed;
    z-index: 1000;
    width: 1210px;
  }
  .pull-right, .nav-collapse 
  Fixes navbar layout due to being fixed in place
  {
    float: right;
  }
$( "<p>Test</p>" ).insertAfter( ".inner" );
40px tall

*/