popup.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
  2. if (request.act === "instances") {
  3. showLists(request.instances);
  4. }
  5. });
  6. chrome.runtime.sendMessage({
  7. act: 'getInstances'
  8. }, function (response) {
  9. showLists(response);
  10. });
  11. function onClick(index) {
  12. chrome.runtime.sendMessage({
  13. act: 'clickInstances',
  14. index: index,
  15. });
  16. }
  17. function onDelete(index) {
  18. chrome.runtime.sendMessage({
  19. act: 'delInstances',
  20. index: index,
  21. });
  22. }
  23. function showLists(lists) {
  24. var html = '';
  25. var j = 1;
  26. var length = Object.keys(lists).length;
  27. var tempLists = $A.jsonParse($A.getStorage("configLists"), {});
  28. for (var index in lists) {
  29. if (!lists.hasOwnProperty(index)) {
  30. continue;
  31. }
  32. if (tempLists[index] === null || typeof tempLists[index] !== "object") {
  33. continue;
  34. }
  35. if (tempLists[index].disabled === true) {
  36. continue;
  37. }
  38. const item = Object.assign(lists[index], {
  39. nickname: tempLists[index].nickname,
  40. online: tempLists[index].online,
  41. });
  42. html+= '<li class="message_box' + (j == length ? ' last' : '') + '" data-index="' + index + '" data-token="' + item.token + '">';
  43. if (item.nickname) {
  44. html+= `<div class="message_username">${item.nickname} (${item.username})</div>`;
  45. } else {
  46. html+= '<div class="message_username">' + item.username + '</div>';
  47. }
  48. html+= '<div class="message_host">' + index + '</div>';
  49. html+= '<div class="message_unread' + (item.unread == 0 ? ' zero' : '') + '' + (item.online === true ? '' : ' offline') + '">未读: ' + item.unread + '</div>';
  50. html+= '<div class="message_delete">删除</div>';
  51. html+= '</li>';
  52. j++;
  53. }
  54. if (!html) {
  55. html+= '<li class="message_box">';
  56. html+= '<div class="message_loading">没有相关的记录!</div>';
  57. html+= '</li>';
  58. }
  59. $("#message_div").html('<ul class="message_lists">' + html + '</ul>');
  60. $("div.message_delete").click(function(){
  61. if (confirm("确定要删除此记录吗?")) {
  62. onDelete($(this).parents("li").attr("data-index"));
  63. }
  64. });
  65. $("div.message_unread,div.message_username,div.message_host").click(function(e){
  66. const index = $(this).parents("li").attr("data-index");
  67. const token = encodeURIComponent($(this).parents("li").attr("data-token"));
  68. var opurl = 'http://' + index + '/todo?token=' + token;
  69. if (e.target.className == 'message_unread') {
  70. opurl+= '&open=chat'
  71. }
  72. chrome.tabs.query({}, function (tabs) {
  73. var has = false;
  74. tabs.some(function (item) {
  75. if ($A.getHost(item.url) == index) {
  76. var params = {rand: Math.round(new Date().getTime())};
  77. if (e.target.className == 'message_unread') {
  78. params.open = 'chat';
  79. }
  80. var url = $A.getPathname(item.url) == '/' ? opurl : ($A.urlAddParams($A.removeURLParameter(item.url, ['open', 'rand']), params))
  81. chrome.windows.update(item.windowId, {focused: true});
  82. chrome.tabs.highlight({tabs: item.index, windowId: item.windowId});
  83. chrome.tabs.update({url: url});
  84. onClick(index);
  85. return has = true;
  86. }
  87. });
  88. if (!has) {
  89. chrome.tabs.create({ url: opurl });
  90. onClick(index);
  91. }
  92. });
  93. })
  94. }