ui-editor.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Copyright 2010-2020 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * The code in this file is free software: you can redistribute it and/or
  8. * modify it under the terms of the GNU Affero General Public License
  9. * (GNU AGPL) as published by the Free Software Foundation, either version 3
  10. * of the License, or (at your option) any later version.
  11. *
  12. * The code in this file is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  15. * General Public License for more details.
  16. *
  17. * As additional permission under GNU AGPL version 3 section 7, you may
  18. * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
  19. * AGPL normally required by section 4, provided you include this license
  20. * notice and a URL through which recipients can access the Corresponding
  21. * Source.
  22. */
  23. /* global browser, singlefile, window, document, prompt */
  24. singlefile.extension.ui.bg.editor = (() => {
  25. const editorElement = document.querySelector(".editor");
  26. const highlightYellowButton = document.querySelector(".highlight-yellow-button");
  27. const highlightPinkButton = document.querySelector(".highlight-pink-button");
  28. const highlightBlueButton = document.querySelector(".highlight-blue-button");
  29. const highlightGreenButton = document.querySelector(".highlight-green-button");
  30. const highlightButtons = Array.from(document.querySelectorAll(".highlight-button"));
  31. const toggleNotesButton = document.querySelector(".toggle-notes-button");
  32. const toggleHighlightsButton = document.querySelector(".toggle-highlights-button");
  33. const removeHighlightButton = document.querySelector(".remove-highlight-button");
  34. const addYellowNoteButton = document.querySelector(".add-note-yellow-button");
  35. const addPinkNoteButton = document.querySelector(".add-note-pink-button");
  36. const addBlueNoteButton = document.querySelector(".add-note-blue-button");
  37. const addGreenNoteButton = document.querySelector(".add-note-green-button");
  38. const editPageButton = document.querySelector(".edit-page-button");
  39. const formatPageButton = document.querySelector(".format-page-button");
  40. const cutPageButton = document.querySelector(".cut-page-button");
  41. const undoCutPageButton = document.querySelector(".undo-cut-page-button");
  42. const undoAllCutPageButton = document.querySelector(".undo-all-cut-page-button");
  43. const redoCutPageButton = document.querySelector(".redo-cut-page-button");
  44. const savePageButton = document.querySelector(".save-page-button");
  45. let tabData, tabDataContents = [];
  46. addYellowNoteButton.title = browser.i18n.getMessage("editorAddYellowNote");
  47. addPinkNoteButton.title = browser.i18n.getMessage("editorAddPinkNote");
  48. addBlueNoteButton.title = browser.i18n.getMessage("editorAddBlueNote");
  49. addGreenNoteButton.title = browser.i18n.getMessage("editorAddGreenNote");
  50. highlightYellowButton.title = browser.i18n.getMessage("editorHighlightYellow");
  51. highlightPinkButton.title = browser.i18n.getMessage("editorHighlightPink");
  52. highlightBlueButton.title = browser.i18n.getMessage("editorHighlightBlue");
  53. highlightGreenButton.title = browser.i18n.getMessage("editorHighlightGreen");
  54. toggleNotesButton.title = browser.i18n.getMessage("editorToggleNotes");
  55. toggleHighlightsButton.title = browser.i18n.getMessage("editorToggleHighlights");
  56. removeHighlightButton.title = browser.i18n.getMessage("editorRemoveHighlight");
  57. editPageButton.title = browser.i18n.getMessage("editorEditPage");
  58. formatPageButton.title = browser.i18n.getMessage("editorFormatPage");
  59. cutPageButton.title = browser.i18n.getMessage("editorCutPage");
  60. undoCutPageButton.title = browser.i18n.getMessage("editorUndoCutPage");
  61. undoAllCutPageButton.title = browser.i18n.getMessage("editorUndoAllCutPage");
  62. redoCutPageButton.title = browser.i18n.getMessage("editorRedoCutPage");
  63. savePageButton.title = browser.i18n.getMessage("editorSavePage");
  64. addYellowNoteButton.onclick = () => editorElement.contentWindow.postMessage(JSON.stringify({ method: "addNote", color: "note-yellow" }), "*");
  65. addPinkNoteButton.onclick = () => editorElement.contentWindow.postMessage(JSON.stringify({ method: "addNote", color: "note-pink" }), "*");
  66. addBlueNoteButton.onclick = () => editorElement.contentWindow.postMessage(JSON.stringify({ method: "addNote", color: "note-blue" }), "*");
  67. addGreenNoteButton.onclick = () => editorElement.contentWindow.postMessage(JSON.stringify({ method: "addNote", color: "note-green" }), "*");
  68. highlightYellowButton.onclick = () => {
  69. if (highlightYellowButton.classList.contains("highlight-disabled")) {
  70. highlightButtons.forEach(highlightButton => highlightButton.classList.add("highlight-disabled"));
  71. highlightYellowButton.classList.remove("highlight-disabled");
  72. editorElement.contentWindow.postMessage(JSON.stringify({ method: "enableHighlight", color: "single-file-highlight-yellow" }), "*");
  73. } else {
  74. highlightYellowButton.classList.add("highlight-disabled");
  75. editorElement.contentWindow.postMessage(JSON.stringify({ method: "disableHighlight" }), "*");
  76. }
  77. };
  78. highlightPinkButton.onclick = () => {
  79. if (highlightPinkButton.classList.contains("highlight-disabled")) {
  80. highlightButtons.forEach(highlightButton => highlightButton.classList.add("highlight-disabled"));
  81. highlightPinkButton.classList.remove("highlight-disabled");
  82. editorElement.contentWindow.postMessage(JSON.stringify({ method: "enableHighlight", color: "single-file-highlight-pink" }), "*");
  83. } else {
  84. highlightPinkButton.classList.add("highlight-disabled");
  85. editorElement.contentWindow.postMessage(JSON.stringify({ method: "disableHighlight" }), "*");
  86. }
  87. };
  88. highlightBlueButton.onclick = () => {
  89. if (highlightBlueButton.classList.contains("highlight-disabled")) {
  90. highlightButtons.forEach(highlightButton => highlightButton.classList.add("highlight-disabled"));
  91. highlightBlueButton.classList.remove("highlight-disabled");
  92. editorElement.contentWindow.postMessage(JSON.stringify({ method: "enableHighlight", color: "single-file-highlight-blue" }), "*");
  93. } else {
  94. highlightBlueButton.classList.add("highlight-disabled");
  95. editorElement.contentWindow.postMessage(JSON.stringify({ method: "disableHighlight" }), "*");
  96. }
  97. };
  98. highlightGreenButton.onclick = () => {
  99. if (highlightGreenButton.classList.contains("highlight-disabled")) {
  100. highlightButtons.forEach(highlightButton => highlightButton.classList.add("highlight-disabled"));
  101. highlightGreenButton.classList.remove("highlight-disabled");
  102. editorElement.contentWindow.postMessage(JSON.stringify({ method: "enableHighlight", color: "single-file-highlight-green" }), "*");
  103. } else {
  104. highlightGreenButton.classList.add("highlight-disabled");
  105. editorElement.contentWindow.postMessage(JSON.stringify({ method: "disableHighlight" }), "*");
  106. }
  107. };
  108. toggleNotesButton.onclick = () => {
  109. if (toggleNotesButton.getAttribute("src") == "/extension/ui/resources/button_note_visible.png") {
  110. toggleNotesButton.src = "/extension/ui/resources/button_note_hidden.png";
  111. editorElement.contentWindow.postMessage(JSON.stringify({ method: "hideNotes" }), "*");
  112. } else {
  113. toggleNotesButton.src = "/extension/ui/resources/button_note_visible.png";
  114. editorElement.contentWindow.postMessage(JSON.stringify({ method: "displayNotes" }), "*");
  115. }
  116. };
  117. toggleHighlightsButton.onclick = () => {
  118. if (toggleHighlightsButton.getAttribute("src") == "/extension/ui/resources/button_highlighter_visible.png") {
  119. toggleHighlightsButton.src = "/extension/ui/resources/button_highlighter_hidden.png";
  120. editorElement.contentWindow.postMessage(JSON.stringify({ method: "hideHighlights" }), "*");
  121. } else {
  122. toggleHighlightsButton.src = "/extension/ui/resources/button_highlighter_visible.png";
  123. editorElement.contentWindow.postMessage(JSON.stringify({ method: "displayHighlights" }), "*");
  124. }
  125. };
  126. removeHighlightButton.onclick = () => {
  127. if (removeHighlightButton.classList.contains("remove-highlight-disabled")) {
  128. removeHighlightButton.classList.remove("remove-highlight-disabled");
  129. editorElement.contentWindow.postMessage(JSON.stringify({ method: "enableRemoveHighlights" }), "*");
  130. } else {
  131. removeHighlightButton.classList.add("remove-highlight-disabled");
  132. editorElement.contentWindow.postMessage(JSON.stringify({ method: "disableRemoveHighlights" }), "*");
  133. }
  134. };
  135. editPageButton.onclick = () => {
  136. if (editPageButton.classList.contains("edit-disabled")) {
  137. editPageButton.classList.remove("edit-disabled");
  138. editorElement.contentWindow.postMessage(JSON.stringify({ method: "enableEditPage" }), "*");
  139. } else {
  140. editPageButton.classList.add("edit-disabled");
  141. editorElement.contentWindow.postMessage(JSON.stringify({ method: "disableEditPage" }), "*");
  142. }
  143. };
  144. formatPageButton.onclick = () => {
  145. if (formatPageButton.classList.contains("format-disabled")) {
  146. formatPageButton.classList.remove("format-disabled");
  147. updatedResources = {};
  148. editorElement.contentWindow.postMessage(JSON.stringify({ method: tabData.options.applySystemTheme ? "formatPage" : "formatPageNoTheme" }), "*");
  149. }
  150. };
  151. cutPageButton.onclick = () => {
  152. if (cutPageButton.classList.contains("cut-disabled")) {
  153. cutPageButton.classList.remove("cut-disabled");
  154. editorElement.contentWindow.postMessage(JSON.stringify({ method: "enableCutPage" }), "*");
  155. } else {
  156. cutPageButton.classList.add("cut-disabled");
  157. editorElement.contentWindow.postMessage(JSON.stringify({ method: "disableCutPage" }), "*");
  158. }
  159. };
  160. undoCutPageButton.onclick = () => {
  161. editorElement.contentWindow.postMessage(JSON.stringify({ method: "undoCutPage" }), "*");
  162. };
  163. undoAllCutPageButton.onclick = () => {
  164. editorElement.contentWindow.postMessage(JSON.stringify({ method: "undoAllCutPage" }), "*");
  165. };
  166. redoCutPageButton.onclick = () => {
  167. editorElement.contentWindow.postMessage(JSON.stringify({ method: "redoCutPage" }), "*");
  168. };
  169. savePageButton.onclick = () => {
  170. savePage();
  171. };
  172. let updatedResources = {};
  173. window.onmessage = event => {
  174. const message = JSON.parse(event.data);
  175. if (message.method == "setMetadata") {
  176. document.title = "[SingleFile] " + message.title;
  177. if (message.icon) {
  178. const linkElement = document.createElement("link");
  179. linkElement.rel = "icon";
  180. linkElement.href = message.icon;
  181. document.head.appendChild(linkElement);
  182. }
  183. }
  184. if (message.method == "setContent") {
  185. const pageData = {
  186. content: message.content,
  187. filename: tabData.filename
  188. };
  189. tabData.options.openEditor = false;
  190. singlefile.extension.core.content.download.downloadPage(pageData, tabData.options);
  191. }
  192. if (message.method == "disableFormatPage") {
  193. formatPageButton.remove();
  194. }
  195. if (message.method == "onUpdate") {
  196. tabData.docSaved = message.saved;
  197. }
  198. };
  199. window.onload = () => {
  200. browser.runtime.sendMessage({ method: "editor.getTabData" });
  201. browser.runtime.onMessage.addListener(message => {
  202. if (message.method == "devtools.resourceCommitted") {
  203. updatedResources[message.url] = { content: message.content, type: message.type, encoding: message.encoding };
  204. return Promise.resolve({});
  205. }
  206. if (message.method == "content.save") {
  207. tabData.options = message.options;
  208. savePage();
  209. browser.runtime.sendMessage({ method: "ui.processInit" });
  210. return Promise.resolve({});
  211. }
  212. if (message.method == "common.promptValueRequest") {
  213. browser.runtime.sendMessage({ method: "tabs.promptValueResponse", value: prompt(message.promptMessage) });
  214. return Promise.resolve({});
  215. }
  216. if (message.method == "editor.setTabData") {
  217. if (message.truncated) {
  218. tabDataContents.push(message.content);
  219. } else {
  220. tabDataContents = [message.content];
  221. }
  222. if (!message.truncated || message.finished) {
  223. tabData = JSON.parse(tabDataContents.join(""));
  224. tabData.docSaved = true;
  225. tabDataContents = [];
  226. editorElement.contentWindow.postMessage(JSON.stringify({ method: "init", content: tabData.content }), "*");
  227. delete tabData.content;
  228. }
  229. return Promise.resolve({});
  230. }
  231. });
  232. };
  233. window.onbeforeunload = event => {
  234. if (tabData.options.warnUnsavedPage && !tabData.docSaved) {
  235. event.preventDefault();
  236. event.returnValue = "";
  237. }
  238. };
  239. function savePage() {
  240. editorElement.contentWindow.postMessage(JSON.stringify({ method: "getContent", compressHTML: tabData.options.compressHTML, updatedResources }), "*");
  241. }
  242. return {};
  243. })();