ui-options.js 48 KB

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