app.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * Created by Hongcai Deng on 2015/12/29.
  3. */
  4. $(function(){
  5. $('.ui.modal')
  6. .modal()
  7. ;
  8. var clipboard = new Clipboard('.copyable');
  9. $maillist = $('#maillist');
  10. $maillist.on('click', 'tr', function() {
  11. var mail = $(this).data('mail');
  12. $('#mailcard .header').text(mail.headers.subject || '无主题');
  13. $('#mailcard .content:last').html(mail.html);
  14. $('#mailcard i').click(function() {
  15. $('#raw').modal('show');
  16. });
  17. $('#raw .header').text('RAW');
  18. $('#raw .content').html($('<pre>').html($('<code>').addClass('language-json').html(JSON.stringify(mail, null, 2))));
  19. Prism.highlightAll();
  20. });
  21. var socket = io();
  22. var setMailAddress = function(id) {
  23. localStorage.setItem('shortid', id);
  24. var mailaddress = id + '@' + location.hostname;
  25. $('#shortid').val(mailaddress).parent().siblings('button').find('.mail').attr('data-clipboard-text', mailaddress);
  26. };
  27. $('#refreshShortid').click(function() {
  28. socket.emit('request shortid', true);
  29. });
  30. socket.on('connect', function() {
  31. if(('localStorage' in window)) {
  32. var shortid = localStorage.getItem('shortid');
  33. if(!shortid) {
  34. socket.emit('request shortid', true);
  35. }
  36. else {
  37. socket.emit('set shortid', true);
  38. setMailAddress(shortid);
  39. }
  40. }
  41. });
  42. socket.on('shortid', function(id) {
  43. setMailAddress(id);
  44. });
  45. socket.on('mail', function(mail) {
  46. if(('Notification' in window)) {
  47. if(Notification.permission === 'granted') {
  48. new Notification('New mail from ' + mail.headers.from);
  49. }
  50. else if(Notification.permission !== 'denied') {
  51. Notification.requestPermission(function(permission) {
  52. if(permission === 'granted') {
  53. new Notification('New mail from ' + mail.headers.from);
  54. }
  55. })
  56. }
  57. }
  58. $tr = $('<tr>').data('mail', mail);
  59. $tr
  60. .append($('<td>').text(mail.headers.from))
  61. .append($('<td>').text(mail.headers.subject || '无主题'))
  62. .append($('<td>').text((new Date(mail.headers.date)).toLocaleTimeString()));
  63. $maillist.prepend($tr);
  64. });
  65. });