Thunder Notification

服务器是雷暴天气时弹出桌面通知

  1. // ==UserScript==
  2. // @name Thunder Notification
  3. // @version 0.4
  4. // @description 服务器是雷暴天气时弹出桌面通知
  5. // @homepage https://woozy.im/
  6. // @include http://map.xjcraft.org/*
  7. // @author woozy
  8. // @grant none
  9. // @namespace https://greasyfork.org/users/8206
  10. // ==/UserScript==
  11.  
  12. if (!('Notification' in window)) {
  13. alert('该浏览器不支持桌面通知');
  14. } else if (Notification.permission !== 'granted') {
  15. Notification.requestPermission();
  16. }
  17.  
  18. var api = 'http://map.xjcraft.org/up/world/MainLand/';
  19. var nowState = false;
  20. var notification;
  21. setInterval(function() {
  22. var x = new XMLHttpRequest();
  23. x.open('GET', api, 1);
  24. x.onreadystatechange = function() {
  25. if (x.readyState == 4 && x.status == 200) {
  26. var data = JSON.parse(x.responseText);
  27. if (data.isThundering && data.hasStorm) {
  28. if (nowState != data.isThundering) {
  29. nowState = data.isThundering;
  30. var t = new Date().toTimeString().match(/(.*) GMT/)[1];
  31. notification = new Notification(t + ' - 打雷啦~');
  32. }
  33. } else {
  34. if (nowState != data.isThundering) {
  35. nowState = data.isThundering;
  36. notification.close();
  37. }
  38. }
  39. }
  40. }
  41. x.send(null);
  42. }, 10000);