Gamefaqs Quick PM and Quick Edit

PM and edit from within topic in gamefaqs

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

  1. // ==UserScript==
  2. // @name Gamefaqs Quick PM and Quick Edit
  3. // @namespace N-eil
  4. // @version 0.8.3
  5. // @description PM and edit from within topic in gamefaqs
  6. // @include *.gamefaqs.com/boards/*
  7. // @grant none
  8. // ==/UserScript==
  9.  
  10. var reg = /\/(\d+)/g //Makes a regex to get board and message ID from the url
  11. , boardID = reg.exec(location.href)[1]
  12. , topicID = reg.exec(location.href)[1];
  13.  
  14. var key; //Gfaqs has a hidden field called "key" that is required to be filled with a certain code(different for each user) in order to post/edit/pm
  15.  
  16. if(document.getElementsByName('key').length) //Look for the key on the page: quickposting is enabled
  17. {
  18. key = document.getElementsByName('key')[0].value;
  19. addLinks();
  20. }
  21. else
  22. {
  23. $.post("/boards/post.php?board=" + boardID + "&topic=" + topicID, {}).done(function(response){ //Otherwise, look for the key requesting a separate post message page
  24. key = response.match(/key" value="([^"]*)"/)[1];
  25. addLinks();
  26. });
  27. }
  28. //If neither method can find the key, don't bother adding the quick PM and edit links since they cannot function properly
  29.  
  30.  
  31. function addLinks(){
  32. var username = $(". a:first").html();
  33. username = username.substring(0,username.indexOf('(')-1);
  34. var $details = $(".msg_stats_left")
  35. , displayLeft = true;
  36. if (!$details.length){ //If nothing was found, they must have user details displayed above the message
  37. $details = $(".msg_stats");
  38. displayLeft = false;
  39. }
  40. $details.each(function(index, el) {
  41. var $el = $(el);
  42. if ($el.html().match(username))
  43. { //Ones with your username in them are your posts, and can be edited
  44. // $.post($el.find("a[title='Detail']").attr("href"), {}).done(function(response){ //Makes a request to the message detail page
  45. // if(response.match(/name="YES" value="Edit/)) //Looks in the response for the edit button, this could be tricked if you wrote the regex in the post but I don't think anyone would do that ever
  46. //{
  47. var editLink = $("<a> Edit </a>");
  48. editLink.click(function() {
  49. if (displayLeft)
  50. showEditWindow($(el).closest(".msg").find(".msg_body").clone());
  51. else
  52. showEditWindow($(el).closest(".top").next().find(".msg_body").clone());
  53. });
  54. $el.append(editLink);
  55. //}
  56. //});
  57. }
  58. else
  59. { //Other ones are posts from other users, and they can be PM'd
  60. var pmLink = $("<a> PM </a>");
  61. pmLink.click(function() {showPMWindow($el.find("a.name").html());});
  62. $el.append(pmLink);
  63. }
  64. });
  65. }
  66.  
  67. function replaceButtons(){} //Placeholder function, modified when the edit window appears to replace the buttons in the relevant place
  68.  
  69. function createPopup(text){
  70. replaceButtons();
  71. $("#popup-window").remove();
  72. var $window = $("<div id='popup-window'> " + text + " </div>")
  73. .css("left", "30%")
  74. .css("top","30%")
  75. .css("position", "fixed")
  76. .toggleClass("reg_dialog", true);
  77. $("body").prepend($window);
  78. return $window;
  79. }
  80.  
  81. function showPMWindow(name) {
  82. var $PMWindow = createPopup("Send a PM to " + name)
  83. , $subject = $("<div>Subject: <input type='text' maxlength='100' /></div>")
  84. , $message = $("<div><textarea rows ='" + Math.floor($(window).height() / 45) + "' cols='80' maxlength='1024'></textarea></div>");
  85. 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);})
  86. , $cancel = $("<button style='margin: 5px;'>Cancel</button>").click(function() {$PMWindow.remove();});
  87. $PMWindow.append($subject).append($message).append($send).append($cancel);
  88. }
  89.  
  90. function sendPM(name, subject, message) {
  91. name = name.slice(3,-4);
  92. $.post('/pm/new', {key: key, to: name, subject: subject, message: message, submit: 'Quick PM'}).done(function(){$("#popup-window textarea").val("PM sent.");});
  93. }
  94.  
  95. function showEditWindow(message) {
  96. function stripTags(index, el) {
  97. //Function to strip out tags like links, TTI images, etc from the message.
  98. var $el = $(el);
  99. console.log(el);
  100. if ($el.hasClass("fspoiler")) //Things hidden in spoilers get this class, but should be turned back into tags
  101. $el.replaceWith("<spoiler>" + $el.html() + "</spoiler>");
  102. else if ($el.is("img"))
  103. $el.replaceWith($el.attr("src"));
  104. else if ($el.is("a"))
  105. $el.replaceWith($el.attr("href"));
  106. else
  107. $el.replaceWith($el.html());
  108. }
  109.  
  110. //Parse the HTML message back into the way it looks while a user is typing
  111. message.html(message.html().replace(/<br>(?:<\/br>)?/g, '\n'));
  112. var tags = [1];
  113. while (tags.length) {
  114. tags = message.find(":not(b, i, code, blockquote, cite, spoiler)"); //Anything that isn't just a display tag has to have the original text stripped out of it to put in the edit messagebox
  115. tags.each(stripTags);
  116. }
  117. var messageID = message.attr("name")
  118. , $editWindow = createPopup("Edit your post")
  119. , $message = $("<div><textarea rows ='" + Math.floor($(window).height() / 45) + "' cols='80' maxlength='4096' name='messagetext'>" + message.html() + "</textarea></div>") //Height of textbox based roughly off height of screen, nothing exact but should ensure all the buttons are visible
  120. , $send = $("<button style='margin: 5px;'>Send</button>").click(function() {makeEdit($message.find("textarea").val(), boardID, topicID, messageID);})
  121. , $cancel = $("<button style='margin: 5px;'>Cancel</button>").click(function() {replaceButtons(); $editWindow.remove(); window.msgArea = document.getElementsByName('messagetext')[0];})
  122. , $buttons = $(".tagbuttons");
  123. if (!$buttons.length) //Either gameweasel or gamefox has replaced the html buttons with their own, so fetch those instead
  124. $buttons = $("#gamefox-html-buttons");
  125. var $buttonHolder = $buttons.prev();
  126. replaceButtons = function() {$buttonHolder.after($buttons);}; //Fills in the placeholder to replace the desired buttons, called either on cancelling the edit or when you edit another post
  127. $editWindow.append($("</br>")).append($buttons).append($message).append($send).append($cancel);
  128. window.msgArea = document.getElementsByName('messagetext')[0];
  129.  
  130. }
  131.  
  132. function makeEdit(message, board, topic, ID) {
  133. var url = "/boards/post.php?board=" + board + "&topic=" + topic + "&message=" + ID;
  134. $.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.");});
  135. }