ui-options.js 58 KB

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