ui-options.js 46 KB

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