popup.js 2.7 KB

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