ui-options.js 48 KB

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