popup.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. });
  41. html+= '<li class="message_box' + (j == length ? ' last' : '') + '" data-index="' + index + '" data-token="' + item.token + '">';
  42. if (item.nickname) {
  43. html+= `<div class="message_username">${item.nickname} (${item.username})</div>`;
  44. } else {
  45. html+= '<div class="message_username">' + item.username + '</div>';
  46. }
  47. html+= '<div class="message_host">' + index + '</div>';
  48. html+= '<div class="message_unread' + (item.unread == 0 ? ' zero' : '') + '">未读: ' + item.unread + '</div>';
  49. html+= '<div class="message_delete">删除</div>';
  50. html+= '</li>';
  51. j++;
  52. }
  53. if (!html) {
  54. html+= '<li class="message_box">';
  55. html+= '<div class="message_loading">没有相关的记录!</div>';
  56. html+= '</li>';
  57. }
  58. $("#message_div").html('<ul class="message_lists">' + html + '</ul>');
  59. $("div.message_delete").click(function(){
  60. if (confirm("确定要删除此记录吗?")) {
  61. onDelete($(this).parents("li").attr("data-index"));
  62. }
  63. });
  64. $("div.message_unread,div.message_host").click(function(){
  65. const index = $(this).parents("li").attr("data-index");
  66. const token = encodeURIComponent($(this).parents("li").attr("data-token"));
  67. const opurl = 'http://' + index + '/todo?token=' + token + '&open=chat';
  68. chrome.tabs.query({}, function (tabs) {
  69. var has = false;
  70. tabs.some(function (item) {
  71. if ($A.getHost(item.url) == index) {
  72. var url = $A.getPathname(item.url) == '/' ? opurl : ($A.urlAddParams($A.removeURLParameter(item.url, ['open', 'rand']), {open: 'chat', rand: Math.round(new Date().getTime())}))
  73. chrome.windows.update(item.windowId, {focused: true});
  74. chrome.tabs.highlight({tabs: item.index, windowId: item.windowId});
  75. chrome.tabs.update({url: url});
  76. onClick(index);
  77. return has = true;
  78. }
  79. });
  80. if (!has) {
  81. chrome.tabs.create({ url: opurl });
  82. onClick(index);
  83. }
  84. });
  85. })
  86. }