popup.js 2.5 KB

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