notify.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * 连接已有会员
  3. * @type {*}
  4. */
  5. var tagId = 0;
  6. const instances = {};
  7. const updateBadgeNum = function () {
  8. var badgeNum = 0;
  9. for (var index in instances) {
  10. if (!instances.hasOwnProperty(index)) {
  11. continue;
  12. }
  13. badgeNum+= instances[index].unread;
  14. }
  15. if (badgeNum == 0) {
  16. chrome.browserAction.setBadgeText({text: ''});
  17. } else {
  18. chrome.browserAction.setBadgeText({text: (badgeNum > 99 ? '99+' : badgeNum) + ''});
  19. }
  20. chrome.runtime.sendMessage({
  21. act: 'instances',
  22. instances: instances
  23. });
  24. }
  25. const getBadgeNum = function () {
  26. var configLists = $A.jsonParse($A.getStorage("configLists"), {});
  27. for (var index in configLists) {
  28. if (!configLists.hasOwnProperty(index)) {
  29. continue;
  30. }
  31. const key = index;
  32. const config = configLists[key];
  33. if (typeof instances[key] === "undefined") {
  34. instances[key] = {};
  35. }
  36. //
  37. if (instances[key].username !== config.username) {
  38. instances[key].username = config.username;
  39. instances[key].token = config.token;
  40. instances[key].unread = 0;
  41. instances[key].open = false;
  42. updateBadgeNum();
  43. if (typeof instances[key].ws !== "undefined") {
  44. instances[key].ws.config(null).close();
  45. }
  46. instances[key].ws = new WTWS({
  47. username: config.username,
  48. token: config.token,
  49. url: config.url,
  50. key: key,
  51. channel: 'chromeExtend'
  52. }).setOnMsgListener('notify', ['open', 'unread', 'user'], function (msgDetail) {
  53. let body = msgDetail.body;
  54. if (['taskA'].indexOf(body.type) !== -1) {
  55. return;
  56. }
  57. var tempLists = $A.jsonParse($A.getStorage("configLists"), {});
  58. if (typeof tempLists[key] == "object" && tempLists[key].disabled === true) {
  59. return;
  60. }
  61. switch (msgDetail.messageType) {
  62. case 'open':
  63. instances[key].open = true;
  64. break;
  65. case 'unread':
  66. instances[key].unread = msgDetail.body.unread;
  67. break;
  68. case 'user':
  69. instances[key].unread++;
  70. chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
  71. if ($A.getHost(tabs[0].url) != key) {
  72. var opurl = 'http://' + key + '/todo?token=' + encodeURIComponent(instances[key].token) + '&open=chat';
  73. $A.showNotify(key, {
  74. body: $A.getMsgDesc(body),
  75. icon: body.userimg
  76. }, opurl);
  77. }
  78. });
  79. break;
  80. }
  81. updateBadgeNum();
  82. }).sendTo('unread', function (res) {
  83. if (res.status === 1) {
  84. instances[key].unread = $A.runNum(res.message);
  85. updateBadgeNum();
  86. }
  87. });
  88. }
  89. }
  90. //
  91. tagId++;
  92. const tmpID = tagId;
  93. setTimeout(function () {
  94. if (tmpID === tagId) {
  95. getBadgeNum();
  96. }
  97. }, 5000);
  98. }
  99. getBadgeNum();
  100. /**
  101. * 监听来自网站的会员信息
  102. */
  103. chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
  104. var configLists;
  105. switch (request.act) {
  106. case "config":
  107. if (sender.tab) {
  108. var hostname = $A.getHost(sender.tab.url);
  109. if (hostname) {
  110. configLists = $A.jsonParse($A.getStorage("configLists"), {});
  111. if (typeof configLists[hostname] !== "object" || configLists[hostname] === null) {
  112. configLists[hostname] = {};
  113. }
  114. configLists[hostname] = Object.assign(configLists[hostname], request.config, {
  115. hostname: hostname,
  116. });
  117. $A.setStorage("configLists", $A.jsonStringify(configLists));
  118. sendResponse(configLists);
  119. }
  120. }
  121. break;
  122. case "getInstances":
  123. sendResponse(instances);
  124. break;
  125. case "clickInstances":
  126. if (typeof instances[request.index] === "object") {
  127. instances[request.index].ws.sendTo('unread', function (res) {
  128. if (res.status === 1) {
  129. instances[request.index].unread = $A.runNum(res.message);
  130. updateBadgeNum();
  131. }
  132. sendResponse(res);
  133. })
  134. }
  135. break;
  136. case "delInstances":
  137. configLists = $A.jsonParse($A.getStorage("configLists"), {});
  138. if (typeof configLists[request.index] === "object") {
  139. delete configLists[request.index];
  140. $A.setStorage("configLists", $A.jsonStringify(configLists));
  141. }
  142. if (typeof instances[request.index] === "object") {
  143. if (typeof instances[request.index].ws !== "undefined") {
  144. instances[request.index].ws.config(null).close();
  145. }
  146. delete instances[request.index];
  147. }
  148. updateBadgeNum();
  149. break;
  150. }
  151. });