ui-editor.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. highlightButtons.forEach(highlightButton => {
  72. highlightButton.onclick = () => {
  73. if (toolbarElement.classList.contains("cut-inner-mode")) {
  74. disableCutInnerPage();
  75. }
  76. if (toolbarElement.classList.contains("cut-outer-mode")) {
  77. disableCutOuterPage();
  78. }
  79. if (toolbarElement.classList.contains("remove-highlight-mode")) {
  80. disableRemoveHighlights();
  81. }
  82. const disabled = highlightButton.classList.contains("highlight-disabled");
  83. resetHighlightButtons();
  84. if (disabled) {
  85. highlightButton.classList.remove("highlight-disabled");
  86. editorElement.contentWindow.postMessage(JSON.stringify({ method: "enableHighlight", color: "single-file-highlight-" + highlightButton.dataset.color }), "*");
  87. } else {
  88. highlightButton.classList.add("highlight-disabled");
  89. }
  90. };
  91. });
  92. toggleNotesButton.onclick = () => {
  93. if (toggleNotesButton.getAttribute("src") == "/extension/ui/resources/button_note_visible.png") {
  94. toggleNotesButton.src = "/extension/ui/resources/button_note_hidden.png";
  95. editorElement.contentWindow.postMessage(JSON.stringify({ method: "hideNotes" }), "*");
  96. } else {
  97. toggleNotesButton.src = "/extension/ui/resources/button_note_visible.png";
  98. editorElement.contentWindow.postMessage(JSON.stringify({ method: "displayNotes" }), "*");
  99. }
  100. };
  101. toggleHighlightsButton.onclick = () => {
  102. if (toggleHighlightsButton.getAttribute("src") == "/extension/ui/resources/button_highlighter_visible.png") {
  103. toggleHighlightsButton.src = "/extension/ui/resources/button_highlighter_hidden.png";
  104. editorElement.contentWindow.postMessage(JSON.stringify({ method: "hideHighlights" }), "*");
  105. } else {
  106. displayHighlights();
  107. }
  108. };
  109. removeHighlightButton.onclick = () => {
  110. if (toolbarElement.classList.contains("cut-inner-mode")) {
  111. disableCutInnerPage();
  112. }
  113. if (toolbarElement.classList.contains("cut-outer-mode")) {
  114. disableCutOuterPage();
  115. }
  116. if (removeHighlightButton.classList.contains("remove-highlight-disabled")) {
  117. removeHighlightButton.classList.remove("remove-highlight-disabled");
  118. toolbarElement.classList.add("remove-highlight-mode");
  119. resetHighlightButtons();
  120. displayHighlights();
  121. editorElement.contentWindow.postMessage(JSON.stringify({ method: "enableRemoveHighlights" }), "*");
  122. editorElement.contentWindow.postMessage(JSON.stringify({ method: "displayHighlights" }), "*");
  123. } else {
  124. disableRemoveHighlights();
  125. }
  126. };
  127. editPageButton.onclick = () => {
  128. if (toolbarElement.classList.contains("cut-inner-mode")) {
  129. disableCutInnerPage();
  130. }
  131. if (toolbarElement.classList.contains("cut-outer-mode")) {
  132. disableCutOuterPage();
  133. }
  134. if (editPageButton.classList.contains("edit-disabled")) {
  135. enableEditPage();
  136. } else {
  137. disableEditPage();
  138. }
  139. };
  140. formatPageButton.onclick = () => {
  141. if (formatPageButton.classList.contains("format-disabled")) {
  142. formatPage();
  143. } else {
  144. cancelFormatPage();
  145. }
  146. };
  147. cutInnerPageButton.onclick = () => {
  148. if (toolbarElement.classList.contains("edit-mode")) {
  149. disableEditPage();
  150. }
  151. if (toolbarElement.classList.contains("cut-outer-mode")) {
  152. disableCutOuterPage();
  153. }
  154. if (cutInnerPageButton.classList.contains("cut-disabled")) {
  155. enableCutInnerPage();
  156. editorElement.contentWindow.focus();
  157. } else {
  158. disableCutInnerPage();
  159. }
  160. };
  161. cutOuterPageButton.onclick = () => {
  162. if (toolbarElement.classList.contains("edit-mode")) {
  163. disableEditPage();
  164. }
  165. if (toolbarElement.classList.contains("cut-inner-mode")) {
  166. disableCutInnerPage();
  167. }
  168. if (cutOuterPageButton.classList.contains("cut-disabled")) {
  169. enableCutOuterPage();
  170. editorElement.contentWindow.focus();
  171. } else {
  172. disableCutOuterPage();
  173. }
  174. };
  175. undoCutPageButton.onclick = () => {
  176. editorElement.contentWindow.postMessage(JSON.stringify({ method: "undoCutPage" }), "*");
  177. editorElement.contentWindow.focus();
  178. };
  179. undoAllCutPageButton.onclick = () => {
  180. editorElement.contentWindow.postMessage(JSON.stringify({ method: "undoAllCutPage" }), "*");
  181. editorElement.contentWindow.focus();
  182. };
  183. redoCutPageButton.onclick = () => {
  184. editorElement.contentWindow.postMessage(JSON.stringify({ method: "redoCutPage" }), "*");
  185. editorElement.contentWindow.focus();
  186. };
  187. savePageButton.onclick = () => {
  188. savePage();
  189. };
  190. let updatedResources = {};
  191. window.onmessage = event => {
  192. const message = JSON.parse(event.data);
  193. if (message.method == "setMetadata") {
  194. document.title = "[SingleFile] " + message.title;
  195. if (message.icon) {
  196. const linkElement = document.createElement("link");
  197. linkElement.rel = "icon";
  198. linkElement.href = message.icon;
  199. document.head.appendChild(linkElement);
  200. }
  201. }
  202. if (message.method == "setContent") {
  203. const pageData = {
  204. content: message.content,
  205. filename: tabData.filename
  206. };
  207. tabData.options.openEditor = false;
  208. singlefile.extension.core.content.download.downloadPage(pageData, tabData.options);
  209. }
  210. if (message.method == "disableFormatPage") {
  211. tabData.options.disableFormatPage = true;
  212. formatPageButton.remove();
  213. }
  214. if (message.method == "onUpdate") {
  215. tabData.docSaved = message.saved;
  216. }
  217. if (message.method == "onInit") {
  218. if (tabData.options.defaultEditorMode == "edit") {
  219. enableEditPage();
  220. } else if (tabData.options.defaultEditorMode == "format" && !tabData.options.disableFormatPage) {
  221. formatPage();
  222. } else if (tabData.options.defaultEditorMode == "cut") {
  223. enableCutInnerPage();
  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. })();