ui-editor.js 8.9 KB

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