GOTA Improvements

Various improvements to Game of Thrones: Ascent

目前为 2014-05-21 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name GOTA Improvements
  3. // @description Various improvements to Game of Thrones: Ascent
  4. // @namespace https://greasyfork.org/users/1665-nolana
  5. // @include http://gota.disruptorbeam.com/
  6. // @include http://gota-www.disruptorbeam.com/
  7. // @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
  8. // @require http://cdnjs.cloudflare.com/ajax/libs/moment.js/2.6.0/moment.min.js
  9. // @version 4
  10. // @grant GM_xmlhttpRequest
  11. // @grant unsafeWindow
  12. // ==/UserScript==
  13.  
  14. this.$ = this.jQuery = jQuery.noConflict(true);
  15.  
  16. // $('div.locked').remove();
  17. $(window).bind("load", function() {
  18. init();
  19. });
  20.  
  21.  
  22. function init() {
  23. $('#page-wrap').css('max-width',$(window).width()+'px');
  24. $('#holdings_container').width($(window).width()-10);
  25. // remove locked buildings
  26. // $(document).on('click','#navlink-buildings',function (e) {
  27. // $('#building_items div.locked').remove();
  28. //});
  29. fixChat();
  30. observeChat();
  31. addGlobalStyle(".attack_date { color: grey; margin-left: 4px; margin-top:2px; text-align: left}");
  32. $(document).on('click', '#incomingtab', function() {
  33. getIncoming();
  34. });
  35. }
  36. var incomingAttacks;
  37.  
  38. function getIncoming() {
  39. console.log('getting incoming');
  40. window.setTimeout(function () {
  41. GM_xmlhttpRequest({
  42. method: "GET",
  43. url: "http://gota.disruptorbeam.com/play/incoming_attacks",
  44. onload: function(response) {
  45. incomingAttacks = JSON.parse(response.responseText);
  46. insertAttackTimestamps(incomingAttacks);
  47. console.log("Incoming:"+response.responseText);
  48. }
  49. });
  50. }, 2000);
  51. }
  52.  
  53. function insertAttackTimestamps(a) {
  54. console.log('insert attack timestamps');
  55. // alert('bubu');
  56. $('div.perkscroll div.achiev-content').each(function() {
  57. $(this).remove('span.attack_date');
  58. var id = /[0-9]+/.exec($(this).find('div.increspond').attr('onclick'))
  59. var attack = a.attacks.filter(function(e){ return e.camp_attack_id === null ? e.pvp_id == id : e.camp_attack_id == id; })[0];
  60. var text = '' + moment(attack.attacker.created_at,"YYYY-MM-DD HH:mm:ss Z").local().format('MMMM Do YYYY, h:mm:ss a') + ' → ' + moment(attack.attacker.updated_at,"YYYY-MM-DD HH:mm:ss Z").local().format('MMMM Do YYYY, h:mm:ss a');
  61. $('<div class="attack_date">'+ text + '</div>').appendTo($(this));
  62. });
  63. unsafeWindow.$('div.perkscroll').data("jsp").reinitialise();
  64. //alert(unsafeWindow.$('div.perkscroll').data("jsp"));
  65. }
  66. function fixChat() {
  67. var chatWidth = $(window).width()/3;
  68. addGlobalStyle(".gamechat{bottom:auto;float:none;height:auto;left:auto;right:188px;margin-left:2px;margin-top:8px;padding:1px 10px 15px;position:absolute;top:3px;width:" + chatWidth +"px;"+
  69. "background:url(http://disruptorbeamcdn-01.insnw.net/images/pvp/actionbg.png?t=e2e9110ff577) repeat-x scroll 0 0 #000;border:1px solid #444;border-radius:12px;color:#FFF;font-size:16px;text-align:left;z-index:3}");
  70. addGlobalStyle("b {font-weight:bold}");
  71. addGlobalStyle("sub {font-size:11px;color:grey}");
  72. addGlobalStyle(".gamechat .chatscroll p { width: " + (chatWidth - 60) + "px;margin-bottom: 2px;font-size: 14px;");
  73. addGlobalStyle("#building_items div.locked { display:none; }");
  74. $('#combatlog').width(chatWidth - 10);
  75. $('#combatlog .jspPane p.pchat').each(function () {
  76. fixChatLine($(this));
  77. });
  78. }
  79.  
  80. function observeChat() {
  81. var target = $('#combatlog .jspPane')[0];
  82. // Create an observer instance
  83. var observer = new MutationObserver(function( mutations ) {
  84. mutations.forEach(function( mutation ) {
  85. var newNodes = mutation.addedNodes; // DOM NodeList
  86. if (newNodes !== null) { // If there are new nodes added
  87. var $nodes = $(newNodes); // jQuery set
  88. $nodes.each(function() {
  89. var $node = $(this);
  90. if ($node.hasClass( "pactivity" )) {
  91. console.log('act:'+$node.text());
  92. // TODO, fix activity line
  93. } else if ($node.hasClass("pchat")) {
  94. console.log('chat:' + $node.text());
  95. fixChatLine($node, new Date().toLocaleString());
  96. }
  97. });
  98. }
  99. });
  100. });
  101. var config = {
  102. attributes: false,
  103. childList: true,
  104. characterData: false
  105. };
  106. observer.observe(target, config);
  107. }
  108.  
  109. function fixChatLine(p, time) {
  110. var text = p.text();
  111. var result = /([^:]+):(.*)/.exec(text);
  112. if (result === null) return; // already highlighted
  113. var author = result[1];
  114. var line = result[2];
  115. p.find('strong').replaceWith('<strong><b>' + author + '</b>' + line + (time ? ' <sub>' + time + '</sub>' : ''));
  116. }
  117. function addGlobalStyle(css) {
  118. try {
  119. var elmHead, elmStyle;
  120. elmHead = document.getElementsByTagName('head')[0];
  121. elmStyle = document.createElement('style');
  122. elmStyle.type = 'text/css';
  123. elmHead.appendChild(elmStyle);
  124. elmStyle.innerHTML = css;
  125. } catch (e) {
  126. if (!document.styleSheets.length) {
  127. document.createStyleSheet();
  128. }
  129. document.styleSheets[0].cssText += css;
  130. }
  131. }