Gamefaqs Quick PM and Quick Edit

PM and edit from within topic in gamefaqs

当前为 2014-05-02 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name       Gamefaqs Quick PM and Quick Edit
// @namespace  N-eil
// @version    0.7
// @description  PM and edit from within topic in gamefaqs 
// @include *.gamefaqs.com/boards/*
// @grant		none
// ==/UserScript==

if(document.getElementsByName('key').length) // Looks for the key, which is only included on the page if quickpost is available. Without the key, nothing in this script works, so the buttons are not added.
											// There doesn't seem to be a realistic fix for this.  
{
    var username = $(".board_nav:first a:first").html();
    username = username.substring(0,username.indexOf('(')-1);
    
    var $details = $(".msg_stats_left")
    ,	displayLeft = true;
    
    if (!$details.length){ //If nothing was found, they must have user details displayed above the message
        $details = $(".msg_stats");
        displayLeft = false;
    }
    
    $details.each(function(index, el) {
        var $el = $(el);
        if ($el.html().match(username))
        { //Ones with your username in them are your posts, and can be edited
            var editLink = $("<a> Edit </a>");
            editLink.click(function() {
                if (displayLeft)
                    showEditWindow($(el).closest(".msg").find(".msg_body").clone());
                else
                    showEditWindow($(el).closest(".top").next().find(".msg_body").clone());
            });
            $el.append(editLink);
        }
        else 
        { //Other ones are posts from other users, and they can be PM'd 
            var pmLink = $("<a> PM </a>");   
            pmLink.click(function() {showPMWindow($el.find("a.name").html());});
            $el.append(pmLink);
        }
            
    });
}
function createPopup(text){
	$("#popup-window").remove();
	var $window = $("<div id='popup-window'> " + text + " </div>")
		.css("left", "30%")
		.css("top","30%")
		.css("position", "fixed")
		.toggleClass("reg_dialog", true);
    $("body").prepend($window);
	return $window; 
}

function showPMWindow(name) {
    var $PMWindow = createPopup("Send a PM to " + name)
    ,   $subject = $("<div>Subject: <input type='text' maxlength='100' /></div>")
    ,   $message = $("<div><textarea rows='20' cols='80' maxlength='1024'></textarea></div>");
    
    var $send = $("<button style='margin: 5px;'>Send</button>").click(function() {sendPM(name, $subject.find("input").val(), $message.find("textarea").val()); setTimeout(function() {$PMWindow.remove();},5000);})
    ,   $cancel = $("<button style='margin: 5px;'>Cancel</button>").click(function() {$PMWindow.remove();});
    
    $PMWindow.append($subject).append($message).append($send).append($cancel);
}

function sendPM(name, subject, message) {
    var key = document.getElementsByName( 'key' )[0].value ; //A key unique to users required to post/PM
    name = name.slice(3,-4);
    $.post('/pm/new', {key: key, to: name, subject: subject, message: message, submit: 'Quick PM'}).done(function(){$("#popup-window textarea").val("PM sent.");});
}

function showEditWindow(message) {
        
    function stripTags(index, el) {
        //Function to strip out tags like links, TTI images, etc from the message.
            var $el = $(el);
            console.log(el);
            if ($el.hasClass("fspoiler"))  //Things hidden in spoilers get this class, but should be turned back into tags
                $el.replaceWith("<spoiler>" + $el.html() + "</spoiler>");
            else if ($el.is("img"))
                $el.replaceWith($el.attr("src"));
            else
                $el.replaceWith($el.html());
    }

    
    //Parse the HTML message back into the way it looks while a user is typing
    message.html(message.html().replace(/<br>(?:<\/br>)?/g, '\n'));
    var tags = [1];
    while (tags.length) {
        tags = message.find("a, span, img, video");
        tags.each(stripTags);
    }
    var reg = /\/(\d+)/g //Makes a regex to get board and message ID from the url
    ,	boardID = reg.exec(location.href)[1]
    ,	topicID = reg.exec(location.href)[1]
    ,	messageID = message.attr("name");    
	
    var $editWindow = createPopup("Edit your post")
    ,   $message = $("<div><textarea rows='20' cols='80' maxlength='4096' name='messagetext'>" + message.html() + "</textarea></div>")
    ,	$send = $("<button style='margin: 5px;'>Send</button>").click(function() {makeEdit($message.find("textarea").val(), boardID, topicID, messageID);})
    ,   $cancel = $("<button style='margin: 5px;'>Cancel</button>").click(function() {$buttonHolder.after($buttons); $editWindow.remove(); window.msgArea = document.getElementsByName('messagetext')[0];})
    ,	$buttons = $(".tagbuttons");
    
    if (!$buttons.length) //Either gameweasel or gamefox has replaced the html buttons with their own, so fetch those instead
        $buttons = $("#gamefox-html-buttons");
    
    var $buttonHolder = $buttons.prev();
    
    $editWindow.append($("</br>")).append($buttons).append($message).append($send).append($cancel);
    window.msgArea = document.getElementsByName('messagetext')[0];

}

function makeEdit(message, board, topic, ID) {
    var url = "/boards/post.php?board=" + board + "&topic=" + topic + "&message=" + ID 
    ,	key = document.getElementsByName( 'key' )[0].value ; //A key unique to users required to post/PM
    $.post(url, {key: key, messagetext: message, post: 'Post without Preview'}).done(function() {location.reload();}).fail(function() {$("#popup-window textarea").val("Could not edit the post.  Are you sure you are within the 1 hour timeframe for edits?");});
}