index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * 便签笔记入口程序
  3. * @author zhaoxianlie
  4. */
  5. let StickyNotes = (() => {
  6. // 添加事件监听
  7. let addListener = () => {
  8. // 正在编辑中
  9. let editing = false;
  10. window.onbeforeunload = function (e) {
  11. if (editing) {
  12. (e || window.event).returnValue = '当前还有未保存的笔记,确定要离开么?';
  13. }
  14. };
  15. // add note
  16. $('#addnote').click(function () {
  17. editing = true;
  18. html5sticky.addNote();
  19. return false;
  20. });
  21. // delete all notes
  22. $('#remove').click(function () {
  23. html5sticky.deleteAllNotes();
  24. return false;
  25. });
  26. // delete all notes
  27. $('#donate-link').click(function (event) {
  28. event.preventDefault();
  29. event.stopPropagation();
  30. chrome.runtime.sendMessage({
  31. type: 'fh-dynamic-any-thing',
  32. thing: 'open-donate-modal',
  33. params: { toolName: 'sticky-notes' }
  34. });
  35. return false;
  36. });
  37. // open options page
  38. $('#other-tools').click(function (event) {
  39. event.preventDefault();
  40. event.stopPropagation();
  41. chrome.runtime.openOptionsPage();
  42. });
  43. $(document.body).delegate('.delete_stickynote', 'click', function (e) {
  44. // delete note
  45. html5sticky.deleteNote($(this));
  46. return false;
  47. }).delegate('.close_stickynote', 'click', function (e) {
  48. editing = false;
  49. // close enlarged note
  50. html5sticky.closeNote($(this));
  51. return false;
  52. }).delegate('.save_stickynote', 'click', function (e) {
  53. editing = false;
  54. // save the note
  55. html5sticky.saveNote($(this));
  56. return false;
  57. }).delegate('.note_common', 'click', function (e) {
  58. // enlarge the note
  59. $(this).find('.btn-close').hide();
  60. html5sticky.enlargeNote($(this));
  61. return false;
  62. }).delegate('.note_common', 'mouseover', function (e) {
  63. // 显示关闭按钮
  64. $(this).find('.btn-close').show();
  65. }).delegate('.note_common', 'mouseout', function (e) {
  66. // 隐藏关闭按钮
  67. $(this).find('.btn-close').hide();
  68. });
  69. // collapse the notes
  70. $('#collapse').click(function (event) {
  71. html5sticky.collapse();
  72. return false;
  73. });
  74. // expand the notes
  75. $('#expand').click(function (event) {
  76. html5sticky.expand();
  77. return false;
  78. });
  79. // allow escape to close big note
  80. $(document).keyup(function (e) {
  81. if (e.keyCode === 27) {
  82. $('#overlay').remove();
  83. $('.bignore').remove();
  84. }
  85. });
  86. // 下载
  87. $('#export').click(function (e) {
  88. html5sticky.export();
  89. return false;
  90. });
  91. // 导入笔记
  92. $('#import').click(function (event) {
  93. if (confirm('仅支持再次导入【之前用本工具导出的*.zip包】,请确认zip包已准备好?')) {
  94. html5sticky.importNotes();
  95. }
  96. return false;
  97. });
  98. // 文件夹选中
  99. $('#folders').delegate('li', 'click', function (e) {
  100. $(this).addClass('x-selected').siblings('li').removeClass('x-selected');
  101. let txt = $(this).text();
  102. let id = $(this).attr('id').replace(/^f_/, '');
  103. html5sticky.setCurrentFolder(txt, id);
  104. html5sticky.loadNotes(id);
  105. });
  106. // 创建文件夹
  107. $('#createFolder').click(function (e) {
  108. let el = html5sticky.createFolder();
  109. if (el) {
  110. el.trigger('click');
  111. }
  112. return false;
  113. });
  114. };
  115. function loadPatchHotfix() {
  116. // 页面加载时自动获取并注入页面的补丁
  117. chrome.runtime.sendMessage({
  118. type: 'fh-dynamic-any-thing',
  119. thing: 'fh-get-tool-patch',
  120. toolName: 'sticky-notes'
  121. }, patch => {
  122. if (patch) {
  123. if (patch.css) {
  124. const style = document.createElement('style');
  125. style.textContent = patch.css;
  126. document.head.appendChild(style);
  127. }
  128. if (patch.js) {
  129. try {
  130. if (window.evalCore && window.evalCore.getEvalInstance) {
  131. window.evalCore.getEvalInstance(window)(patch.js);
  132. }
  133. } catch (e) {
  134. console.error('sticky-notes补丁JS执行失败', e);
  135. }
  136. }
  137. }
  138. });
  139. }
  140. // 初始化
  141. let init = () => {
  142. html5sticky.buildFoldersAndInitNotes();
  143. addListener();
  144. loadPatchHotfix();
  145. };
  146. return {
  147. init: init
  148. };
  149. })();
  150. StickyNotes.init();