Starlog Group Sender

Add the ability to send blueprints (from any kind) to starlog groups

当前为 2015-11-07 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Starlog Group Sender
  3. // @namespace bitbucket.org/Odahviing
  4. // @include http://www.war-facts.com/player.php
  5. // @include http://www.war-facts.com/bptrade.php?*
  6. // @include http://www.war-facts.com/sdtrade.php?*
  7. // @description Add the ability to send blueprints (from any kind) to starlog groups
  8. // @version 1.1
  9. // @grant GM_getValue
  10. // @grant GM_setValue
  11. // ==/UserScript==
  12.  
  13. // Version 1.0 - Support on blueprint trade and ship design trade, not best implemented, will make it better in the future (use same button)
  14. // Version 1.1 - Integrated the two buttons to one, still need better response message
  15.  
  16. /* Settings */
  17. var ButtonName = "buttonGM";
  18. var PlayerSettings = "player";
  19. var BlueprintSendName = "bptrade";
  20. var BlueprintSendSelect = "bpplayer";
  21. var ShipDesignSendName = "sdtrade";
  22. var ShipDesignSendSelect = "sdplayer";
  23. /* End Settings */
  24. function saveGroups()
  25. {
  26. var groupsNames = document.getElementsByName('editstarloggroupname');
  27. var groupsId = document.getElementsByName('editstarloggroup');
  28. var arrayList = []
  29. for (var index = 0 ; index < groupsId.length; index ++)
  30. {
  31. var singleArray = [groupsId[index].value, groupsNames[index].value];
  32. var playersList = document.getElementById("starloggroup" + groupsId[index].value);
  33. var playersSpan = playersList.getElementsByTagName('span');
  34. for (var sIndex = 0; sIndex < playersSpan.length; sIndex ++)
  35. {
  36. var finalId = playersSpan[sIndex].id.substring(3, playersSpan[sIndex].id.indexOf('-'));
  37. singleArray.push(finalId);
  38. }
  39. arrayList.push(singleArray);
  40. }
  41. GM_setValue("stargroupId", arrayList);
  42. }
  43.  
  44. function addGroupsOptions(selectName)
  45. {
  46. var loadedData = GM_getValue("stargroupId");
  47. var select = document.getElementsByName(selectName);
  48. var docfrag = document.createDocumentFragment();
  49. for (var index = 0; index < loadedData.length; index ++)
  50. {
  51. docfrag.appendChild(
  52. new Option("Starlog Group: " + loadedData[index][1], loadedData[index][0]));
  53. }
  54. select[0].insertBefore(docfrag,select[0].firstChild);
  55. }
  56.  
  57. // Old Function - Version 1.0
  58. function drawButton(Name, Select)
  59. {
  60. var row = document.getElementsByTagName('form');
  61. row[0].innerHTML = row[0].innerHTML +
  62. "<input id='" + ButtonName + "' name='Submit' value='Send To Group' type='button'>";
  63. document.getElementById(ButtonName).addEventListener("click", function(){
  64. sendToGroup(Name, Select)},
  65. false);
  66. }
  67.  
  68. function changeButton(Name, Select)
  69. {
  70. var row = document.getElementsByName('Submit');
  71. row[0].type = "button";
  72. row[0].addEventListener("click", function(){
  73. sendToGroup(Name, Select)},
  74. false);
  75. }
  76.  
  77. function getInputValues(form, page)
  78. {
  79. var inputValues = form.getElementsByTagName('input');
  80. var values = [];
  81. switch(page)
  82. {
  83. case ShipDesignSendName:
  84. values = [inputValues[0].value, inputValues[1].value];
  85. return "Submit=Give&tradeid=" + values[0] + "&type=" + values[1] + "&" + ShipDesignSendSelect +"=";
  86. case BlueprintSendName:
  87. values = [inputValues[0].value, inputValues[1].value, inputValues[2].value];
  88. return "Submit=Give&tradeid=" + values[0] + "&type=" + values[1] + "&subtype=" + values[2] + "&" + BlueprintSendSelect + "=";
  89. }
  90. }
  91.  
  92. function sendToGroup(page, selector)
  93. {
  94. var loadedData = GM_getValue("stargroupId");
  95. var select = document.getElementsByName(selector);
  96. var regForm = document.getElementsByTagName('form');
  97.  
  98. var groupId = select[0].value;
  99. var neededIndex = -1;
  100. for (var index = 0; index < loadedData.length; index ++)
  101. {
  102. if (loadedData[index][0] == groupId)
  103. neededIndex = index;
  104. }
  105.  
  106. if (neededIndex == -1)
  107. {
  108. regForm[0].submit();
  109. return;
  110. }
  111. baseParams = getInputValues(regForm[0], page);
  112. var playerAmount = loadedData[neededIndex].length - 2;
  113. for (var sIndex = 0; sIndex < playerAmount; sIndex ++)
  114. {
  115. var params = baseParams + loadedData[neededIndex][sIndex + 2];
  116. xhttp = new XMLHttpRequest();
  117. xhttp.open("POST", page + ".php", true);
  118. xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  119. xhttp.send(params);
  120. }
  121. alert("Finish Sending Blueprint To '" +
  122. loadedData[neededIndex][1] +
  123. "' Group (" +
  124. playerAmount.toString() +
  125. " Players)");
  126. regForm[0].submit();
  127. }
  128.  
  129.  
  130. function main()
  131. {
  132. var page = document.URL.substring(document.URL.indexOf("/"));
  133. if (page.includes(PlayerSettings))
  134. {
  135. saveGroups();
  136. }
  137. else if (page.includes(BlueprintSendName))
  138. {
  139. changeButton(BlueprintSendName, BlueprintSendSelect);
  140. addGroupsOptions(BlueprintSendSelect);
  141. }
  142. else if (page.includes(ShipDesignSendName))
  143. {
  144. changeButton(ShipDesignSendName, ShipDesignSendName);
  145. addGroupsOptions(ShipDesignSendSelect);
  146. }
  147. }
  148.  
  149. main();