ui-options.js 58 KB

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