ui-editor.js 14 KB

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