ui-editor.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright 2010-2019 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 cutPageButton = document.querySelector(".cut-page-button");
  40. const undoCutPageButton = document.querySelector(".undo-cut-page-button");
  41. const undoAllCutPageButton = document.querySelector(".undo-all-cut-page-button");
  42. const savePageButton = document.querySelector(".save-page-button");
  43. let tabData;
  44. addYellowNoteButton.title = browser.i18n.getMessage("editorAddYellowNote");
  45. addPinkNoteButton.title = browser.i18n.getMessage("editorAddPinkNote");
  46. addBlueNoteButton.title = browser.i18n.getMessage("editorAddBlueNote");
  47. addGreenNoteButton.title = browser.i18n.getMessage("editorAddGreenNote");
  48. highlightYellowButton.title = browser.i18n.getMessage("editorHighlightYellow");
  49. highlightPinkButton.title = browser.i18n.getMessage("editorHighlightPink");
  50. highlightBlueButton.title = browser.i18n.getMessage("editorHighlightBlue");
  51. highlightGreenButton.title = browser.i18n.getMessage("editorHighlightGreen");
  52. toggleNotesButton.title = browser.i18n.getMessage("editorToggleNotes");
  53. toggleHighlightsButton.title = browser.i18n.getMessage("editorToggleHighlights");
  54. removeHighlightButton.title = browser.i18n.getMessage("editorRemoveHighlight");
  55. editPageButton.title = browser.i18n.getMessage("editorEditPage");
  56. cutPageButton.title = browser.i18n.getMessage("editorCutPage");
  57. undoCutPageButton.title = browser.i18n.getMessage("editorUndoCutPage");
  58. undoAllCutPageButton.title = browser.i18n.getMessage("editorUndoAllCutPage");
  59. savePageButton.title = browser.i18n.getMessage("editorSavePage");
  60. addYellowNoteButton.onclick = () => editorElement.contentWindow.postMessage(JSON.stringify({ method: "addNote", color: "note-yellow" }), "*");
  61. addPinkNoteButton.onclick = () => editorElement.contentWindow.postMessage(JSON.stringify({ method: "addNote", color: "note-pink" }), "*");
  62. addBlueNoteButton.onclick = () => editorElement.contentWindow.postMessage(JSON.stringify({ method: "addNote", color: "note-blue" }), "*");
  63. addGreenNoteButton.onclick = () => editorElement.contentWindow.postMessage(JSON.stringify({ method: "addNote", color: "note-green" }), "*");
  64. highlightYellowButton.onclick = () => {
  65. if (highlightYellowButton.classList.contains("highlight-disabled")) {
  66. highlightButtons.forEach(highlightButton => highlightButton.classList.add("highlight-disabled"));
  67. highlightYellowButton.classList.remove("highlight-disabled");
  68. editorElement.contentWindow.postMessage(JSON.stringify({ method: "enableHighlight", color: "single-file-highlight-yellow" }), "*");
  69. } else {
  70. highlightYellowButton.classList.add("highlight-disabled");
  71. editorElement.contentWindow.postMessage(JSON.stringify({ method: "disableHighlight" }), "*");
  72. }
  73. };
  74. highlightPinkButton.onclick = () => {
  75. if (highlightPinkButton.classList.contains("highlight-disabled")) {
  76. highlightButtons.forEach(highlightButton => highlightButton.classList.add("highlight-disabled"));
  77. highlightPinkButton.classList.remove("highlight-disabled");
  78. editorElement.contentWindow.postMessage(JSON.stringify({ method: "enableHighlight", color: "single-file-highlight-pink" }), "*");
  79. } else {
  80. highlightPinkButton.classList.add("highlight-disabled");
  81. editorElement.contentWindow.postMessage(JSON.stringify({ method: "disableHighlight" }), "*");
  82. }
  83. };
  84. highlightBlueButton.onclick = () => {
  85. if (highlightBlueButton.classList.contains("highlight-disabled")) {
  86. highlightButtons.forEach(highlightButton => highlightButton.classList.add("highlight-disabled"));
  87. highlightBlueButton.classList.remove("highlight-disabled");
  88. editorElement.contentWindow.postMessage(JSON.stringify({ method: "enableHighlight", color: "single-file-highlight-blue" }), "*");
  89. } else {
  90. highlightBlueButton.classList.add("highlight-disabled");
  91. editorElement.contentWindow.postMessage(JSON.stringify({ method: "disableHighlight" }), "*");
  92. }
  93. };
  94. highlightGreenButton.onclick = () => {
  95. if (highlightGreenButton.classList.contains("highlight-disabled")) {
  96. highlightButtons.forEach(highlightButton => highlightButton.classList.add("highlight-disabled"));
  97. highlightGreenButton.classList.remove("highlight-disabled");
  98. editorElement.contentWindow.postMessage(JSON.stringify({ method: "enableHighlight", color: "single-file-highlight-green" }), "*");
  99. } else {
  100. highlightGreenButton.classList.add("highlight-disabled");
  101. editorElement.contentWindow.postMessage(JSON.stringify({ method: "disableHighlight" }), "*");
  102. }
  103. };
  104. toggleNotesButton.onclick = () => {
  105. if (toggleNotesButton.getAttribute("src") == "/extension/ui/resources/button_note_visible.png") {
  106. toggleNotesButton.src = "/extension/ui/resources/button_note_hidden.png";
  107. editorElement.contentWindow.postMessage(JSON.stringify({ method: "hideNotes" }), "*");
  108. } else {
  109. toggleNotesButton.src = "/extension/ui/resources/button_note_visible.png";
  110. editorElement.contentWindow.postMessage(JSON.stringify({ method: "displayNotes" }), "*");
  111. }
  112. };
  113. toggleHighlightsButton.onclick = () => {
  114. if (toggleHighlightsButton.getAttribute("src") == "/extension/ui/resources/button_highlighter_visible.png") {
  115. toggleHighlightsButton.src = "/extension/ui/resources/button_highlighter_hidden.png";
  116. editorElement.contentWindow.postMessage(JSON.stringify({ method: "hideHighlights" }), "*");
  117. } else {
  118. toggleHighlightsButton.src = "/extension/ui/resources/button_highlighter_visible.png";
  119. editorElement.contentWindow.postMessage(JSON.stringify({ method: "displayHighlights" }), "*");
  120. }
  121. };
  122. removeHighlightButton.onclick = () => {
  123. if (removeHighlightButton.classList.contains("remove-highlight-disabled")) {
  124. removeHighlightButton.classList.remove("remove-highlight-disabled");
  125. editorElement.contentWindow.postMessage(JSON.stringify({ method: "enableRemoveHighlights" }), "*");
  126. } else {
  127. removeHighlightButton.classList.add("remove-highlight-disabled");
  128. editorElement.contentWindow.postMessage(JSON.stringify({ method: "disableRemoveHighlights" }), "*");
  129. }
  130. };
  131. editPageButton.onclick = () => {
  132. if (editPageButton.classList.contains("edit-disabled")) {
  133. editPageButton.classList.remove("edit-disabled");
  134. editorElement.contentWindow.postMessage(JSON.stringify({ method: "enableEditPage" }), "*");
  135. } else {
  136. editPageButton.classList.add("edit-disabled");
  137. editorElement.contentWindow.postMessage(JSON.stringify({ method: "disableEditPage" }), "*");
  138. }
  139. };
  140. cutPageButton.onclick = () => {
  141. if (cutPageButton.classList.contains("cut-disabled")) {
  142. cutPageButton.classList.remove("cut-disabled");
  143. editorElement.contentWindow.postMessage(JSON.stringify({ method: "enableCutPage" }), "*");
  144. } else {
  145. cutPageButton.classList.add("cut-disabled");
  146. editorElement.contentWindow.postMessage(JSON.stringify({ method: "disableCutPage" }), "*");
  147. }
  148. };
  149. undoCutPageButton.onclick = () => {
  150. editorElement.contentWindow.postMessage(JSON.stringify({ method: "undoCutPage" }), "*");
  151. };
  152. undoAllCutPageButton.onclick = () => {
  153. editorElement.contentWindow.postMessage(JSON.stringify({ method: "undoAllCutPage" }), "*");
  154. };
  155. savePageButton.onclick = () => {
  156. savePage();
  157. };
  158. window.onmessage = event => {
  159. const message = JSON.parse(event.data);
  160. if (message.method == "setMetadata") {
  161. document.title = "[SingleFile] " + message.title;
  162. if (message.icon) {
  163. const linkElement = document.createElement("link");
  164. linkElement.rel = "icon";
  165. linkElement.href = message.icon;
  166. document.head.appendChild(linkElement);
  167. }
  168. }
  169. if (message.method == "setContent") {
  170. const pageData = {
  171. content: message.content,
  172. filename: tabData.filename
  173. };
  174. tabData.options.openEditor = false;
  175. singlefile.extension.core.content.download.downloadPage(pageData, tabData.options);
  176. }
  177. };
  178. window.onload = async () => {
  179. tabData = await browser.runtime.sendMessage({ method: "editor.getTabData" });
  180. editorElement.contentWindow.postMessage(JSON.stringify({ method: "init", content: tabData.content }), "*");
  181. };
  182. browser.runtime.onMessage.addListener(message => {
  183. if (message.method == "content.save") {
  184. savePage();
  185. browser.runtime.sendMessage({ method: "ui.processInit" });
  186. return {};
  187. }
  188. if (message.method == "common.promptValueRequest") {
  189. browser.runtime.sendMessage({ method: "tabs.promptValueResponse", value: prompt(message.promptMessage) });
  190. return {};
  191. }
  192. });
  193. function savePage() {
  194. editorElement.contentWindow.postMessage(JSON.stringify({ method: "getContent", compressHTML: tabData.options.compressHTML }), "*");
  195. }
  196. return {};
  197. })();