ui-options.js 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  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, window, document, localStorage, FileReader, location, fetch, TextDecoder, DOMParser, HTMLElement */
  24. const HELP_ICON_URL = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAABIUlEQVQ4y+2TsarCMBSGvxTBRdqiUZAWOrhJB9EXcPKFfCvfQYfulUKHDqXg4CYUJSioYO4mSDX3ttzt3n87fMlHTpIjlsulxpDZbEYYhgghSNOUOI5Ny2mZYBAELBYLer0eAJ7ncTweKYri4x7LJJRS0u12n7XrukgpjSc0CpVSXK/XZ32/31FKNW85z3PW6zXT6RSAJEnIsqy5UGvNZrNhu90CcDqd+C6tT6J+v//2Th+PB2VZ1hN2Oh3G4zGTyQTbtl/YbrdjtVpxu91+Ljyfz0RRhG3bzOfzF+Y4TvNXvlwuaK2pE4tfzr/wzwsty0IIURlL0998KxRCMBqN8H2/wlzXJQxD2u12vVkeDoeUZUkURRU+GAw4HA7s9/sK+wK6CWHasQ/S/wAAAABJRU5ErkJggg==";
  25. const HELP_PAGE_PATH = "/extension/ui/pages/help.html";
  26. let DEFAULT_PROFILE_NAME, DISABLED_PROFILE_NAME, CURRENT_PROFILE_NAME;
  27. browser.runtime.sendMessage({ method: "config.getConstants" }).then(data => ({ DEFAULT_PROFILE_NAME, DISABLED_PROFILE_NAME, CURRENT_PROFILE_NAME } = data));
  28. const removeHiddenElementsLabel = document.getElementById("removeHiddenElementsLabel");
  29. const removeUnusedStylesLabel = document.getElementById("removeUnusedStylesLabel");
  30. const removeUnusedFontsLabel = document.getElementById("removeUnusedFontsLabel");
  31. const removeFramesLabel = document.getElementById("removeFramesLabel");
  32. const removeImportsLabel = document.getElementById("removeImportsLabel");
  33. const removeScriptsLabel = document.getElementById("removeScriptsLabel");
  34. const saveRawPageLabel = document.getElementById("saveRawPageLabel");
  35. const insertMetaCSPLabel = document.getElementById("insertMetaCSPLabel");
  36. const saveToClipboardLabel = document.getElementById("saveToClipboardLabel");
  37. const saveToFilesystemLabel = document.getElementById("saveToFilesystemLabel");
  38. const addProofLabel = document.getElementById("addProofLabel");
  39. const woleetKeyLabel = document.getElementById("woleetKeyLabel");
  40. const saveToGDriveLabel = document.getElementById("saveToGDriveLabel");
  41. const saveToGitHubLabel = document.getElementById("saveToGitHubLabel");
  42. const githubTokenLabel = document.getElementById("githubTokenLabel");
  43. const githubUserLabel = document.getElementById("githubUserLabel");
  44. const githubRepositoryLabel = document.getElementById("githubRepositoryLabel");
  45. const githubBranchLabel = document.getElementById("githubBranchLabel");
  46. const saveWithCompanionLabel = document.getElementById("saveWithCompanionLabel");
  47. const compressHTMLLabel = document.getElementById("compressHTMLLabel");
  48. const compressCSSLabel = document.getElementById("compressCSSLabel");
  49. const loadDeferredImagesLabel = document.getElementById("loadDeferredImagesLabel");
  50. const loadDeferredImagesMaxIdleTimeLabel = document.getElementById("loadDeferredImagesMaxIdleTimeLabel");
  51. const loadDeferredImagesKeepZoomLevelLabel = document.getElementById("loadDeferredImagesKeepZoomLevelLabel");
  52. const addMenuEntryLabel = document.getElementById("addMenuEntryLabel");
  53. const filenameTemplateLabel = document.getElementById("filenameTemplateLabel");
  54. const filenameMaxLengthLabel = document.getElementById("filenameMaxLengthLabel");
  55. const shadowEnabledLabel = document.getElementById("shadowEnabledLabel");
  56. const setMaxResourceSizeLabel = document.getElementById("setMaxResourceSizeLabel");
  57. const maxResourceSizeLabel = document.getElementById("maxResourceSizeLabel");
  58. const confirmFilenameLabel = document.getElementById("confirmFilenameLabel");
  59. const filenameConflictActionLabel = document.getElementById("filenameConflictActionLabel");
  60. const filenameConflictActionUniquifyLabel = document.getElementById("filenameConflictActionUniquifyLabel");
  61. const filenameConflictActionOverwriteLabel = document.getElementById("filenameConflictActionOverwriteLabel");
  62. const filenameConflictActionPromptLabel = document.getElementById("filenameConflictActionPromptLabel");
  63. const filenameConflictActionSkipLabel = document.getElementById("filenameConflictActionSkipLabel");
  64. const removeAudioLabel = document.getElementById("removeAudioLabel");
  65. const removeVideoLabel = document.getElementById("removeVideoLabel");
  66. const displayInfobarLabel = document.getElementById("displayInfobarLabel");
  67. const displayStatsLabel = document.getElementById("displayStatsLabel");
  68. const backgroundSaveLabel = document.getElementById("backgroundSaveLabel");
  69. const autoSaveDelayLabel = document.getElementById("autoSaveDelayLabel");
  70. const autoSaveLoadLabel = document.getElementById("autoSaveLoadLabel");
  71. const autoSaveUnloadLabel = document.getElementById("autoSaveUnloadLabel");
  72. const autoSaveLoadOrUnloadLabel = document.getElementById("autoSaveLoadOrUnloadLabel");
  73. const autoSaveDiscardLabel = document.getElementById("autoSaveDiscardLabel");
  74. const autoSaveRemoveLabel = document.getElementById("autoSaveRemoveLabel");
  75. const autoSaveRepeatLabel = document.getElementById("autoSaveRepeatLabel");
  76. const autoSaveRepeatDelayLabel = document.getElementById("autoSaveRepeatDelayLabel");
  77. const autoSaveExternalSaveLabel = document.getElementById("autoSaveExternalSaveLabel");
  78. const removeAlternativeFontsLabel = document.getElementById("removeAlternativeFontsLabel");
  79. const removeAlternativeImagesLabel = document.getElementById("removeAlternativeImagesLabel");
  80. const removeAlternativeMediasLabel = document.getElementById("removeAlternativeMediasLabel");
  81. const saveCreatedBookmarksLabel = document.getElementById("saveCreatedBookmarksLabel");
  82. const passReferrerOnErrorLabel = document.getElementById("passReferrerOnErrorLabel");
  83. const replaceBookmarkURLLabel = document.getElementById("replaceBookmarkURLLabel");
  84. const allowedBookmarkFoldersLabel = document.getElementById("allowedBookmarkFoldersLabel");
  85. const ignoredBookmarkFoldersLabel = document.getElementById("ignoredBookmarkFoldersLabel");
  86. const titleLabel = document.getElementById("titleLabel");
  87. const userInterfaceLabel = document.getElementById("userInterfaceLabel");
  88. const filenameLabel = document.getElementById("filenameLabel");
  89. const htmlContentLabel = document.getElementById("htmlContentLabel");
  90. const imagesLabel = document.getElementById("imagesLabel");
  91. const stylesheetsLabel = document.getElementById("stylesheetsLabel");
  92. const fontsLabel = document.getElementById("fontsLabel");
  93. const otherResourcesLabel = document.getElementById("otherResourcesLabel");
  94. const destinationLabel = document.getElementById("destinationLabel");
  95. const bookmarksLabel = document.getElementById("bookmarksLabel");
  96. const autoSaveLabel = document.getElementById("autoSaveLabel");
  97. const autoSettingsLabel = document.getElementById("autoSettingsLabel");
  98. const autoSettingsUrlLabel = document.getElementById("autoSettingsUrlLabel");
  99. const autoSettingsProfileLabel = document.getElementById("autoSettingsProfileLabel");
  100. const autoSettingsAutoSaveProfileLabel = document.getElementById("autoSettingsAutoSaveProfileLabel");
  101. const showAllProfilesLabel = document.getElementById("showAllProfilesLabel");
  102. const showAutoSaveProfileLabel = document.getElementById("showAutoSaveProfileLabel");
  103. const groupDuplicateImagesLabel = document.getElementById("groupDuplicateImagesLabel");
  104. const confirmInfobarLabel = document.getElementById("confirmInfobarLabel");
  105. const autoCloseLabel = document.getElementById("autoCloseLabel");
  106. const editorLabel = document.getElementById("editorLabel");
  107. const openEditorLabel = document.getElementById("openEditorLabel");
  108. const openSavedPageLabel = document.getElementById("openSavedPageLabel");
  109. const autoOpenEditorLabel = document.getElementById("autoOpenEditorLabel");
  110. const defaultEditorModeLabel = document.getElementById("defaultEditorModeLabel");
  111. const applySystemThemeLabel = document.getElementById("applySystemThemeLabel");
  112. const warnUnsavedPageLabel = document.getElementById("warnUnsavedPageLabel");
  113. const infobarTemplateLabel = document.getElementById("infobarTemplateLabel");
  114. const includeInfobarLabel = document.getElementById("includeInfobarLabel");
  115. const miscLabel = document.getElementById("miscLabel");
  116. const helpLabel = document.getElementById("helpLabel");
  117. const synchronizeLabel = document.getElementById("synchronizeLabel");
  118. const addProfileButton = document.getElementById("addProfileButton");
  119. const deleteProfileButton = document.getElementById("deleteProfileButton");
  120. const renameProfileButton = document.getElementById("renameProfileButton");
  121. const resetButton = document.getElementById("resetButton");
  122. const exportButton = document.getElementById("exportButton");
  123. const importButton = document.getElementById("importButton");
  124. const fileInput = document.getElementById("fileInput");
  125. const profileNamesInput = document.getElementById("profileNamesInput");
  126. const removeHiddenElementsInput = document.getElementById("removeHiddenElementsInput");
  127. const removeUnusedStylesInput = document.getElementById("removeUnusedStylesInput");
  128. const removeUnusedFontsInput = document.getElementById("removeUnusedFontsInput");
  129. const removeFramesInput = document.getElementById("removeFramesInput");
  130. const removeImportsInput = document.getElementById("removeImportsInput");
  131. const removeScriptsInput = document.getElementById("removeScriptsInput");
  132. const saveRawPageInput = document.getElementById("saveRawPageInput");
  133. const insertMetaCSPInput = document.getElementById("insertMetaCSPInput");
  134. const saveToClipboardInput = document.getElementById("saveToClipboardInput");
  135. const addProofInput = document.getElementById("addProofInput");
  136. const woleetKeyInput = document.getElementById("woleetKeyInput");
  137. const saveToGDriveInput = document.getElementById("saveToGDriveInput");
  138. const saveToGitHubInput = document.getElementById("saveToGitHubInput");
  139. const githubTokenInput = document.getElementById("githubTokenInput");
  140. const githubUserInput = document.getElementById("githubUserInput");
  141. const githubRepositoryInput = document.getElementById("githubRepositoryInput");
  142. const githubBranchInput = document.getElementById("githubBranchInput");
  143. const saveWithCompanionInput = document.getElementById("saveWithCompanionInput");
  144. const saveToFilesystemInput = document.getElementById("saveToFilesystemInput");
  145. const compressHTMLInput = document.getElementById("compressHTMLInput");
  146. const compressCSSInput = document.getElementById("compressCSSInput");
  147. const loadDeferredImagesInput = document.getElementById("loadDeferredImagesInput");
  148. const loadDeferredImagesMaxIdleTimeInput = document.getElementById("loadDeferredImagesMaxIdleTimeInput");
  149. const loadDeferredImagesKeepZoomLevelInput = document.getElementById("loadDeferredImagesKeepZoomLevelInput");
  150. const contextMenuEnabledInput = document.getElementById("contextMenuEnabledInput");
  151. const filenameTemplateInput = document.getElementById("filenameTemplateInput");
  152. const filenameMaxLengthInput = document.getElementById("filenameMaxLengthInput");
  153. const shadowEnabledInput = document.getElementById("shadowEnabledInput");
  154. const maxResourceSizeInput = document.getElementById("maxResourceSizeInput");
  155. const maxResourceSizeEnabledInput = document.getElementById("maxResourceSizeEnabledInput");
  156. const confirmFilenameInput = document.getElementById("confirmFilenameInput");
  157. const filenameConflictActionInput = document.getElementById("filenameConflictActionInput");
  158. const removeAudioSrcInput = document.getElementById("removeAudioSrcInput");
  159. const removeVideoSrcInput = document.getElementById("removeVideoSrcInput");
  160. const displayInfobarInput = document.getElementById("displayInfobarInput");
  161. const displayStatsInput = document.getElementById("displayStatsInput");
  162. const backgroundSaveInput = document.getElementById("backgroundSaveInput");
  163. const autoSaveDelayInput = document.getElementById("autoSaveDelayInput");
  164. const autoSaveLoadInput = document.getElementById("autoSaveLoadInput");
  165. const autoSaveUnloadInput = document.getElementById("autoSaveUnloadInput");
  166. const autoSaveDiscardInput = document.getElementById("autoSaveDiscardInput");
  167. const autoSaveRemoveInput = document.getElementById("autoSaveRemoveInput");
  168. const autoSaveLoadOrUnloadInput = document.getElementById("autoSaveLoadOrUnloadInput");
  169. const autoSaveRepeatInput = document.getElementById("autoSaveRepeatInput");
  170. const autoSaveRepeatDelayInput = document.getElementById("autoSaveRepeatDelayInput");
  171. const autoSaveExternalSaveInput = document.getElementById("autoSaveExternalSaveInput");
  172. const removeAlternativeFontsInput = document.getElementById("removeAlternativeFontsInput");
  173. const removeAlternativeImagesInput = document.getElementById("removeAlternativeImagesInput");
  174. const removeAlternativeMediasInput = document.getElementById("removeAlternativeMediasInput");
  175. const saveCreatedBookmarksInput = document.getElementById("saveCreatedBookmarksInput");
  176. const passReferrerOnErrorInput = document.getElementById("passReferrerOnErrorInput");
  177. const replaceBookmarkURLInput = document.getElementById("replaceBookmarkURLInput");
  178. const allowedBookmarkFoldersInput = document.getElementById("allowedBookmarkFoldersInput");
  179. const ignoredBookmarkFoldersInput = document.getElementById("ignoredBookmarkFoldersInput");
  180. const groupDuplicateImagesInput = document.getElementById("groupDuplicateImagesInput");
  181. const infobarTemplateInput = document.getElementById("infobarTemplateInput");
  182. const includeInfobarInput = document.getElementById("includeInfobarInput");
  183. const confirmInfobarInput = document.getElementById("confirmInfobarInput");
  184. const autoCloseInput = document.getElementById("autoCloseInput");
  185. const openEditorInput = document.getElementById("openEditorInput");
  186. const openSavedPageInput = document.getElementById("openSavedPageInput");
  187. const autoOpenEditorInput = document.getElementById("autoOpenEditorInput");
  188. const defaultEditorModeInput = document.getElementById("defaultEditorModeInput");
  189. const defaultEditorModeNormalLabel = document.getElementById("defaultEditorModeNormalLabel");
  190. const defaultEditorModeEditLabel = document.getElementById("defaultEditorModeEditLabel");
  191. const defaultEditorModeFormatLabel = document.getElementById("defaultEditorModeFormatLabel");
  192. const defaultEditorModeCutLabel = document.getElementById("defaultEditorModeCutLabel");
  193. const defaultEditorModeCutExternalLabel = document.getElementById("defaultEditorModeCutExternalLabel");
  194. const applySystemThemeInput = document.getElementById("applySystemThemeInput");
  195. const warnUnsavedPageInput = document.getElementById("warnUnsavedPageInput");
  196. const expandAllButton = document.getElementById("expandAllButton");
  197. const rulesDeleteAllButton = document.getElementById("rulesDeleteAllButton");
  198. const ruleUrlInput = document.getElementById("ruleUrlInput");
  199. const ruleProfileInput = document.getElementById("ruleProfileInput");
  200. const ruleAutoSaveProfileInput = document.getElementById("ruleAutoSaveProfileInput");
  201. const ruleEditProfileInput = document.getElementById("ruleEditProfileInput");
  202. const ruleEditAutoSaveProfileInput = document.getElementById("ruleEditAutoSaveProfileInput");
  203. const ruleAddButton = document.getElementById("ruleAddButton");
  204. const rulesElement = document.querySelector(".rules-table");
  205. const rulesContainerElement = document.querySelector(".rules-table-container");
  206. const ruleEditUrlInput = document.getElementById("ruleEditUrlInput");
  207. const ruleEditButton = document.getElementById("ruleEditButton");
  208. const createURLElement = rulesElement.querySelector(".rule-create");
  209. const showAllProfilesInput = document.getElementById("showAllProfilesInput");
  210. const showAutoSaveProfileInput = document.getElementById("showAutoSaveProfileInput");
  211. const synchronizeInput = document.getElementById("synchronizeInput");
  212. const resetAllButton = document.getElementById("resetAllButton");
  213. const resetCurrentButton = document.getElementById("resetCurrentButton");
  214. const resetCancelButton = document.getElementById("resetCancelButton");
  215. const confirmButton = document.getElementById("confirmButton");
  216. const cancelButton = document.getElementById("cancelButton");
  217. const promptInput = document.getElementById("promptInput");
  218. const promptCancelButton = document.getElementById("promptCancelButton");
  219. const promptConfirmButton = document.getElementById("promptConfirmButton");
  220. let sidePanelDisplay;
  221. if (location.href.endsWith("#side-panel")) {
  222. sidePanelDisplay = true;
  223. document.querySelector(".options-title").remove();
  224. document.documentElement.classList.add("side-panel");
  225. }
  226. browser.runtime.onMessage.addListener(message => {
  227. if (message.method == "options.refresh" || (message.method == "options.refreshPanel" && sidePanelDisplay)) {
  228. refresh(message.profileName);
  229. }
  230. });
  231. let pendingSave = Promise.resolve();
  232. let autoSaveProfileChanged;
  233. ruleProfileInput.onchange = () => {
  234. if (!autoSaveProfileChanged && ruleProfileInput.value != CURRENT_PROFILE_NAME) {
  235. ruleAutoSaveProfileInput.value = ruleProfileInput.value;
  236. }
  237. };
  238. ruleAutoSaveProfileInput.onchange = () => {
  239. autoSaveProfileChanged = true;
  240. };
  241. rulesDeleteAllButton.addEventListener("click", async event => {
  242. if (await confirm(browser.i18n.getMessage("optionsDeleteDisplayedRulesConfirm"), event.clientY - 100)) {
  243. await browser.runtime.sendMessage({ method: "config.deleteRules", profileName: !showAllProfilesInput.checked && profileNamesInput.value });
  244. await refresh();
  245. await refreshExternalComponents();
  246. }
  247. }, false);
  248. createURLElement.onsubmit = async event => {
  249. event.preventDefault();
  250. try {
  251. await browser.runtime.sendMessage({ method: "config.addRule", url: ruleUrlInput.value, profileName: ruleProfileInput.value, autoSaveProfileName: ruleAutoSaveProfileInput.value });
  252. } catch (error) {
  253. // ignored
  254. }
  255. ruleUrlInput.value = "";
  256. ruleProfileInput.value = ruleAutoSaveProfileInput.value = DEFAULT_PROFILE_NAME;
  257. autoSaveProfileChanged = false;
  258. await refresh();
  259. await refreshExternalComponents();
  260. ruleUrlInput.focus();
  261. };
  262. ruleUrlInput.onclick = ruleUrlInput.onkeyup = ruleUrlInput.onchange = async () => {
  263. ruleAddButton.disabled = !ruleUrlInput.value;
  264. const rules = await browser.runtime.sendMessage({ method: "config.getRules" });
  265. if (rules.find(rule => rule.url == ruleUrlInput.value)) {
  266. ruleAddButton.disabled = true;
  267. }
  268. };
  269. ruleEditUrlInput.onclick = ruleEditUrlInput.onkeyup = ruleEditUrlInput.onchange = async () => {
  270. ruleEditButton.disabled = !ruleEditUrlInput.value;
  271. const rules = await browser.runtime.sendMessage({ method: "config.getRules" });
  272. if (rules.find(rule => rule.url == ruleEditUrlInput.value)) {
  273. ruleEditButton.disabled = true;
  274. }
  275. };
  276. if (getLocalStorageItem("optionShowAutoSaveProfile")) {
  277. showAutoSaveProfileInput.checked = true;
  278. rulesContainerElement.classList.remove("compact");
  279. }
  280. showAutoSaveProfileInput.addEventListener("click", () => {
  281. if (showAutoSaveProfileInput.checked) {
  282. setLocalStorageItem("optionShowAutoSaveProfile", 1);
  283. rulesContainerElement.classList.remove("compact");
  284. } else {
  285. removeLocalStorageItem("optionShowAutoSaveProfile");
  286. rulesContainerElement.classList.add("compact");
  287. }
  288. }, false);
  289. if (getLocalStorageItem("optionShowAllProfiles")) {
  290. showAllProfilesInput.checked = true;
  291. }
  292. showAllProfilesInput.addEventListener("click", () => {
  293. if (showAllProfilesInput.checked) {
  294. setLocalStorageItem("optionShowAllProfiles", 1);
  295. } else {
  296. removeLocalStorageItem("optionShowAllProfiles");
  297. }
  298. }, false);
  299. addProfileButton.addEventListener("click", async event => {
  300. const profileName = await prompt(browser.i18n.getMessage("profileAddPrompt"), event.clientY + 50);
  301. if (profileName) {
  302. try {
  303. await browser.runtime.sendMessage({ method: "config.createProfile", profileName, fromProfileName: profileNamesInput.value });
  304. } catch (error) {
  305. // ignored
  306. }
  307. if (sidePanelDisplay) {
  308. await refresh();
  309. } else {
  310. await refresh(profileName);
  311. }
  312. await refreshExternalComponents();
  313. }
  314. }, false);
  315. deleteProfileButton.addEventListener("click", async event => {
  316. if (await confirm(browser.i18n.getMessage("profileDeleteConfirm"), event.clientY + 50)) {
  317. try {
  318. await browser.runtime.sendMessage({ method: "config.deleteProfile", profileName: profileNamesInput.value });
  319. } catch (error) {
  320. // ignored
  321. }
  322. profileNamesInput.value = null;
  323. await refresh();
  324. await refreshExternalComponents();
  325. }
  326. }, false);
  327. renameProfileButton.addEventListener("click", async event => {
  328. const profileName = await prompt(browser.i18n.getMessage("profileRenamePrompt"), event.clientY + 50, profileNamesInput.value);
  329. if (profileName) {
  330. try {
  331. await browser.runtime.sendMessage({ method: "config.renameProfile", profileName: profileNamesInput.value, newProfileName: profileName });
  332. } catch (error) {
  333. // ignored
  334. }
  335. await refresh(profileName);
  336. await refreshExternalComponents();
  337. }
  338. }, false);
  339. resetButton.addEventListener("click", async event => {
  340. const choice = await reset(event.clientY - 250);
  341. if (choice) {
  342. if (choice == "all") {
  343. await browser.runtime.sendMessage({ method: "config.resetProfiles" });
  344. await refresh(DEFAULT_PROFILE_NAME);
  345. await refreshExternalComponents();
  346. }
  347. if (choice == "current") {
  348. await browser.runtime.sendMessage({ method: "config.resetProfile", profileName: profileNamesInput.value });
  349. await refresh();
  350. await refreshExternalComponents();
  351. }
  352. await update();
  353. }
  354. }, false);
  355. exportButton.addEventListener("click", async () => {
  356. await browser.runtime.sendMessage({ method: "config.exportConfig" });
  357. }, false);
  358. importButton.addEventListener("click", () => {
  359. fileInput.onchange = async () => {
  360. if (fileInput.files.length) {
  361. const reader = new FileReader();
  362. reader.readAsText(fileInput.files[0]);
  363. const serializedConfig = await new Promise((resolve, reject) => {
  364. reader.addEventListener("load", () => resolve(reader.result), false);
  365. reader.addEventListener("error", reject, false);
  366. });
  367. const config = JSON.parse(serializedConfig);
  368. await browser.runtime.sendMessage({ method: "config.importConfig", config });
  369. await refresh(DEFAULT_PROFILE_NAME);
  370. await refreshExternalComponents();
  371. fileInput.value = "";
  372. }
  373. };
  374. fileInput.click();
  375. }, false);
  376. autoSaveUnloadInput.addEventListener("click", async () => {
  377. if (!autoSaveLoadInput.checked && !autoSaveUnloadInput.checked) {
  378. autoSaveLoadOrUnloadInput.checked = true;
  379. }
  380. }, false);
  381. autoSaveLoadInput.addEventListener("click", async () => {
  382. if (!autoSaveLoadInput.checked && !autoSaveUnloadInput.checked) {
  383. autoSaveLoadOrUnloadInput.checked = true;
  384. }
  385. }, false);
  386. autoSaveLoadOrUnloadInput.addEventListener("click", async () => {
  387. if (autoSaveLoadOrUnloadInput.checked) {
  388. autoSaveUnloadInput.checked = autoSaveLoadInput.checked = false;
  389. } else {
  390. autoSaveUnloadInput.checked = false;
  391. }
  392. }, false);
  393. expandAllButton.addEventListener("click", () => {
  394. if (expandAllButton.className) {
  395. expandAllButton.className = "";
  396. } else {
  397. expandAllButton.className = "opened";
  398. }
  399. document.querySelectorAll("details").forEach(detailElement => detailElement.open = Boolean(expandAllButton.className));
  400. }, false);
  401. removeScriptsInput.addEventListener("click", () => {
  402. if (!removeScriptsInput.checked) {
  403. removeHiddenElementsInput.checked = false;
  404. removeUnusedStylesInput.checked = false;
  405. }
  406. }, false);
  407. saveCreatedBookmarksInput.addEventListener("click", saveCreatedBookmarks, false);
  408. passReferrerOnErrorInput.addEventListener("click", passReferrerOnError, false);
  409. autoSaveExternalSaveInput.addEventListener("click", () => enableExternalSave(autoSaveExternalSaveInput), false);
  410. saveWithCompanionInput.addEventListener("click", () => enableExternalSave(saveWithCompanionInput), false);
  411. saveToFilesystemInput.addEventListener("click", async () => await browser.runtime.sendMessage({ method: "downloads.disableGDrive" }), false);
  412. saveToClipboardInput.addEventListener("click", async () => await browser.runtime.sendMessage({ method: "downloads.disableGDrive" }), false);
  413. saveWithCompanionInput.addEventListener("click", async () => await browser.runtime.sendMessage({ method: "downloads.disableGDrive" }), false);
  414. addProofInput.addEventListener("click", async event => {
  415. if (addProofInput.checked) {
  416. addProofInput.checked = false;
  417. if (await confirm(browser.i18n.getMessage("optionsAddProofConfirm"), event.clientY - 100)) {
  418. addProofInput.checked = true;
  419. woleetKeyInput.disabled = false;
  420. }
  421. await update();
  422. }
  423. });
  424. browser.runtime.sendMessage({ method: "config.isSync" }).then(data => synchronizeInput.checked = data.sync);
  425. synchronizeInput.addEventListener("click", async () => {
  426. if (synchronizeInput.checked) {
  427. await browser.runtime.sendMessage({ method: "config.enableSync" });
  428. await refresh(DEFAULT_PROFILE_NAME);
  429. } else {
  430. await browser.runtime.sendMessage({ method: "config.disableSync" });
  431. await refresh();
  432. }
  433. }, false);
  434. document.body.onchange = async event => {
  435. let target = event.target;
  436. if (target != ruleUrlInput &&
  437. target != ruleProfileInput &&
  438. target != ruleAutoSaveProfileInput &&
  439. target != ruleEditUrlInput &&
  440. target != ruleEditProfileInput &&
  441. target != ruleEditAutoSaveProfileInput &&
  442. target != showAutoSaveProfileInput &&
  443. target != saveCreatedBookmarksInput &&
  444. target != passReferrerOnErrorInput) {
  445. if (target != profileNamesInput && target != showAllProfilesInput) {
  446. await update();
  447. }
  448. if (target == profileNamesInput) {
  449. await refresh(profileNamesInput.value);
  450. if (sidePanelDisplay) {
  451. const tabsData = await browser.runtime.sendMessage({ method: "tabsData.get" });
  452. tabsData.profileName = profileNamesInput.value;
  453. await browser.runtime.sendMessage({ method: "tabsData.set", tabsData: tabsData });
  454. await browser.runtime.sendMessage({ method: "ui.refreshMenu" });
  455. }
  456. } else {
  457. if (target == contextMenuEnabledInput) {
  458. await browser.runtime.sendMessage({ method: "ui.refreshMenu" });
  459. }
  460. if (target == openEditorInput) {
  461. await browser.runtime.sendMessage({ method: "ui.refreshMenu" });
  462. }
  463. await refresh();
  464. }
  465. }
  466. };
  467. addProfileButton.title = browser.i18n.getMessage("profileAddButtonTooltip");
  468. deleteProfileButton.title = browser.i18n.getMessage("profileDeleteButtonTooltip");
  469. renameProfileButton.title = browser.i18n.getMessage("profileRenameButtonTooltip");
  470. removeHiddenElementsLabel.textContent = browser.i18n.getMessage("optionRemoveHiddenElements");
  471. removeUnusedStylesLabel.textContent = browser.i18n.getMessage("optionRemoveUnusedStyles");
  472. removeUnusedFontsLabel.textContent = browser.i18n.getMessage("optionRemoveUnusedFonts");
  473. removeFramesLabel.textContent = browser.i18n.getMessage("optionRemoveFrames");
  474. removeImportsLabel.textContent = browser.i18n.getMessage("optionRemoveImports");
  475. removeScriptsLabel.textContent = browser.i18n.getMessage("optionRemoveScripts");
  476. saveRawPageLabel.textContent = browser.i18n.getMessage("optionSaveRawPage");
  477. insertMetaCSPLabel.textContent = browser.i18n.getMessage("optionInsertMetaCSP");
  478. saveToClipboardLabel.textContent = browser.i18n.getMessage("optionSaveToClipboard");
  479. saveToFilesystemLabel.textContent = browser.i18n.getMessage("optionSaveToFilesystem");
  480. addProofLabel.textContent = browser.i18n.getMessage("optionAddProof");
  481. woleetKeyLabel.textContent = browser.i18n.getMessage("optionWoleetKey");
  482. saveToGDriveLabel.textContent = browser.i18n.getMessage("optionSaveToGDrive");
  483. saveToGitHubLabel.textContent = browser.i18n.getMessage("optionSaveToGitHub");
  484. githubTokenLabel.textContent = browser.i18n.getMessage("optionGitHubToken");
  485. githubUserLabel.textContent = browser.i18n.getMessage("optionGitHubUser");
  486. githubRepositoryLabel.textContent = browser.i18n.getMessage("optionGitHubRepository");
  487. githubBranchLabel.textContent = browser.i18n.getMessage("optionGitHubBranch");
  488. saveWithCompanionLabel.textContent = browser.i18n.getMessage("optionSaveWithCompanion");
  489. compressHTMLLabel.textContent = browser.i18n.getMessage("optionCompressHTML");
  490. compressCSSLabel.textContent = browser.i18n.getMessage("optionCompressCSS");
  491. loadDeferredImagesLabel.textContent = browser.i18n.getMessage("optionLoadDeferredImages");
  492. loadDeferredImagesMaxIdleTimeLabel.textContent = browser.i18n.getMessage("optionLoadDeferredImagesMaxIdleTime");
  493. loadDeferredImagesKeepZoomLevelLabel.textContent = browser.i18n.getMessage("optionLoadDeferredImagesKeepZoomLevel");
  494. addMenuEntryLabel.textContent = browser.i18n.getMessage("optionAddMenuEntry");
  495. filenameTemplateLabel.textContent = browser.i18n.getMessage("optionFilenameTemplate");
  496. filenameMaxLengthLabel.textContent = browser.i18n.getMessage("optionFilenameMaxLength");
  497. shadowEnabledLabel.textContent = browser.i18n.getMessage("optionDisplayShadow");
  498. setMaxResourceSizeLabel.textContent = browser.i18n.getMessage("optionSetMaxResourceSize");
  499. maxResourceSizeLabel.textContent = browser.i18n.getMessage("optionMaxResourceSize");
  500. confirmFilenameLabel.textContent = browser.i18n.getMessage("optionConfirmFilename");
  501. filenameConflictActionLabel.textContent = browser.i18n.getMessage("optionFilenameConflictAction");
  502. filenameConflictActionUniquifyLabel.textContent = browser.i18n.getMessage("optionFilenameConflictActionUniquify");
  503. filenameConflictActionOverwriteLabel.textContent = browser.i18n.getMessage("optionFilenameConflictActionOverwrite");
  504. filenameConflictActionPromptLabel.textContent = browser.i18n.getMessage("optionFilenameConflictActionPrompt");
  505. filenameConflictActionSkipLabel.textContent = browser.i18n.getMessage("optionFilenameConflictActionSkip");
  506. removeAudioLabel.textContent = browser.i18n.getMessage("optionRemoveAudio");
  507. removeVideoLabel.textContent = browser.i18n.getMessage("optionRemoveVideo");
  508. displayInfobarLabel.textContent = browser.i18n.getMessage("optionDisplayInfobar");
  509. displayStatsLabel.textContent = browser.i18n.getMessage("optionDisplayStats");
  510. backgroundSaveLabel.textContent = browser.i18n.getMessage("optionBackgroundSave");
  511. autoSaveDelayLabel.textContent = browser.i18n.getMessage("optionAutoSaveDelay");
  512. autoSaveLoadLabel.textContent = browser.i18n.getMessage("optionAutoSaveLoad");
  513. autoSaveUnloadLabel.textContent = browser.i18n.getMessage("optionAutoSaveUnload");
  514. autoSaveLoadOrUnloadLabel.textContent = browser.i18n.getMessage("optionAutoSaveLoadOrUnload");
  515. autoSaveDiscardLabel.textContent = browser.i18n.getMessage("optionAutoSaveDiscard");
  516. autoSaveRemoveLabel.textContent = browser.i18n.getMessage("optionAutoSaveRemove");
  517. autoSaveRepeatLabel.textContent = browser.i18n.getMessage("optionAutoSaveRepeat");
  518. autoSaveRepeatDelayLabel.textContent = browser.i18n.getMessage("optionAutoSaveRepeatDelay");
  519. autoSaveExternalSaveLabel.textContent = browser.i18n.getMessage("optionAutoSaveExternalSave");
  520. removeAlternativeFontsLabel.textContent = browser.i18n.getMessage("optionRemoveAlternativeFonts");
  521. removeAlternativeImagesLabel.textContent = browser.i18n.getMessage("optionRemoveAlternativeImages");
  522. removeAlternativeMediasLabel.textContent = browser.i18n.getMessage("optionRemoveAlternativeMedias");
  523. saveCreatedBookmarksLabel.textContent = browser.i18n.getMessage("optionSaveCreatedBookmarks");
  524. passReferrerOnErrorLabel.textContent = browser.i18n.getMessage("optionPassReferrerOnError");
  525. replaceBookmarkURLLabel.textContent = browser.i18n.getMessage("optionReplaceBookmarkURL");
  526. allowedBookmarkFoldersLabel.textContent = browser.i18n.getMessage("optionAllowedBookmarkFolders");
  527. ignoredBookmarkFoldersLabel.textContent = browser.i18n.getMessage("optionIgnoredBookmarkFolders");
  528. groupDuplicateImagesLabel.textContent = browser.i18n.getMessage("optionGroupDuplicateImages");
  529. titleLabel.textContent = browser.i18n.getMessage("optionsTitle");
  530. userInterfaceLabel.textContent = browser.i18n.getMessage("optionsUserInterfaceSubTitle");
  531. filenameLabel.textContent = browser.i18n.getMessage("optionsFileNameSubTitle");
  532. htmlContentLabel.textContent = browser.i18n.getMessage("optionsHTMLContentSubTitle");
  533. imagesLabel.textContent = browser.i18n.getMessage("optionsImagesSubTitle");
  534. stylesheetsLabel.textContent = browser.i18n.getMessage("optionsStylesheetsSubTitle");
  535. fontsLabel.textContent = browser.i18n.getMessage("optionsFontsSubTitle");
  536. otherResourcesLabel.textContent = browser.i18n.getMessage("optionsOtherResourcesSubTitle");
  537. destinationLabel.textContent = browser.i18n.getMessage("optionsDestionationSubTitle");
  538. bookmarksLabel.textContent = browser.i18n.getMessage("optionsBookmarkSubTitle");
  539. autoSaveLabel.textContent = browser.i18n.getMessage("optionsAutoSaveSubTitle");
  540. miscLabel.textContent = browser.i18n.getMessage("optionsMiscSubTitle");
  541. helpLabel.textContent = browser.i18n.getMessage("optionsHelpLink");
  542. infobarTemplateLabel.textContent = browser.i18n.getMessage("optionInfobarTemplate");
  543. includeInfobarLabel.textContent = browser.i18n.getMessage("optionIncludeInfobar");
  544. confirmInfobarLabel.textContent = browser.i18n.getMessage("optionConfirmInfobar");
  545. autoCloseLabel.textContent = browser.i18n.getMessage("optionAutoClose");
  546. editorLabel.textContent = browser.i18n.getMessage("optionsEditorSubTitle");
  547. openEditorLabel.textContent = browser.i18n.getMessage("optionOpenEditor");
  548. openSavedPageLabel.textContent = browser.i18n.getMessage("optionOpenSavedPage");
  549. autoOpenEditorLabel.textContent = browser.i18n.getMessage("optionAutoOpenEditor");
  550. defaultEditorModeLabel.textContent = browser.i18n.getMessage("optionDefaultEditorMode");
  551. defaultEditorModeNormalLabel.textContent = browser.i18n.getMessage("optionDefaultEditorModeNormal");
  552. defaultEditorModeEditLabel.textContent = browser.i18n.getMessage("optionDefaultEditorModeEdit");
  553. defaultEditorModeFormatLabel.textContent = browser.i18n.getMessage("optionDefaultEditorModeFormat");
  554. defaultEditorModeCutLabel.textContent = browser.i18n.getMessage("optionDefaultEditorModeCut");
  555. defaultEditorModeCutExternalLabel.textContent = browser.i18n.getMessage("optionDefaultEditorModeCutExternal");
  556. applySystemThemeLabel.textContent = browser.i18n.getMessage("optionApplySystemTheme");
  557. warnUnsavedPageLabel.textContent = browser.i18n.getMessage("optionWarnUnsavedPage");
  558. resetButton.textContent = browser.i18n.getMessage("optionsResetButton");
  559. exportButton.textContent = browser.i18n.getMessage("optionsExportButton");
  560. importButton.textContent = browser.i18n.getMessage("optionsImportButton");
  561. resetButton.title = browser.i18n.getMessage("optionsResetTooltip");
  562. autoSettingsLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsSubTitle");
  563. autoSettingsUrlLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsUrl");
  564. autoSettingsProfileLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsProfile");
  565. autoSettingsAutoSaveProfileLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsAutoSaveProfile");
  566. ruleAddButton.title = browser.i18n.getMessage("optionsAddRuleTooltip");
  567. ruleEditButton.title = browser.i18n.getMessage("optionsValidateChangesTooltip");
  568. rulesDeleteAllButton.title = browser.i18n.getMessage("optionsDeleteRulesTooltip");
  569. showAllProfilesLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsShowAllProfiles");
  570. showAutoSaveProfileLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsShowAutoSaveProfile");
  571. ruleUrlInput.placeholder = ruleEditUrlInput.placeholder = browser.i18n.getMessage("optionsAutoSettingsUrlPlaceholder");
  572. synchronizeLabel.textContent = browser.i18n.getMessage("optionSynchronize");
  573. resetAllButton.textContent = browser.i18n.getMessage("optionsResetAllButton");
  574. resetCurrentButton.textContent = browser.i18n.getMessage("optionsResetCurrentButton");
  575. resetCancelButton.textContent = promptCancelButton.textContent = cancelButton.textContent = browser.i18n.getMessage("optionsCancelButton");
  576. confirmButton.textContent = promptConfirmButton.textContent = browser.i18n.getMessage("optionsOKButton");
  577. document.getElementById("resetConfirmLabel").textContent = browser.i18n.getMessage("optionsResetConfirm");
  578. if (location.href.endsWith("#")) {
  579. document.querySelector(".new-window-link").remove();
  580. document.documentElement.classList.add("maximized");
  581. }
  582. let tabsData;
  583. browser.runtime.sendMessage({ method: "tabsData.get" }).then(allTabsData => {
  584. tabsData = allTabsData;
  585. return refresh(tabsData.profileName);
  586. });
  587. getHelpContents();
  588. async function refresh(profileName) {
  589. const [profiles, rules, companionState] = await Promise.all([
  590. browser.runtime.sendMessage({ method: "config.getProfiles" }),
  591. browser.runtime.sendMessage({ method: "config.getRules" }),
  592. browser.runtime.sendMessage({ method: "companion.state" })]);
  593. const selectedProfileName = profileName || profileNamesInput.value || DEFAULT_PROFILE_NAME;
  594. Array.from(profileNamesInput.childNodes).forEach(node => node.remove());
  595. profileNamesInput.options.length = 0;
  596. ruleProfileInput.options.length = 0;
  597. ruleAutoSaveProfileInput.options.length = 0;
  598. ruleEditProfileInput.options.length = 0;
  599. ruleEditAutoSaveProfileInput.options.length = 0;
  600. let optionElement = document.createElement("option");
  601. optionElement.value = DEFAULT_PROFILE_NAME;
  602. optionElement.textContent = browser.i18n.getMessage("profileDefaultSettings");
  603. [CURRENT_PROFILE_NAME].concat(...Object.keys(profiles)).forEach(profileName => {
  604. const optionElement = document.createElement("option");
  605. optionElement.value = optionElement.textContent = profileName;
  606. if (profileName == DEFAULT_PROFILE_NAME) {
  607. optionElement.textContent = browser.i18n.getMessage("profileDefaultSettings");
  608. }
  609. if (profileName != CURRENT_PROFILE_NAME) {
  610. profileNamesInput.appendChild(optionElement);
  611. }
  612. ruleProfileInput.appendChild(optionElement.cloneNode(true));
  613. ruleAutoSaveProfileInput.appendChild(optionElement.cloneNode(true));
  614. ruleEditProfileInput.appendChild(optionElement.cloneNode(true));
  615. ruleEditAutoSaveProfileInput.appendChild(optionElement.cloneNode(true));
  616. });
  617. profileNamesInput.disabled = profileNamesInput.options.length == 1;
  618. optionElement = document.createElement("option");
  619. optionElement.value = DISABLED_PROFILE_NAME;
  620. optionElement.textContent = browser.i18n.getMessage("profileDisabled");
  621. ruleAutoSaveProfileInput.appendChild(optionElement);
  622. ruleEditAutoSaveProfileInput.appendChild(optionElement.cloneNode(true));
  623. const rulesDataElement = rulesElement.querySelector(".rules-data");
  624. Array.from(rulesDataElement.childNodes).forEach(node => (!node.className || !node.className.includes("rule-edit")) && node.remove());
  625. const editURLElement = rulesElement.querySelector(".rule-edit");
  626. createURLElement.hidden = false;
  627. editURLElement.hidden = true;
  628. ruleProfileInput.value = ruleAutoSaveProfileInput.value = selectedProfileName;
  629. let rulesDisplayed;
  630. rules.forEach(rule => {
  631. if (showAllProfilesInput.checked || selectedProfileName == rule.profile || selectedProfileName == rule.autoSaveProfile) {
  632. rulesDisplayed = true;
  633. const ruleElement = rulesElement.querySelector(".rule-view").cloneNode(true);
  634. const ruleUrlElement = ruleElement.querySelector(".rule-url");
  635. const ruleProfileElement = ruleElement.querySelector(".rule-profile");
  636. const ruleAutoSaveProfileElement = ruleElement.querySelector(".rule-autosave-profile");
  637. ruleUrlElement.textContent = ruleUrlElement.title = rule.url;
  638. ruleProfileElement.textContent = ruleProfileElement.title = getProfileText(rule.profile);
  639. ruleAutoSaveProfileElement.textContent = ruleAutoSaveProfileElement.title = getProfileText(rule.autoSaveProfile);
  640. ruleElement.hidden = false;
  641. ruleElement.className = "tr data";
  642. rulesDataElement.appendChild(ruleElement);
  643. const ruleDeleteButton = ruleElement.querySelector(".rule-delete-button");
  644. const ruleUpdateButton = ruleElement.querySelector(".rule-update-button");
  645. ruleDeleteButton.title = browser.i18n.getMessage("optionsDeleteRuleTooltip");
  646. ruleDeleteButton.addEventListener("click", async event => {
  647. if (await confirm(browser.i18n.getMessage("optionsDeleteRuleConfirm"), event.clientY - 100)) {
  648. await browser.runtime.sendMessage({ method: "config.deleteRule", url: rule.url });
  649. await refresh();
  650. await refreshExternalComponents();
  651. }
  652. }, false);
  653. ruleUpdateButton.title = browser.i18n.getMessage("optionsUpdateRuleTooltip");
  654. ruleUpdateButton.addEventListener("click", async () => {
  655. if (editURLElement.hidden) {
  656. createURLElement.hidden = true;
  657. editURLElement.hidden = false;
  658. rulesDataElement.replaceChild(editURLElement, ruleElement);
  659. ruleEditUrlInput.value = rule.url;
  660. ruleEditProfileInput.value = rule.profile;
  661. ruleEditAutoSaveProfileInput.value = rule.autoSaveProfile;
  662. ruleEditUrlInput.focus();
  663. editURLElement.onsubmit = async event => {
  664. event.preventDefault();
  665. rulesElement.appendChild(editURLElement);
  666. await browser.runtime.sendMessage({ method: "config.updateRule", url: rule.url, newUrl: ruleEditUrlInput.value, profileName: ruleEditProfileInput.value, autoSaveProfileName: ruleEditAutoSaveProfileInput.value });
  667. await refresh();
  668. await refreshExternalComponents();
  669. ruleUrlInput.focus();
  670. };
  671. }
  672. }, false);
  673. }
  674. });
  675. rulesDeleteAllButton.disabled = !rulesDisplayed;
  676. rulesElement.appendChild(createURLElement);
  677. profileNamesInput.value = selectedProfileName;
  678. renameProfileButton.disabled = deleteProfileButton.disabled = profileNamesInput.value == DEFAULT_PROFILE_NAME;
  679. const profileOptions = profiles[selectedProfileName];
  680. removeHiddenElementsInput.checked = profileOptions.removeHiddenElements;
  681. removeUnusedStylesInput.checked = profileOptions.removeUnusedStyles;
  682. removeUnusedFontsInput.checked = profileOptions.removeUnusedFonts;
  683. removeFramesInput.checked = profileOptions.removeFrames;
  684. removeImportsInput.checked = profileOptions.removeImports;
  685. removeScriptsInput.checked = profileOptions.removeScripts;
  686. saveRawPageInput.checked = profileOptions.saveRawPage;
  687. insertMetaCSPInput.checked = profileOptions.insertMetaCSP;
  688. saveToClipboardInput.checked = profileOptions.saveToClipboard;
  689. addProofInput.checked = profileOptions.addProof;
  690. woleetKeyInput.value = profileOptions.woleetKey;
  691. woleetKeyInput.disabled = !profileOptions.addProof;
  692. saveToGDriveInput.checked = profileOptions.saveToGDrive;
  693. saveToGitHubInput.checked = profileOptions.saveToGitHub;
  694. githubTokenInput.value = profileOptions.githubToken;
  695. githubTokenInput.disabled = !profileOptions.saveToGitHub;
  696. githubUserInput.value = profileOptions.githubUser;
  697. githubUserInput.disabled = !profileOptions.saveToGitHub;
  698. githubRepositoryInput.value = profileOptions.githubRepository;
  699. githubRepositoryInput.disabled = !profileOptions.saveToGitHub;
  700. githubBranchInput.value = profileOptions.githubBranch;
  701. githubBranchInput.disabled = !profileOptions.saveToGitHub;
  702. saveWithCompanionInput.checked = profileOptions.saveWithCompanion;
  703. saveToFilesystemInput.checked = !profileOptions.saveToGDrive && !profileOptions.saveToGitHub && !profileOptions.saveWithCompanion && !saveToClipboardInput.checked;
  704. compressHTMLInput.checked = profileOptions.compressHTML;
  705. compressCSSInput.checked = profileOptions.compressCSS;
  706. loadDeferredImagesInput.checked = profileOptions.loadDeferredImages;
  707. loadDeferredImagesMaxIdleTimeInput.value = profileOptions.loadDeferredImagesMaxIdleTime;
  708. loadDeferredImagesKeepZoomLevelInput.checked = profileOptions.loadDeferredImagesKeepZoomLevel;
  709. loadDeferredImagesKeepZoomLevelInput.disabled = !profileOptions.loadDeferredImages;
  710. loadDeferredImagesMaxIdleTimeInput.disabled = !profileOptions.loadDeferredImages;
  711. contextMenuEnabledInput.checked = profileOptions.contextMenuEnabled;
  712. filenameTemplateInput.value = profileOptions.filenameTemplate;
  713. filenameMaxLengthInput.value = profileOptions.filenameMaxLength;
  714. shadowEnabledInput.checked = profileOptions.shadowEnabled;
  715. maxResourceSizeEnabledInput.checked = profileOptions.maxResourceSizeEnabled;
  716. maxResourceSizeInput.value = profileOptions.maxResourceSize;
  717. maxResourceSizeInput.disabled = !profileOptions.maxResourceSizeEnabled;
  718. confirmFilenameInput.checked = profileOptions.confirmFilename;
  719. filenameConflictActionInput.value = profileOptions.filenameConflictAction;
  720. removeAudioSrcInput.checked = profileOptions.removeAudioSrc;
  721. removeVideoSrcInput.checked = profileOptions.removeVideoSrc;
  722. displayInfobarInput.checked = profileOptions.displayInfobar;
  723. displayStatsInput.checked = profileOptions.displayStats;
  724. backgroundSaveInput.checked = profileOptions.backgroundSave;
  725. autoSaveDelayInput.value = profileOptions.autoSaveDelay;
  726. autoSaveLoadInput.checked = !profileOptions.autoSaveLoadOrUnload && profileOptions.autoSaveLoad;
  727. autoSaveLoadOrUnloadInput.checked = profileOptions.autoSaveLoadOrUnload;
  728. autoSaveUnloadInput.checked = !profileOptions.autoSaveLoadOrUnload && profileOptions.autoSaveUnload;
  729. autoSaveLoadInput.disabled = profileOptions.autoSaveLoadOrUnload;
  730. autoSaveUnloadInput.disabled = profileOptions.autoSaveLoadOrUnload;
  731. autoSaveDiscardInput.checked = profileOptions.autoSaveDiscard;
  732. autoSaveRemoveInput.checked = profileOptions.autoSaveRemove;
  733. autoSaveRepeatInput.checked = profileOptions.autoSaveRepeat;
  734. autoSaveRepeatDelayInput.value = profileOptions.autoSaveRepeatDelay;
  735. autoSaveRepeatDelayInput.disabled = !profileOptions.autoSaveRepeat;
  736. autoSaveExternalSaveInput.checked = profileOptions.autoSaveExternalSave;
  737. autoSaveExternalSaveInput.parentElement.hidden = !companionState.enabled;
  738. removeAlternativeFontsInput.checked = profileOptions.removeAlternativeFonts;
  739. removeAlternativeImagesInput.checked = profileOptions.removeAlternativeImages;
  740. groupDuplicateImagesInput.checked = profileOptions.groupDuplicateImages;
  741. removeAlternativeMediasInput.checked = profileOptions.removeAlternativeMedias;
  742. saveCreatedBookmarksInput.checked = profileOptions.saveCreatedBookmarks;
  743. passReferrerOnErrorInput.checked = profileOptions.passReferrerOnError;
  744. replaceBookmarkURLInput.checked = profileOptions.replaceBookmarkURL;
  745. replaceBookmarkURLInput.disabled = !profileOptions.saveCreatedBookmarks;
  746. allowedBookmarkFoldersInput.value = profileOptions.allowedBookmarkFolders.map(folder => folder.replace(/,/g, "\\,")).join(","); // eslint-disable-line no-useless-escape
  747. allowedBookmarkFoldersInput.disabled = !profileOptions.saveCreatedBookmarks;
  748. ignoredBookmarkFoldersInput.value = profileOptions.ignoredBookmarkFolders.map(folder => folder.replace(/,/g, "\\,")).join(","); // eslint-disable-line no-useless-escape
  749. ignoredBookmarkFoldersInput.disabled = !profileOptions.saveCreatedBookmarks;
  750. infobarTemplateInput.value = profileOptions.infobarTemplate;
  751. includeInfobarInput.checked = profileOptions.includeInfobar;
  752. confirmInfobarInput.checked = profileOptions.confirmInfobarContent;
  753. autoCloseInput.checked = profileOptions.autoClose;
  754. openEditorInput.checked = profileOptions.openEditor;
  755. openSavedPageInput.checked = profileOptions.openSavedPage;
  756. autoOpenEditorInput.checked = profileOptions.autoOpenEditor;
  757. defaultEditorModeInput.value = profileOptions.defaultEditorMode;
  758. applySystemThemeInput.checked = profileOptions.applySystemTheme;
  759. warnUnsavedPageInput.checked = profileOptions.warnUnsavedPage;
  760. }
  761. function getProfileText(profileName) {
  762. return profileName == DEFAULT_PROFILE_NAME ? browser.i18n.getMessage("profileDefaultSettings") : profileName == DISABLED_PROFILE_NAME ? browser.i18n.getMessage("profileDisabled") : profileName;
  763. }
  764. async function update() {
  765. try {
  766. await pendingSave;
  767. } catch (error) {
  768. // ignored
  769. }
  770. pendingSave = browser.runtime.sendMessage({
  771. method: "config.updateProfile",
  772. profileName: profileNamesInput.value,
  773. profile: {
  774. removeHiddenElements: removeHiddenElementsInput.checked,
  775. removeUnusedStyles: removeUnusedStylesInput.checked,
  776. removeUnusedFonts: removeUnusedFontsInput.checked,
  777. removeFrames: removeFramesInput.checked,
  778. removeImports: removeImportsInput.checked,
  779. removeScripts: removeScriptsInput.checked,
  780. saveRawPage: saveRawPageInput.checked,
  781. insertMetaCSP: insertMetaCSPInput.checked,
  782. saveToClipboard: saveToClipboardInput.checked,
  783. addProof: addProofInput.checked,
  784. woleetKey: woleetKeyInput.value,
  785. saveToGDrive: saveToGDriveInput.checked,
  786. saveToGitHub: saveToGitHubInput.checked,
  787. githubToken: githubTokenInput.value,
  788. githubUser: githubUserInput.value,
  789. githubRepository: githubRepositoryInput.value,
  790. githubBranch: githubBranchInput.value,
  791. saveWithCompanion: saveWithCompanionInput.checked,
  792. compressHTML: compressHTMLInput.checked,
  793. compressCSS: compressCSSInput.checked,
  794. loadDeferredImages: loadDeferredImagesInput.checked,
  795. loadDeferredImagesMaxIdleTime: Math.max(loadDeferredImagesMaxIdleTimeInput.value, 0),
  796. loadDeferredImagesKeepZoomLevel: loadDeferredImagesKeepZoomLevelInput.checked,
  797. contextMenuEnabled: contextMenuEnabledInput.checked,
  798. filenameTemplate: filenameTemplateInput.value,
  799. filenameMaxLength: filenameMaxLengthInput.value,
  800. shadowEnabled: shadowEnabledInput.checked,
  801. maxResourceSizeEnabled: maxResourceSizeEnabledInput.checked,
  802. maxResourceSize: Math.max(maxResourceSizeInput.value, 0),
  803. confirmFilename: confirmFilenameInput.checked,
  804. filenameConflictAction: filenameConflictActionInput.value,
  805. removeAudioSrc: removeAudioSrcInput.checked,
  806. removeVideoSrc: removeVideoSrcInput.checked,
  807. displayInfobar: displayInfobarInput.checked,
  808. displayStats: displayStatsInput.checked,
  809. backgroundSave: backgroundSaveInput.checked,
  810. autoSaveDelay: Math.max(autoSaveDelayInput.value, 0),
  811. autoSaveLoad: autoSaveLoadInput.checked,
  812. autoSaveUnload: autoSaveUnloadInput.checked,
  813. autoSaveDiscard: autoSaveDiscardInput.checked,
  814. autoSaveRemove: autoSaveRemoveInput.checked,
  815. autoSaveLoadOrUnload: autoSaveLoadOrUnloadInput.checked,
  816. autoSaveRepeat: autoSaveRepeatInput.checked,
  817. autoSaveRepeatDelay: Math.max(autoSaveRepeatDelayInput.value, 1),
  818. autoSaveExternalSave: autoSaveExternalSaveInput.checked,
  819. removeAlternativeFonts: removeAlternativeFontsInput.checked,
  820. removeAlternativeImages: removeAlternativeImagesInput.checked,
  821. removeAlternativeMedias: removeAlternativeMediasInput.checked,
  822. saveCreatedBookmarks: saveCreatedBookmarksInput.checked,
  823. passReferrerOnError: passReferrerOnErrorInput.checked,
  824. replaceBookmarkURL: replaceBookmarkURLInput.checked,
  825. allowedBookmarkFolders: allowedBookmarkFoldersInput.value.replace(/([^\\]),/g, "$1 ,").split(/[^\\],/).map(folder => folder.replace(/\\,/g, ",")),
  826. ignoredBookmarkFolders: ignoredBookmarkFoldersInput.value.replace(/([^\\]),/g, "$1 ,").split(/[^\\],/).map(folder => folder.replace(/\\,/g, ",")),
  827. groupDuplicateImages: groupDuplicateImagesInput.checked,
  828. infobarTemplate: infobarTemplateInput.value,
  829. includeInfobar: includeInfobarInput.checked,
  830. confirmInfobarContent: confirmInfobarInput.checked,
  831. autoClose: autoCloseInput.checked,
  832. openEditor: openEditorInput.checked,
  833. openSavedPage: openSavedPageInput.checked,
  834. autoOpenEditor: autoOpenEditorInput.checked,
  835. defaultEditorMode: defaultEditorModeInput.value,
  836. applySystemTheme: applySystemThemeInput.checked,
  837. warnUnsavedPage: warnUnsavedPageInput.checked
  838. }
  839. });
  840. try {
  841. await pendingSave;
  842. } catch (error) {
  843. // ignored
  844. }
  845. }
  846. async function refreshExternalComponents() {
  847. try {
  848. await browser.runtime.sendMessage({ method: "ui.refreshMenu" });
  849. if (sidePanelDisplay) {
  850. await browser.runtime.sendMessage({ method: "options.refresh", profileName: profileNamesInput.value });
  851. } else {
  852. await browser.runtime.sendMessage({ method: "options.refreshPanel", profileName: profileNamesInput.value });
  853. }
  854. } catch (error) {
  855. // ignored
  856. }
  857. }
  858. async function saveCreatedBookmarks() {
  859. if (saveCreatedBookmarksInput.checked) {
  860. saveCreatedBookmarksInput.checked = false;
  861. try {
  862. const permissionGranted = await browser.permissions.request({ permissions: ["bookmarks"] });
  863. if (permissionGranted) {
  864. saveCreatedBookmarksInput.checked = true;
  865. await update();
  866. await refresh();
  867. await browser.runtime.sendMessage({ method: "bookmarks.saveCreatedBookmarks" });
  868. } else {
  869. await disableOption();
  870. }
  871. } catch (error) {
  872. saveCreatedBookmarksInput.checked = false;
  873. await disableOption();
  874. }
  875. } else {
  876. await disableOption();
  877. }
  878. async function disableOption() {
  879. await update();
  880. await refresh();
  881. await browser.runtime.sendMessage({ method: "bookmarks.disable" });
  882. }
  883. }
  884. async function passReferrerOnError() {
  885. if (passReferrerOnErrorInput.checked) {
  886. passReferrerOnErrorInput.checked = false;
  887. try {
  888. const permissionGranted = await browser.permissions.request({ permissions: ["webRequest", "webRequestBlocking"] });
  889. if (permissionGranted) {
  890. passReferrerOnErrorInput.checked = true;
  891. await update();
  892. await refresh();
  893. await browser.runtime.sendMessage({ method: "requests.enableReferrerOnError" });
  894. } else {
  895. await disableOption();
  896. }
  897. } catch (error) {
  898. await disableOption();
  899. }
  900. } else {
  901. await disableOption();
  902. }
  903. async function disableOption() {
  904. await update();
  905. await refresh();
  906. await browser.runtime.sendMessage({ method: "requests.disableReferrerOnError" });
  907. await browser.permissions.remove({ permissions: ["webRequest", "webRequestBlocking"] });
  908. }
  909. }
  910. async function enableExternalSave(input) {
  911. if (input.checked) {
  912. input.checked = false;
  913. try {
  914. const permissionGranted = await browser.permissions.request({ permissions: ["nativeMessaging"] });
  915. if (permissionGranted) {
  916. input.checked = true;
  917. await refreshOption();
  918. if (window.chrome) {
  919. window.chrome.runtime.reload();
  920. location.reload();
  921. }
  922. } else {
  923. await refreshOption();
  924. }
  925. } catch (error) {
  926. input.checked = true;
  927. await refreshOption();
  928. }
  929. } else {
  930. await refreshOption();
  931. }
  932. async function refreshOption() {
  933. await update();
  934. await refresh();
  935. }
  936. }
  937. async function confirm(message, positionY) {
  938. document.getElementById("confirmLabel").textContent = message;
  939. document.getElementById("formConfirmContainer").style.setProperty("display", "flex");
  940. document.querySelector("#formConfirmContainer .popup-content").style.setProperty("margin-top", positionY + "px");
  941. confirmButton.focus();
  942. document.body.style.setProperty("overflow-y", "hidden");
  943. return new Promise(resolve => {
  944. confirmButton.onclick = event => hideAndResolve(event, true);
  945. cancelButton.onclick = event => hideAndResolve(event);
  946. window.onkeyup = event => {
  947. if (event.key == "Escape") {
  948. hideAndResolve(event);
  949. }
  950. };
  951. function hideAndResolve(event, value) {
  952. event.preventDefault();
  953. document.getElementById("formConfirmContainer").style.setProperty("display", "none");
  954. document.body.style.setProperty("overflow-y", "");
  955. resolve(value);
  956. }
  957. });
  958. }
  959. async function reset(positionY) {
  960. document.getElementById("formResetContainer").style.setProperty("display", "flex");
  961. document.querySelector("#formResetContainer .popup-content").style.setProperty("margin-top", positionY + "px");
  962. resetCancelButton.focus();
  963. document.body.style.setProperty("overflow-y", "hidden");
  964. return new Promise(resolve => {
  965. resetAllButton.onclick = event => hideAndResolve(event, "all");
  966. resetCurrentButton.onclick = event => hideAndResolve(event, "current");
  967. resetCancelButton.onclick = event => hideAndResolve(event);
  968. window.onkeyup = event => {
  969. if (event.key == "Escape") {
  970. hideAndResolve(event);
  971. }
  972. };
  973. function hideAndResolve(event, value) {
  974. event.preventDefault();
  975. document.getElementById("formResetContainer").style.setProperty("display", "none");
  976. document.body.style.setProperty("overflow-y", "");
  977. resolve(value);
  978. }
  979. });
  980. }
  981. async function prompt(message, positionY, defaultValue = "") {
  982. document.getElementById("promptLabel").textContent = message;
  983. document.getElementById("formPromptContainer").style.setProperty("display", "flex");
  984. document.querySelector("#formPromptContainer .popup-content").style.setProperty("margin-top", positionY + "px");
  985. promptInput.value = defaultValue;
  986. promptInput.focus();
  987. document.body.style.setProperty("overflow-y", "hidden");
  988. return new Promise(resolve => {
  989. promptConfirmButton.onclick = event => hideAndResolve(event, promptInput.value);
  990. promptCancelButton.onclick = event => hideAndResolve(event);
  991. window.onkeyup = event => {
  992. if (event.key == "Escape") {
  993. hideAndResolve(event);
  994. }
  995. };
  996. function hideAndResolve(event, value) {
  997. event.preventDefault();
  998. document.getElementById("formPromptContainer").style.setProperty("display", "none");
  999. document.body.style.setProperty("overflow-y", "");
  1000. resolve(value);
  1001. }
  1002. });
  1003. }
  1004. async function getHelpContents() {
  1005. const helpPage = await fetch(browser.runtime.getURL(HELP_PAGE_PATH));
  1006. const content = new TextDecoder().decode(await helpPage.arrayBuffer());
  1007. const doc = (new DOMParser()).parseFromString(content, "text/html");
  1008. const items = doc.querySelectorAll("[data-options-label]");
  1009. items.forEach(itemElement => {
  1010. const optionLabel = document.getElementById(itemElement.dataset.optionsLabel);
  1011. const helpIconWrapper = document.createElement("span");
  1012. const helpIconContainer = document.createElement("span");
  1013. const helpIcon = document.createElement("img");
  1014. helpIcon.src = HELP_ICON_URL;
  1015. helpIconWrapper.className = "help-icon-wrapper";
  1016. const labelWords = optionLabel.textContent.split(/\s+/);
  1017. if (labelWords.length > 1) {
  1018. helpIconWrapper.textContent = labelWords.pop();
  1019. optionLabel.textContent = labelWords.join(" ") + " ";
  1020. }
  1021. helpIconContainer.className = "help-icon";
  1022. helpIconContainer.onclick = () => {
  1023. helpContent.hidden = !helpContent.hidden;
  1024. return false;
  1025. };
  1026. helpIcon.tabIndex = 0;
  1027. helpIconContainer.onkeyup = event => {
  1028. if (event.code == "Enter") {
  1029. helpContent.hidden = !helpContent.hidden;
  1030. return false;
  1031. }
  1032. };
  1033. helpIconContainer.appendChild(helpIcon);
  1034. helpIconWrapper.appendChild(helpIconContainer);
  1035. optionLabel.appendChild(helpIconWrapper);
  1036. const helpContent = document.createElement("div");
  1037. helpContent.hidden = true;
  1038. helpContent.className = "help-content";
  1039. itemElement.childNodes.forEach(node => {
  1040. if (node instanceof HTMLElement && node.className != "option") {
  1041. helpContent.appendChild(document.importNode(node, true));
  1042. }
  1043. });
  1044. helpContent.querySelectorAll("a[href]").forEach(linkElement => {
  1045. const hrefValue = linkElement.getAttribute("href");
  1046. if (hrefValue.startsWith("#")) {
  1047. linkElement.href = browser.runtime.getURL(HELP_PAGE_PATH + linkElement.getAttribute("href"));
  1048. linkElement.target = "_blank";
  1049. }
  1050. });
  1051. optionLabel.parentElement.insertAdjacentElement("afterEnd", helpContent);
  1052. });
  1053. }
  1054. function getLocalStorageItem(key) {
  1055. try {
  1056. return localStorage.getItem(key);
  1057. } catch (error) {
  1058. // ignored
  1059. }
  1060. }
  1061. function setLocalStorageItem(key, value) {
  1062. try {
  1063. return localStorage.setItem(key, value);
  1064. } catch (error) {
  1065. // ignored
  1066. }
  1067. }
  1068. function removeLocalStorageItem(key) {
  1069. try {
  1070. return localStorage.removeItem(key);
  1071. } catch (error) {
  1072. // ignored
  1073. }
  1074. }