Thread reply button for Lithium communities

Add a "Thread Reply" button to forum post pages on many Lithium-based forums. This button initiates a reply to the original post instead of the linked post, even if you are not at the first page.

目前為 2014-07-21 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Thread reply button for Lithium communities
  3. // @namespace http://sites.google.com/site/cerisesorbet/
  4. // @description Add a "Thread Reply" button to forum post pages on many Lithium-based forums. This button initiates a reply to the original post instead of the linked post, even if you are not at the first page.
  5. // @include http://lithosphere.lithium.com/*
  6. // @include http://bookclubs.barnesandnoble.com/*
  7. // @include http://forums.verizon.com/*
  8. // @include http://boards.adultswim.com/*
  9. // @include http://community.secondlife.com/*
  10. // @version 20140721
  11. // @license MIT License http://nrm.mit-license.org/2012
  12. // @copyright (c) 2012-2014 Cerise Sorbet
  13. // ==/UserScript==
  14.  
  15. // return global thread ID, or 0 if missing
  16. function GetReplyThreadNumber() {
  17. var links = document.getElementsByTagName('link');
  18. if (links.length) {
  19. var x; for (x = 0; x < links.length; x++) {
  20. if (links[x].rel == 'canonical') {
  21. var pathChop = links[x].href.split('/');
  22. var tdpIndex = pathChop.indexOf('td-p');
  23. if (~tdpIndex)
  24. return Number(pathChop[tdpIndex + 1]);
  25. }
  26. }
  27. }
  28. return 0;
  29. }
  30.  
  31. function MakeThreadReplyButton() {
  32. // Is there an active reply button? If not, give up.
  33. var replySpan = document.getElementsByClassName('primary-action message-reply');
  34. if (replySpan.length) {
  35. var replyLink = replySpan[0].getElementsByTagName('a');
  36. if (replyLink.length == 0)
  37. return;
  38. }
  39. else
  40. return;
  41.  
  42. var replyThreadNumber = GetReplyThreadNumber();
  43. if (replyThreadNumber) {
  44. // create the new button
  45. var threadReplyButton = document.createElement('span');
  46. threadReplyButton.className = 'primary-action';
  47. threadReplyButton.innerHTML = '<a class="lia-button lia-button-primary" style="margin-right: 10px" id="cerise-thread-reply-'
  48. + replyThreadNumber + '" rel="nofollow:"><span>Thread Reply</span></button>';
  49.  
  50. // squeeze in the new one
  51. var bottomBar = document.getElementsByClassName("lia-menu-bar lia-menu-bar-bottom");
  52. if (bottomBar.length) {
  53. var buttonDiv = bottomBar[0].getElementsByClassName('lia-menu-bar-buttons');
  54. if (buttonDiv.length) {
  55. buttonDiv[0].style.display = ''; // div is there but typically hidden
  56. buttonDiv[0].appendChild(threadReplyButton);
  57.  
  58. // The button is installed, so add an event handler.
  59. threadReplyButton.firstChild.addEventListener('click', DoThreadReply, true);
  60. }
  61. }
  62. }
  63. }
  64.  
  65. function DoThreadReply() {
  66. var threadNumber = this.id.replace(/^cerise-thread-reply-/, '');
  67.  
  68. // Get the board_id to build a reply URL
  69. var XMLReq = new XMLHttpRequest();
  70. var XMLhref = window.location.protocol + '//' + window.location.hostname
  71. + '/restapi/vc/messages/id/' + threadNumber + '?xslt=json.xsl&amp;restapi.response_style=view';
  72. XMLReq.open('GET', XMLhref, true);
  73. XMLReq.onreadystatechange = function(e) {
  74. if (XMLReq.readyState == 4) {
  75. if(XMLReq.status != 200) { // HTTP error
  76. alert('Unable to get reply link, HTTP error ' + XMLReq.status);
  77. }
  78. else {
  79. try {
  80. var article = JSON.parse(XMLReq.responseText);
  81. }
  82. catch(err) {
  83. alert("Unable to get reply link, can't parse server response.");
  84. return;
  85. }
  86.  
  87. if (!article.response.status) { // all responses should have this
  88. alert('Unable to get reply link, missing REST response status');
  89. }
  90. else if (article.response.status != "success") { // internal Lithium error, like no permission or deleted message
  91. errorText = article.response.error.code ? '[' + article.response.error.code + '] ' : '[unknown] ';
  92. if (article.response.error.message)
  93. errorText += article.response.error.message;
  94. alert("Can't get reply link, Lithium error:\n" + errorText);
  95. }
  96. else { // must be a success...
  97. var message = article.response.message;
  98. if (!message.board_id.$)
  99. alert("Can't get message ID for thread reply");
  100. else if (!message.board.href)
  101. alert("Can't get board ID for thread reply");
  102. else {
  103. window.location.href = window.location.protocol + '//' + window.location.hostname
  104. + '/t5/forums/replypage/board-id/'
  105. + message.board.href.split('/').pop() + '/message-id/' + message.board_id.$;
  106. }
  107. }
  108. }
  109. }
  110. };
  111.  
  112. XMLReq.send(null);
  113. }
  114.  
  115. MakeThreadReplyButton();