ui-editor.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. disableCutePage();
  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. disableCutePage();
  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 (toolbarElement.classList.contains("cut-mode")) {
  121. disableCutePage();
  122. }
  123. if (editPageButton.classList.contains("edit-disabled")) {
  124. enableEditPage();
  125. } else {
  126. disableEditPage();
  127. }
  128. };
  129. formatPageButton.onclick = () => {
  130. enableFormatPage();
  131. };
  132. cutPageButton.onclick = () => {
  133. if (toolbarElement.classList.contains("edit-mode")) {
  134. disableEditPage();
  135. }
  136. if (cutPageButton.classList.contains("cut-disabled")) {
  137. enableCutPage();
  138. editorElement.contentWindow.focus();
  139. } else {
  140. disableCutePage();
  141. }
  142. };
  143. undoCutPageButton.onclick = () => {
  144. editorElement.contentWindow.postMessage(JSON.stringify({ method: "undoCutPage" }), "*");
  145. editorElement.contentWindow.focus();
  146. };
  147. undoAllCutPageButton.onclick = () => {
  148. editorElement.contentWindow.postMessage(JSON.stringify({ method: "undoAllCutPage" }), "*");
  149. editorElement.contentWindow.focus();
  150. };
  151. redoCutPageButton.onclick = () => {
  152. editorElement.contentWindow.postMessage(JSON.stringify({ method: "redoCutPage" }), "*");
  153. editorElement.contentWindow.focus();
  154. };
  155. savePageButton.onclick = () => {
  156. savePage();
  157. };
  158. let updatedResources = {};
  159. window.onmessage = event => {
  160. const message = JSON.parse(event.data);
  161. if (message.method == "setMetadata") {
  162. document.title = "[SingleFile] " + message.title;
  163. if (message.icon) {
  164. const linkElement = document.createElement("link");
  165. linkElement.rel = "icon";
  166. linkElement.href = message.icon;
  167. document.head.appendChild(linkElement);
  168. }
  169. }
  170. if (message.method == "setContent") {
  171. const pageData = {
  172. content: message.content,
  173. filename: tabData.filename
  174. };
  175. tabData.options.openEditor = false;
  176. singlefile.extension.core.content.download.downloadPage(pageData, tabData.options);
  177. }
  178. if (message.method == "disableFormatPage") {
  179. tabData.options.disableFormatPage = true;
  180. formatPageButton.remove();
  181. }
  182. if (message.method == "onUpdate") {
  183. tabData.docSaved = message.saved;
  184. }
  185. if (message.method == "onInit") {
  186. if (tabData.options.defaultEditorMode == "edit") {
  187. enableEditPage();
  188. } else if (tabData.options.defaultEditorMode == "format" && !tabData.options.disableFormatPage) {
  189. enableFormatPage();
  190. } else if (tabData.options.defaultEditorMode == "cut") {
  191. enableCutPage();
  192. }
  193. }
  194. };
  195. window.onload = () => {
  196. browser.runtime.sendMessage({ method: "editor.getTabData" });
  197. browser.runtime.onMessage.addListener(message => {
  198. if (message.method == "devtools.resourceCommitted") {
  199. updatedResources[message.url] = { content: message.content, type: message.type, encoding: message.encoding };
  200. return Promise.resolve({});
  201. }
  202. if (message.method == "content.save") {
  203. tabData.options = message.options;
  204. savePage();
  205. browser.runtime.sendMessage({ method: "ui.processInit" });
  206. return Promise.resolve({});
  207. }
  208. if (message.method == "common.promptValueRequest") {
  209. browser.runtime.sendMessage({ method: "tabs.promptValueResponse", value: prompt(message.promptMessage) });
  210. return Promise.resolve({});
  211. }
  212. if (message.method == "editor.setTabData") {
  213. if (message.truncated) {
  214. tabDataContents.push(message.content);
  215. } else {
  216. tabDataContents = [message.content];
  217. }
  218. if (!message.truncated || message.finished) {
  219. tabData = JSON.parse(tabDataContents.join(""));
  220. tabData.docSaved = true;
  221. tabDataContents = [];
  222. editorElement.contentWindow.postMessage(JSON.stringify({ method: "init", content: tabData.content }), "*");
  223. delete tabData.content;
  224. }
  225. return Promise.resolve({});
  226. }
  227. });
  228. };
  229. window.onbeforeunload = event => {
  230. if (tabData.options.warnUnsavedPage && !tabData.docSaved) {
  231. event.preventDefault();
  232. event.returnValue = "";
  233. }
  234. };
  235. function disableEditPage() {
  236. editPageButton.classList.add("edit-disabled");
  237. toolbarElement.classList.remove("edit-mode");
  238. editorElement.contentWindow.postMessage(JSON.stringify({ method: "disableEditPage" }), "*");
  239. }
  240. function disableCutePage() {
  241. cutPageButton.classList.add("cut-disabled");
  242. toolbarElement.classList.remove("cut-mode");
  243. editorElement.contentWindow.postMessage(JSON.stringify({ method: "disableCutPage" }), "*");
  244. }
  245. function resetHighlightButtons() {
  246. highlightButtons.forEach(highlightButton => highlightButton.classList.add("highlight-disabled"));
  247. editorElement.contentWindow.postMessage(JSON.stringify({ method: "disableHighlight" }), "*");
  248. }
  249. function disableRemoveHighlights() {
  250. toolbarElement.classList.remove("remove-highlight-mode");
  251. removeHighlightButton.classList.add("remove-highlight-disabled");
  252. editorElement.contentWindow.postMessage(JSON.stringify({ method: "disableRemoveHighlights" }), "*");
  253. }
  254. function displayHighlights() {
  255. toggleHighlightsButton.src = "/extension/ui/resources/button_highlighter_visible.png";
  256. editorElement.contentWindow.postMessage(JSON.stringify({ method: "displayHighlights" }), "*");
  257. }
  258. function enableEditPage() {
  259. editPageButton.classList.remove("edit-disabled");
  260. toolbarElement.classList.add("edit-mode");
  261. editorElement.contentWindow.postMessage(JSON.stringify({ method: "enableEditPage" }), "*");
  262. }
  263. function enableFormatPage() {
  264. if (formatPageButton.classList.contains("format-disabled")) {
  265. formatPageButton.classList.remove("format-disabled");
  266. updatedResources = {};
  267. editorElement.contentWindow.postMessage(JSON.stringify({ method: tabData.options.applySystemTheme ? "formatPage" : "formatPageNoTheme" }), "*");
  268. }
  269. }
  270. function enableCutPage() {
  271. cutPageButton.classList.remove("cut-disabled");
  272. toolbarElement.classList.add("cut-mode");
  273. resetHighlightButtons();
  274. disableRemoveHighlights();
  275. editorElement.contentWindow.postMessage(JSON.stringify({ method: "enableCutPage" }), "*");
  276. }
  277. function savePage() {
  278. editorElement.contentWindow.postMessage(JSON.stringify({ method: "getContent", compressHTML: tabData.options.compressHTML, updatedResources }), "*");
  279. }
  280. return {};
  281. })();