ui-options.js 45 KB

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