notify.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. channel: 'chromeExtend'
  51. }).setOnMsgListener('notify', ['open', 'unread', 'user'], function (msgDetail) {
  52. let body = msgDetail.body;
  53. if (['taskA'].indexOf(body.type) !== -1) {
  54. return;
  55. }
  56. var tempLists = $A.jsonParse($A.getStorage("configLists"), {});
  57. if (typeof tempLists[key] == "object" && tempLists[key].disabled === true) {
  58. return;
  59. }
  60. switch (msgDetail.messageType) {
  61. case 'open':
  62. instances[key].open = true;
  63. break;
  64. case 'unread':
  65. instances[key].unread = msgDetail.body.unread;
  66. break;
  67. case 'user':
  68. instances[key].unread++;
  69. chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
  70. if ($A.getHost(tabs[0].url) != key) {
  71. var opurl = 'http://' + key + '/todo?token=' + encodeURIComponent(instances[key].token) + '&open=chat';
  72. $A.showNotify(key, {
  73. body: $A.getMsgDesc(body),
  74. icon: body.userimg
  75. }, opurl);
  76. }
  77. });
  78. break;
  79. }
  80. updateBadgeNum();
  81. }).sendTo('unread', function (res) {
  82. if (res.status === 1) {
  83. instances[key].unread = $A.runNum(res.message);
  84. updateBadgeNum();
  85. }
  86. });
  87. }
  88. }
  89. //
  90. tagId++;
  91. const tmpID = tagId;
  92. setTimeout(function () {
  93. if (tmpID === tagId) {
  94. getBadgeNum();
  95. }
  96. }, 5000);
  97. }
  98. getBadgeNum();
  99. /**
  100. * 监听来自网站的会员信息
  101. */
  102. chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
  103. var configLists;
  104. if (request.act === "config") {
  105. if (sender.tab) {
  106. var hostname = $A.getHost(sender.tab.url);
  107. if (hostname) {
  108. configLists = $A.jsonParse($A.getStorage("configLists"), {});
  109. if (typeof configLists[hostname] !== "object") {
  110. configLists[hostname] = {};
  111. }
  112. configLists[hostname] = Object.assign(request.config, {
  113. hostname: hostname,
  114. });
  115. $A.setStorage("configLists", $A.jsonStringify(configLists));
  116. sendResponse(configLists);
  117. }
  118. }
  119. } else if (request.act === "getInstances") {
  120. sendResponse(instances);
  121. } else if (request.act === "clickInstances") {
  122. if (typeof instances[request.index] === "object") {
  123. instances[request.index].ws.sendTo('unread', function (res) {
  124. if (res.status === 1) {
  125. instances[request.index].unread = $A.runNum(res.message);
  126. updateBadgeNum();
  127. }
  128. sendResponse(res);
  129. })
  130. }
  131. } else if (request.act === "delInstances") {
  132. configLists = $A.jsonParse($A.getStorage("configLists"), {});
  133. if (typeof configLists[request.index] === "object") {
  134. delete configLists[request.index];
  135. $A.setStorage("configLists", $A.jsonStringify(configLists));
  136. }
  137. if (typeof instances[request.index] === "object") {
  138. if (typeof instances[request.index].ws !== "undefined") {
  139. instances[request.index].ws.config(null).close();
  140. }
  141. delete instances[request.index];
  142. }
  143. updateBadgeNum();
  144. }
  145. });