Notifications

show desktop notifications

  1. // jscs:disable
  2. // ==UserScript==
  3. // @name Notifications
  4. // @namespace https://*.waysofhistory.com/
  5. // @version 0.5
  6. // @description show desktop notifications
  7. // @author menya
  8. // @match https://*.waysofhistory.com/*
  9. // @exclude https://ruforum.waysofhistory.com/
  10. // @exclude https://ru.waysofhistory.com/
  11. // @grant none
  12. // ==/UserScript==
  13.  
  14. (function () {
  15.  
  16. var REPORT_TYPE = {
  17. 0: 'Научный отчет',
  18. 1: 'Основание нового города',
  19.  
  20. 2: 'Финансы',
  21. 6: 'Финансы',
  22. 7: 'Финансы',
  23. 17: 'Финансы',
  24. 18: 'Финансы',
  25. 31: 'Финансы',
  26.  
  27. 3: 'Военный отчет',
  28. 4: 'Дипломатия',
  29. 5: 'Остановка мира',
  30.  
  31. 8: 'Торговый отчет',
  32. 16: 'Торговый отчет',
  33. 25: 'Торговый отчет',
  34. 26: 'Торговый отчет',
  35. 30: 'Торговый отчет',
  36.  
  37. 9: 'Платеж',
  38. 10: 'Ветер Удачи',
  39. 11: 'Перечисление Монет Удачи',
  40. 12: 'Торговое предложение',
  41. 13: 'Заселение',
  42. 14: 'Поздравление',
  43. 15: 'Бонус-код',
  44. 19: 'Военный отчет',
  45. 20: 'Военный отчет',
  46. 21: 'Военный отчет',
  47. 22: 'Отчет о подкреплении',
  48. 23: 'Отчет казначейства',
  49. 24: 'Отчет о транспортировке',
  50. 27: 'Голод',
  51. 28: 'Дипломатия',
  52. 29: 'Разведка',
  53. 32: 'Управление городом'
  54. };
  55.  
  56. // Сделаем себе царь-аккаунт
  57. Account.prototype.isPremium = function () {
  58. return true;
  59. };
  60.  
  61. // вырубим "Ты не одинок"
  62. Appl.prototype.initRefNotif = function () {
  63. return true;
  64. };
  65.  
  66. // Запросим разрешение на выдачу уведомлений
  67. Notification.requestPermission();
  68.  
  69. // функция для показа оповещения
  70. function notif(title, body) {
  71. var currentdate = new Date();
  72. var datetime = currentdate.getDate() + " " +
  73. currentdate.getHours() + ":" +
  74. currentdate.getMinutes() + ":" +
  75. currentdate.getSeconds() + ' ';
  76. var newNotif = new Notification(datetime + title, {
  77. icon: 'https://lh6.googleusercontent.com/V0Va2fD54HtOr1N7z6Mv_XSZQBBQdxA-7susY75wwd5NbbRxEsI4obfUO71IyMoMMjH85DvH=s50-h50-e365-rw',
  78. requireInteraction: true,
  79. body: body
  80. });
  81. // скрываем по клику
  82. newNotif.addEventListener('click', function () {
  83. newNotif.close();
  84. });
  85. }
  86.  
  87. var initCheck = setInterval(function () {
  88. if (webSocketMgr.dataInited) {
  89. clearTimeout(initCheck);
  90. for (var town in wofh.towns) {
  91. localStorage.setItem(town, JSON.stringify(wofh.towns[town].build));
  92. }
  93. }
  94. }, 400);
  95.  
  96. function wsdecorator(func) {
  97. return function () {
  98. try {
  99.  
  100. var message = JSON.parse(arguments[0].data);
  101.  
  102. switch (message.type) {
  103. case 10:
  104.  
  105. if (message.data.writer[0] !== servodata.account.id) {
  106. notif('Новое сообщение от ' + message.data.writer[1], message.data.text);
  107. }
  108.  
  109. break;
  110. case 3:
  111. if ([8, 16, 25, 26, 30, 24].indexOf(message.data.type) == -1) {
  112. notif('Новый отчет: "' + REPORT_TYPE[message.data.type] + ' "');
  113. }
  114. break;
  115. case 8:
  116. if (message.data.quests) {
  117. notif('Выполнили или появился новый квест!');
  118. }
  119. break;
  120. case 14:
  121.  
  122. if (message.data.clickers && message.data.clickers.add) {
  123. notif(wofh.towns[message.data.id].name + ' Новые таблички!', JSON.stringify(message.data.clickers));
  124. }
  125.  
  126. if (message.data.army) {
  127. notif(wofh.towns[message.data.id].name + ' Завершили тренировку войск!');
  128. }
  129.  
  130. if (message.data.build) {
  131. var saved = JSON.parse(localStorage.getItem(message.data.id));
  132.  
  133. for (var itown in message.data.build.state) {
  134. if ((saved.state[itown] && (message.data.build.state[itown][1] != saved.state[itown][1])) || !saved.state[itown]) {
  135. localStorage.setItem(message.data.id, JSON.stringify(message.data.build));
  136. notif(wofh.towns[message.data.id].name + ' Достроили здание:',
  137. '"' + Build.lib[message.data.build.state[itown][0]].name + ' - ' +
  138. message.data.build.state[itown][1] + ' уровень. ' + itown + ' квартал"');
  139. }
  140. }
  141.  
  142. }
  143.  
  144. break;
  145. }
  146.  
  147. } catch (e) {
  148. notif('Возникла ошибка:', e);
  149. } finally {
  150. func.apply(this, arguments);
  151. }
  152.  
  153. };
  154. }
  155.  
  156. try {
  157. webSocketMgr.ws.onmessage = wsdecorator(webSocketMgr.ws.onmessage);
  158. } catch (e) {
  159. notif('Возникла ошибка:', e);
  160. }
  161.  
  162. })();