Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Dynasty Thingifier
- // @namespace Alice Cheshire
- // @include http://dynasty-scans.com/*
- // @exclude http://dynasty-scans.com/system/images_images/*
- // @version 2.0
- // @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==
- (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> @import url(http://alice-cheshire.rocks/Dynasty%20Thingifier.css);</style>');
- $('body').prepend('<div id="thingifier"></div>');
- $('#thingifier').append('<div id="thingifier-options"></div>');
- $('#thingifier-options').append('<ul><li><input type="checkbox" id="thingifier-unhide-spoilers"> Unhide spoilers</li></ul>');
- $('#thingifier-options ul').append('<li><input type="checkbox" id="thingifier-fixed-navbar"> Fixed navbar</li>');
- $('#thingifier-options ul').append('<li><input type="checkbox" id="thingifier-pagination"> Add page selector to top of page</li>');
- $('#thingifier-options ul').append('<li><input type="checkbox" id="thingifier-bbcode-buttons"> Add quick reply and post page bbcode buttons</li>');
- $('#thingifier-options ul').append('<li><input type="checkbox" id="thingifier-quote-to-quickreply"> Quote to quick reply instead of new post page</li>');
- $('#thingifier-options ul').append('<li><input type="checkbox" id="thingifier-quote-move-quickreply"> Move quick reply to under quoted post</li>');
- $('#thingifier-options ul').append('<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>');
- $('#thingifier-options ul').append('<li><a href="http://dynasty-scans.com/forum/posts?user_id=' + yourid + '" id="thingifier-ownposts"> Your posts</a></li>');
- $('#thingifier-options ul').append('<li><input type="text" id="useridinput"><input type="button" value="Submit user id" id="useridsubmit"></li>');
- $('#thingifier-options ul').append('<li><input type="button" id="thingifier-clear" value="Clear stored data"></li>');
- $('#thingifier').append('<div id="thingified-toggle"><input type="button" id="thingifier-toggle-button" value="X"></div>');
- //Setup own posts link stuff
- $('#useridinput').hide();
- $('#useridsubmit').hide();
- if (GM_getValue("youruserid", "Not Set") == "Not Set") {
- //GM_getValue("youruserid", "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+/)) {
- console.log("Retrieved user id: " + GM_getValue("youruserid"));
- yourid = "http://dynasty-scans.com/forum/posts?user_id=" + GM_getValue("youruserid");
- } else {
- console.log("User id not set!");
- yourid = "Your user id isn't set!";
- }
- //console.log(JSON.stringify(postids));
- }
- }
- //Set user ID for own posts link
- function setuserid() {
- $('input#useridsubmit').click(function () {
- console.log("User id submitted");
- 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");
- });
- //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");
- 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"));
- 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();
- }
- 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);
- }
- //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
- }
- //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 + "\">");
- //tmp.replaceWith("<input type=\"button\" class=\"postquote\" id=\"" + tmphref + "\" value=\"Quote\" name=\"post_" + counter + "\">");
- id = id.replace(/forum_post_/, "");
- }
- if(isuserpostsurl !== "http://dynasty-scans.com/forum/posts?user_id=") {
- $(".time").eq(i).replaceWith("<div class=\"span5 time\"><a class=\"timelink\" href=\"" + pageurl + "#forum_post_" + 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
- }
- counter++;
- }
- $('input.postquote').mouseup(function(e) {
- 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_/, "");
- $.ajax({
- type: "GET",
- url: postpath,
- dataType: "html"
- })
- .done(function(data) {
- quote[postid] = data.replace(/([\u0000-\uffff]+<textarea .+ id="forum_post_message".+>)([\u0000-\uffff]+)(<\/textarea>[\u0000-\uffff]+)/, "$2");
- quote[postid] = htmlDecode(quote[postid]);
- $('#forum_post_message').val(quote[postid]);
- var post = GM_getValue("quoteid");
- var username = GM_getValue("quotename");
- quote = "> [" + username + "](" + post + ") \n> ";
- var message = document.getElementById('forum_post_message').value;
- document.getElementById('forum_post_message').value = quote + message;
- })
- .fail(function() {
- console.log("error");
- });
- if (quickreply) {
- var replybox;
- if ($("#thingifier-quickreply").length < 1) {
- $("#new_forum_post").wrap("<div id=\"thingifier-quickreply\"></div>");
- }
- if (!replybox) {
- replybox = $("#thingifier-quickreply").detach();
- replybox.appendTo(quoteid);
- replybox = null;
- }
- }
- });
- function htmlDecode(input){
- var e = document.createElement('div');
- e.innerHTML = input;
- return e.childNodes.length === 0 ? "" : e.childNodes[0].nodeValue;
- }
- $('a').click(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);
- } else { /*This is where code would run if we were doing anything for clicking other links*/ }
- });
- if (pageurl.match(/posts\/new/)) {
- var post = GM_getValue("quoteid");
- var username = GM_getValue("quotename");
- quote = "> [" + username + "](" + post + ") \n> ";
- console.log(quote);
- var message = "\n" + document.getElementById('forum_post_message').val;
- console.log(message);
- document.getElementById('forum_post_message').value = quote + message;
- }
- });
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement