ui-options.js 47 KB

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