Epic battle notifier

Alert when epic battle detected, checks every 5 min. It does NOT fight for you, only notify.

目前为 2015-05-11 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Epic battle notifier
  3. // @author Rashe
  4. // @version 1.0
  5. // @description Alert when epic battle detected, checks every 5 min. It does NOT fight for you, only notify.
  6. // @match http://www.erepublik.com/*
  7. // @copyright lol what?
  8. // @noframes
  9. // @namespace https://greasyfork.org/users/11245
  10. // ==/UserScript==
  11.  
  12.  
  13. //Dom ready
  14. window.readyHandlers = [];
  15. window.ready = function ready(handler) {
  16. window.readyHandlers.push(handler);
  17. handleState();
  18. };
  19. window.handleState = function handleState() {
  20. if (['interactive', 'complete'].indexOf(document.readyState) > -1) {
  21. while (window.readyHandlers.length > 0) {
  22. window.readyHandlers.shift()();
  23. }
  24. }
  25. };
  26. document.onreadystatechange = window.handleState;
  27.  
  28. (function () {
  29. var epic_alert = function () {
  30. var _that = this;
  31. this.settings = {
  32.  
  33. debug: false,
  34. selectors: {
  35. epic_selector: 'isEpicBattle',
  36. epic_text: 'war_details_text'
  37. },
  38. urls: {
  39. war_url: 'http://www.erepublik.com/en/military/campaigns',
  40. battle_url_part: 'battlefield-new'
  41. },
  42. messages: {
  43. message: 'Hey Epic battle is waiting',
  44. epic_text_span: 'War: Epic battle '
  45. }
  46. };
  47.  
  48. this.init = function () {
  49. this.main.start();
  50. };
  51.  
  52. this.main = {
  53. start: function () {
  54. _that.main.wait_1min();
  55. },
  56. go_to: function () {
  57. if (window.location.href == _that.settings.urls.war_url) {
  58. _that.main.check_if_epic();
  59. }
  60. else if (_that.main.check_if_not_in_battle()) {
  61. _that.main.wait_20min();
  62. }
  63. else {
  64. window.location.href = _that.settings.urls.war_url;
  65. }
  66. },
  67. check_if_epic: function () {
  68. var elements = document.getElementsByClassName(_that.settings.selectors.epic_selector);
  69. if (elements.length > 0) {
  70. for (var i = 0; i < elements.length; i++) {
  71. var parent = elements[i].parentNode,
  72. value = parent.childNodes[0].textContent;
  73. if (value == _that.settings.messages.epic_text_span) {
  74. alert(_that.settings.messages.message);
  75. }
  76. }
  77. } else {
  78. _that.main.wait_5min();
  79. }
  80. },
  81. wait_1min: function () {
  82. setTimeout(function () {
  83. _that.main.go_to();
  84. }, 60 * 1000);
  85. },
  86. wait_5min: function () {
  87. setTimeout(function () {
  88. document.location.reload(true);
  89. }, 5 * 60 * 1000);
  90. },
  91. wait_20min: function () {
  92. setTimeout(function () {
  93. window.location.href = _that.settings.urls.war_url;
  94. }, 20 * 60 * 1000);
  95. },
  96. check_if_not_in_battle: function () {
  97. var url_parts_arr = window.location.pathname.split('/');
  98. return (url_parts_arr[3] == _that.settings.urls.battle_url_part);
  99. }
  100. };
  101. };
  102. window.epic_alert = new epic_alert();
  103. }());
  104.  
  105. ready(function () {
  106. epic_alert.init({debug: false});
  107. });