ui-editor.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. const defaultEditorMode = tabData.options.defaultEditorMode;
  219. if (defaultEditorMode == "edit") {
  220. enableEditPage();
  221. } else if (defaultEditorMode == "format" && !tabData.options.disableFormatPage) {
  222. formatPage();
  223. } else if (defaultEditorMode == "cut") {
  224. enableCutInnerPage();
  225. } else if (defaultEditorMode == "cut-external") {
  226. enableCutOuterPage();
  227. }
  228. }
  229. };
  230. window.onload = () => {
  231. browser.runtime.sendMessage({ method: "editor.getTabData" });
  232. browser.runtime.onMessage.addListener(message => {
  233. if (message.method == "devtools.resourceCommitted") {
  234. updatedResources[message.url] = { content: message.content, type: message.type, encoding: message.encoding };
  235. return Promise.resolve({});
  236. }
  237. if (message.method == "content.save") {
  238. tabData.options = message.options;
  239. savePage();
  240. browser.runtime.sendMessage({ method: "ui.processInit" });
  241. return Promise.resolve({});
  242. }
  243. if (message.method == "common.promptValueRequest") {
  244. browser.runtime.sendMessage({ method: "tabs.promptValueResponse", value: prompt(message.promptMessage) });
  245. return Promise.resolve({});
  246. }
  247. if (message.method == "editor.setTabData") {
  248. if (message.truncated) {
  249. tabDataContents.push(message.content);
  250. } else {
  251. tabDataContents = [message.content];
  252. }
  253. if (!message.truncated || message.finished) {
  254. tabData = JSON.parse(tabDataContents.join(""));
  255. tabData.docSaved = true;
  256. tabDataContents = [];
  257. editorElement.contentWindow.postMessage(JSON.stringify({ method: "init", content: tabData.content }), "*");
  258. delete tabData.content;
  259. }
  260. return Promise.resolve({});
  261. }
  262. });
  263. };
  264. window.onbeforeunload = event => {
  265. if (tabData.options.warnUnsavedPage && !tabData.docSaved) {
  266. event.preventDefault();
  267. event.returnValue = "";
  268. }
  269. };
  270. function disableEditPage() {
  271. editPageButton.classList.add("edit-disabled");
  272. toolbarElement.classList.remove("edit-mode");
  273. editorElement.contentWindow.postMessage(JSON.stringify({ method: "disableEditPage" }), "*");
  274. }
  275. function disableCutInnerPage() {
  276. cutInnerPageButton.classList.add("cut-disabled");
  277. toolbarElement.classList.remove("cut-inner-mode");
  278. editorElement.contentWindow.postMessage(JSON.stringify({ method: "disableCutInnerPage" }), "*");
  279. }
  280. function disableCutOuterPage() {
  281. cutOuterPageButton.classList.add("cut-disabled");
  282. toolbarElement.classList.remove("cut-outer-mode");
  283. editorElement.contentWindow.postMessage(JSON.stringify({ method: "disableCutOuterPage" }), "*");
  284. }
  285. function resetHighlightButtons() {
  286. highlightButtons.forEach(highlightButton => highlightButton.classList.add("highlight-disabled"));
  287. editorElement.contentWindow.postMessage(JSON.stringify({ method: "disableHighlight" }), "*");
  288. }
  289. function disableRemoveHighlights() {
  290. toolbarElement.classList.remove("remove-highlight-mode");
  291. removeHighlightButton.classList.add("remove-highlight-disabled");
  292. editorElement.contentWindow.postMessage(JSON.stringify({ method: "disableRemoveHighlights" }), "*");
  293. }
  294. function displayHighlights() {
  295. toggleHighlightsButton.src = "/extension/ui/resources/button_highlighter_visible.png";
  296. editorElement.contentWindow.postMessage(JSON.stringify({ method: "displayHighlights" }), "*");
  297. }
  298. function enableEditPage() {
  299. editPageButton.classList.remove("edit-disabled");
  300. toolbarElement.classList.add("edit-mode");
  301. editorElement.contentWindow.postMessage(JSON.stringify({ method: "enableEditPage" }), "*");
  302. }
  303. function formatPage() {
  304. formatPageButton.classList.remove("format-disabled");
  305. updatedResources = {};
  306. editorElement.contentWindow.postMessage(JSON.stringify({ method: tabData.options.applySystemTheme ? "formatPage" : "formatPageNoTheme" }), "*");
  307. }
  308. function cancelFormatPage() {
  309. formatPageButton.classList.add("format-disabled");
  310. updatedResources = {};
  311. editorElement.contentWindow.postMessage(JSON.stringify({ method: "cancelFormatPage" }), "*");
  312. }
  313. function enableCutInnerPage() {
  314. cutInnerPageButton.classList.remove("cut-disabled");
  315. toolbarElement.classList.add("cut-inner-mode");
  316. resetHighlightButtons();
  317. disableRemoveHighlights();
  318. editorElement.contentWindow.postMessage(JSON.stringify({ method: "enableCutInnerPage" }), "*");
  319. }
  320. function enableCutOuterPage() {
  321. cutOuterPageButton.classList.remove("cut-disabled");
  322. toolbarElement.classList.add("cut-outer-mode");
  323. resetHighlightButtons();
  324. disableRemoveHighlights();
  325. editorElement.contentWindow.postMessage(JSON.stringify({ method: "enableCutOuterPage" }), "*");
  326. }
  327. function savePage() {
  328. editorElement.contentWindow.postMessage(JSON.stringify({ method: "getContent", compressHTML: tabData.options.compressHTML, updatedResources }), "*");
  329. }
  330. return {};
  331. })();