2ch-notifications

Появляется уведомление, когда появился новый ответ на твой пост в треде, где ты отписался

  1. // ==UserScript==
  2. // @name 2ch-notifications
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Появляется уведомление, когда появился новый ответ на твой пост в треде, где ты отписался
  6. // @author user661
  7. // @match https://*/*
  8. // @grant GM_setValue
  9. // @grant GM_getValue
  10. // @grant GM_notification
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. var getJSON = function(url, callback) {
  15. var xhr = new XMLHttpRequest();
  16. xhr.open('GET', url, true);
  17. xhr.responseType = 'json';
  18. xhr.onload = function() {
  19. var status = xhr.status;
  20. if (status === 200) {
  21. callback(null, xhr.response);
  22. } else {
  23. callback(status, xhr.response);
  24. }
  25. };
  26. xhr.send();
  27. };
  28.  
  29. var getReMyPosts = function() {
  30. var myPosts = JSON.parse(localStorage['de-myposts']);
  31. let reMyPosts = {};
  32. for (const key in myPosts) {
  33. reMyPosts[key] = {};
  34. }
  35. for (const key in myPosts) {
  36. for (const key2 in myPosts[key]) {
  37. reMyPosts[key][myPosts[key][key2][1]] = [];
  38. }
  39. for (const key2 in myPosts[key]) {
  40. reMyPosts[key][myPosts[key][key2][1]][reMyPosts[key][myPosts[key][key2][1]].length ? reMyPosts[key][myPosts[key][key2][1]].length : 0] = key2;
  41. }
  42. }
  43. return reMyPosts;
  44. }
  45.  
  46. var timerIds = [];
  47. if (!GM_getValue("not-first")) {
  48. GM_setValue("posts-viewed", "");
  49. }
  50. GM_setValue("not-first", true);
  51.  
  52. if (location.hostname === '2ch.hk') {
  53. for (var i = 0; i < timerIds.length; i++) {
  54. clearInterval(timerIds[i]);
  55. }
  56. let reMyPosts = getReMyPosts();
  57. console.log(reMyPosts);
  58. for (const board in reMyPosts) {
  59. for (const thread in reMyPosts[board]) {
  60. var minimum = Infinity;
  61. for (var val of reMyPosts[board][thread]) {
  62. if (val < minimum) {
  63. minimum = val;
  64. }
  65. }
  66. const MINIMUM = minimum;
  67. timerIds.push(setInterval(function(){
  68. getJSON('https://2ch.hk/makaba/mobile.fcgi?task=get_thread&board=' + board + '&thread=' + thread + '&num=' + MINIMUM,
  69. function(err, data) {
  70. if (err !== null) {
  71. console.log('Something went wrong: ' + err);
  72. } else {
  73. let reMyPosts = getReMyPosts();
  74. //console.log(reMyPosts);
  75. //console.log(MINIMUM);
  76. for (var i = 0; i < data.length; i++) {
  77. var regExp = />>>([0-9]+)<\/a>/g;
  78. var matches = [];
  79. var m = regExp.exec(data[i]["comment"]);
  80. while (m !== null) {
  81. matches.push(m[1]);
  82. m = regExp.exec(data[i]["comment"]);
  83. }
  84. //console.log(matches);
  85. //console.log(data[i]);
  86. if (matches) {
  87. var hasMatch = false;
  88. for (var k = 0; k < matches.length; k++) {
  89. console.log(reMyPosts[board][thread]);
  90. console.log(matches[k]);
  91. if (reMyPosts[board][thread].includes(matches[k])) {
  92. //console.log("ok");
  93. hasMatch = true;
  94. break;
  95. }
  96. }
  97. //console.log(GM_getValue("posts-viewed"));
  98. if (hasMatch) {
  99. var postsViewed = GM_getValue("posts-viewed").split(",");
  100. //console.log(postsViewed);
  101. //console.log(data[i]["num"]);
  102. if (!postsViewed.includes(data[i]["num"].toString())) {
  103. //console.log(data[i]["num"]);
  104. const j = i;
  105. GM_notification ( {title: 'Новое сообщение!', text: "2ch.hk", onclick: () => {window.open("https://2ch.hk/" + board + "/res/" + thread + ".html#" + data[j]["num"]);}});
  106. GM_setValue("posts-viewed", GM_getValue("posts-viewed") + "," + data[i]["num"]);
  107. }
  108. }
  109. }
  110. }
  111. }
  112. });}, 20000));
  113. }
  114. }
  115. }
  116. })();