popup.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 onDelete(index) {
  12. chrome.runtime.sendMessage({
  13. act: 'delInstances',
  14. index: index,
  15. });
  16. }
  17. function showLists(lists) {
  18. var html = '';
  19. console.log(lists);
  20. for (var index in lists) {
  21. if (!lists.hasOwnProperty(index)) {
  22. continue;
  23. }
  24. const item = lists[index];
  25. html+= '<li class="message_box" data-index="' + index + '" data-token="' + item.token + '">';
  26. html+= '<div class="message_username">' + item.username + '</div>';
  27. html+= '<div class="message_host">' + index + '</div>';
  28. html+= '<div class="message_unread">未读: ' + item.unread + '</div>';
  29. html+= '<div class="message_delete">删除</div>';
  30. html+= '</li>';
  31. }
  32. if (!html) {
  33. html+= '<li class="message_box">';
  34. html+= '<div class="message_loading">没有相关的记录!</div>';
  35. html+= '</li>';
  36. }
  37. $("#message_div").html('<ul>' + html + '</ul>');
  38. $("div.message_delete").click(function(){
  39. if (confirm("确定要删除此记录吗?")) {
  40. onDelete($(this).parents("li").attr("data-index"));
  41. }
  42. });
  43. $("div.message_unread,div.message_host").click(function(){
  44. const index = $(this).parents("li").attr("data-index");
  45. const token = encodeURIComponent($(this).parents("li").attr("data-token"));
  46. chrome.tabs.query({}, function (tabs) {
  47. var has = false;
  48. tabs.some(function (item) {
  49. if ($A.getHostname(item.url) == index) {
  50. chrome.windows.update(item.windowId, {focused: true});
  51. chrome.tabs.highlight({tabs: item.index, windowId: item.windowId});
  52. chrome.tabs.update({url: $A.urlAddParams($A.removeURLParameter(item.url, ['open', 'rand']), {open: 'chat', rand: Math.round(new Date().getTime())})});
  53. return has = true;
  54. }
  55. });
  56. if (!has) {
  57. chrome.tabs.create({ url: 'http://' + index + '/#/todo?token=' + token + '&open=chat' })
  58. }
  59. });
  60. })
  61. }