ui-options.js 49 KB

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