ui-editor.js 10.0 KB

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