ui-editor.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 toolbarElement = document.querySelector(".toolbar");
  27. const highlightYellowButton = document.querySelector(".highlight-yellow-button");
  28. const highlightPinkButton = document.querySelector(".highlight-pink-button");
  29. const highlightBlueButton = document.querySelector(".highlight-blue-button");
  30. const highlightGreenButton = document.querySelector(".highlight-green-button");
  31. const highlightButtons = Array.from(document.querySelectorAll(".highlight-button"));
  32. const toggleNotesButton = document.querySelector(".toggle-notes-button");
  33. const toggleHighlightsButton = document.querySelector(".toggle-highlights-button");
  34. const removeHighlightButton = document.querySelector(".remove-highlight-button");
  35. const addYellowNoteButton = document.querySelector(".add-note-yellow-button");
  36. const addPinkNoteButton = document.querySelector(".add-note-pink-button");
  37. const addBlueNoteButton = document.querySelector(".add-note-blue-button");
  38. const addGreenNoteButton = document.querySelector(".add-note-green-button");
  39. const editPageButton = document.querySelector(".edit-page-button");
  40. const formatPageButton = document.querySelector(".format-page-button");
  41. const cutPageButton = document.querySelector(".cut-page-button");
  42. const undoCutPageButton = document.querySelector(".undo-cut-page-button");
  43. const undoAllCutPageButton = document.querySelector(".undo-all-cut-page-button");
  44. const redoCutPageButton = document.querySelector(".redo-cut-page-button");
  45. const savePageButton = document.querySelector(".save-page-button");
  46. let tabData, tabDataContents = [];
  47. addYellowNoteButton.title = browser.i18n.getMessage("editorAddYellowNote");
  48. addPinkNoteButton.title = browser.i18n.getMessage("editorAddPinkNote");
  49. addBlueNoteButton.title = browser.i18n.getMessage("editorAddBlueNote");
  50. addGreenNoteButton.title = browser.i18n.getMessage("editorAddGreenNote");
  51. highlightYellowButton.title = browser.i18n.getMessage("editorHighlightYellow");
  52. highlightPinkButton.title = browser.i18n.getMessage("editorHighlightPink");
  53. highlightBlueButton.title = browser.i18n.getMessage("editorHighlightBlue");
  54. highlightGreenButton.title = browser.i18n.getMessage("editorHighlightGreen");
  55. toggleNotesButton.title = browser.i18n.getMessage("editorToggleNotes");
  56. toggleHighlightsButton.title = browser.i18n.getMessage("editorToggleHighlights");
  57. removeHighlightButton.title = browser.i18n.getMessage("editorRemoveHighlight");
  58. editPageButton.title = browser.i18n.getMessage("editorEditPage");
  59. formatPageButton.title = browser.i18n.getMessage("editorFormatPage");
  60. cutPageButton.title = browser.i18n.getMessage("editorCutPage");
  61. undoCutPageButton.title = browser.i18n.getMessage("editorUndoCutPage");
  62. undoAllCutPageButton.title = browser.i18n.getMessage("editorUndoAllCutPage");
  63. redoCutPageButton.title = browser.i18n.getMessage("editorRedoCutPage");
  64. savePageButton.title = browser.i18n.getMessage("editorSavePage");
  65. addYellowNoteButton.onclick = () => editorElement.contentWindow.postMessage(JSON.stringify({ method: "addNote", color: "note-yellow" }), "*");
  66. addPinkNoteButton.onclick = () => editorElement.contentWindow.postMessage(JSON.stringify({ method: "addNote", color: "note-pink" }), "*");
  67. addBlueNoteButton.onclick = () => editorElement.contentWindow.postMessage(JSON.stringify({ method: "addNote", color: "note-blue" }), "*");
  68. addGreenNoteButton.onclick = () => editorElement.contentWindow.postMessage(JSON.stringify({ method: "addNote", color: "note-green" }), "*");
  69. highlightButtons.forEach(highlightButton => {
  70. highlightButton.onclick = () => {
  71. if (toolbarElement.classList.contains("cut-mode")) {
  72. disableCuteMode();
  73. }
  74. if (toolbarElement.classList.contains("remove-highlight-mode")) {
  75. disableRemoveHighlights();
  76. }
  77. const disabled = highlightButton.classList.contains("highlight-disabled");
  78. resetHighlightButtons();
  79. if (disabled) {
  80. highlightButton.classList.remove("highlight-disabled");
  81. editorElement.contentWindow.postMessage(JSON.stringify({ method: "enableHighlight", color: "single-file-highlight-" + highlightButton.dataset.color }), "*");
  82. } else {
  83. highlightButton.classList.add("highlight-disabled");
  84. }
  85. };
  86. });
  87. toggleNotesButton.onclick = () => {
  88. if (toggleNotesButton.getAttribute("src") == "/extension/ui/resources/button_note_visible.png") {
  89. toggleNotesButton.src = "/extension/ui/resources/button_note_hidden.png";
  90. editorElement.contentWindow.postMessage(JSON.stringify({ method: "hideNotes" }), "*");
  91. } else {
  92. toggleNotesButton.src = "/extension/ui/resources/button_note_visible.png";
  93. editorElement.contentWindow.postMessage(JSON.stringify({ method: "displayNotes" }), "*");
  94. }
  95. };
  96. toggleHighlightsButton.onclick = () => {
  97. if (toggleHighlightsButton.getAttribute("src") == "/extension/ui/resources/button_highlighter_visible.png") {
  98. toggleHighlightsButton.src = "/extension/ui/resources/button_highlighter_hidden.png";
  99. editorElement.contentWindow.postMessage(JSON.stringify({ method: "hideHighlights" }), "*");
  100. } else {
  101. displayHighlights();
  102. }
  103. };
  104. removeHighlightButton.onclick = () => {
  105. if (toolbarElement.classList.contains("cut-mode")) {
  106. disableCuteMode();
  107. }
  108. if (removeHighlightButton.classList.contains("remove-highlight-disabled")) {
  109. removeHighlightButton.classList.remove("remove-highlight-disabled");
  110. toolbarElement.classList.add("remove-highlight-mode");
  111. resetHighlightButtons();
  112. displayHighlights();
  113. editorElement.contentWindow.postMessage(JSON.stringify({ method: "enableRemoveHighlights" }), "*");
  114. editorElement.contentWindow.postMessage(JSON.stringify({ method: "displayHighlights" }), "*");
  115. } else {
  116. disableRemoveHighlights();
  117. }
  118. };
  119. editPageButton.onclick = () => {
  120. if (editPageButton.classList.contains("edit-disabled")) {
  121. enableEditPage();
  122. } else {
  123. editPageButton.classList.add("edit-disabled");
  124. editorElement.contentWindow.postMessage(JSON.stringify({ method: "disableEditPage" }), "*");
  125. }
  126. };
  127. formatPageButton.onclick = () => {
  128. enableFormatPage();
  129. };
  130. cutPageButton.onclick = () => {
  131. if (cutPageButton.classList.contains("cut-disabled")) {
  132. enableCutPage();
  133. editorElement.contentWindow.focus();
  134. } else {
  135. disableCuteMode();
  136. }
  137. };
  138. undoCutPageButton.onclick = () => {
  139. editorElement.contentWindow.postMessage(JSON.stringify({ method: "undoCutPage" }), "*");
  140. editorElement.contentWindow.focus();
  141. };
  142. undoAllCutPageButton.onclick = () => {
  143. editorElement.contentWindow.postMessage(JSON.stringify({ method: "undoAllCutPage" }), "*");
  144. editorElement.contentWindow.focus();
  145. };
  146. redoCutPageButton.onclick = () => {
  147. editorElement.contentWindow.postMessage(JSON.stringify({ method: "redoCutPage" }), "*");
  148. editorElement.contentWindow.focus();
  149. };
  150. savePageButton.onclick = () => {
  151. savePage();
  152. };
  153. let updatedResources = {};
  154. window.onmessage = event => {
  155. const message = JSON.parse(event.data);
  156. if (message.method == "setMetadata") {
  157. document.title = "[SingleFile] " + message.title;
  158. if (message.icon) {
  159. const linkElement = document.createElement("link");
  160. linkElement.rel = "icon";
  161. linkElement.href = message.icon;
  162. document.head.appendChild(linkElement);
  163. }
  164. }
  165. if (message.method == "setContent") {
  166. const pageData = {
  167. content: message.content,
  168. filename: tabData.filename
  169. };
  170. tabData.options.openEditor = false;
  171. singlefile.extension.core.content.download.downloadPage(pageData, tabData.options);
  172. }
  173. if (message.method == "disableFormatPage") {
  174. tabData.options.disableFormatPage = true;
  175. formatPageButton.remove();
  176. }
  177. if (message.method == "onUpdate") {
  178. tabData.docSaved = message.saved;
  179. }
  180. if (message.method == "onInit") {
  181. if (tabData.options.defaultEditorMode == "edit") {
  182. enableEditPage();
  183. } else if (tabData.options.defaultEditorMode == "format" && !tabData.options.disableFormatPage) {
  184. enableFormatPage();
  185. } else if (tabData.options.defaultEditorMode == "cut") {
  186. enableCutPage();
  187. }
  188. }
  189. };
  190. window.onload = () => {
  191. browser.runtime.sendMessage({ method: "editor.getTabData" });
  192. browser.runtime.onMessage.addListener(message => {
  193. if (message.method == "devtools.resourceCommitted") {
  194. updatedResources[message.url] = { content: message.content, type: message.type, encoding: message.encoding };
  195. return Promise.resolve({});
  196. }
  197. if (message.method == "content.save") {
  198. tabData.options = message.options;
  199. savePage();
  200. browser.runtime.sendMessage({ method: "ui.processInit" });
  201. return Promise.resolve({});
  202. }
  203. if (message.method == "common.promptValueRequest") {
  204. browser.runtime.sendMessage({ method: "tabs.promptValueResponse", value: prompt(message.promptMessage) });
  205. return Promise.resolve({});
  206. }
  207. if (message.method == "editor.setTabData") {
  208. if (message.truncated) {
  209. tabDataContents.push(message.content);
  210. } else {
  211. tabDataContents = [message.content];
  212. }
  213. if (!message.truncated || message.finished) {
  214. tabData = JSON.parse(tabDataContents.join(""));
  215. tabData.docSaved = true;
  216. tabDataContents = [];
  217. editorElement.contentWindow.postMessage(JSON.stringify({ method: "init", content: tabData.content }), "*");
  218. delete tabData.content;
  219. }
  220. return Promise.resolve({});
  221. }
  222. });
  223. };
  224. window.onbeforeunload = event => {
  225. if (tabData.options.warnUnsavedPage && !tabData.docSaved) {
  226. event.preventDefault();
  227. event.returnValue = "";
  228. }
  229. };
  230. function disableCuteMode() {
  231. cutPageButton.classList.add("cut-disabled");
  232. toolbarElement.classList.remove("cut-mode");
  233. editorElement.contentWindow.postMessage(JSON.stringify({ method: "disableCutPage" }), "*");
  234. }
  235. function resetHighlightButtons() {
  236. highlightButtons.forEach(highlightButton => highlightButton.classList.add("highlight-disabled"));
  237. editorElement.contentWindow.postMessage(JSON.stringify({ method: "disableHighlight" }), "*");
  238. }
  239. function disableRemoveHighlights() {
  240. toolbarElement.classList.remove("remove-highlight-mode");
  241. removeHighlightButton.classList.add("remove-highlight-disabled");
  242. editorElement.contentWindow.postMessage(JSON.stringify({ method: "disableRemoveHighlights" }), "*");
  243. }
  244. function displayHighlights() {
  245. toggleHighlightsButton.src = "/extension/ui/resources/button_highlighter_visible.png";
  246. editorElement.contentWindow.postMessage(JSON.stringify({ method: "displayHighlights" }), "*");
  247. }
  248. function enableEditPage() {
  249. editPageButton.classList.remove("edit-disabled");
  250. editorElement.contentWindow.postMessage(JSON.stringify({ method: "enableEditPage" }), "*");
  251. }
  252. function enableFormatPage() {
  253. if (formatPageButton.classList.contains("format-disabled")) {
  254. formatPageButton.classList.remove("format-disabled");
  255. updatedResources = {};
  256. editorElement.contentWindow.postMessage(JSON.stringify({ method: tabData.options.applySystemTheme ? "formatPage" : "formatPageNoTheme" }), "*");
  257. }
  258. }
  259. function enableCutPage() {
  260. cutPageButton.classList.remove("cut-disabled");
  261. toolbarElement.classList.add("cut-mode");
  262. resetHighlightButtons();
  263. disableRemoveHighlights();
  264. editorElement.contentWindow.postMessage(JSON.stringify({ method: "enableCutPage" }), "*");
  265. }
  266. function savePage() {
  267. editorElement.contentWindow.postMessage(JSON.stringify({ method: "getContent", compressHTML: tabData.options.compressHTML, updatedResources }), "*");
  268. }
  269. return {};
  270. })();