popup.js 2.9 KB

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