ui-options.js 57 KB

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