notify.js 4.6 KB

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