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. getHelpContents();
  177. ruleProfileInput.onchange = () => {
  178. if (!autoSaveProfileChanged && ruleProfileInput.value != CURRENT_PROFILE_NAME) {
  179. ruleAutoSaveProfileInput.value = ruleProfileInput.value;
  180. }
  181. };
  182. ruleAutoSaveProfileInput.onchange = () => {
  183. autoSaveProfileChanged = true;
  184. };
  185. rulesDeleteAllButton.addEventListener("click", async () => {
  186. if (await confirm(browser.i18n.getMessage("optionsDeleteDisplayedRulesConfirm"), "flex-end")) {
  187. await browser.runtime.sendMessage({ method: "config.deleteRules", profileName: !showAllProfilesInput.checked && profileNamesInput.value });
  188. await refresh();
  189. await refreshExternalComponents();
  190. }
  191. }, false);
  192. createURLElement.onsubmit = async event => {
  193. event.preventDefault();
  194. try {
  195. await browser.runtime.sendMessage({ method: "config.addRule", url: ruleUrlInput.value, profileName: ruleProfileInput.value, autoSaveProfileName: ruleAutoSaveProfileInput.value });
  196. } catch (error) {
  197. // ignored
  198. }
  199. ruleUrlInput.value = "";
  200. ruleProfileInput.value = ruleAutoSaveProfileInput.value = DEFAULT_PROFILE_NAME;
  201. autoSaveProfileChanged = false;
  202. await refresh();
  203. await refreshExternalComponents();
  204. ruleUrlInput.focus();
  205. };
  206. ruleUrlInput.onclick = ruleUrlInput.onkeyup = ruleUrlInput.onchange = async () => {
  207. ruleAddButton.disabled = !ruleUrlInput.value;
  208. const rules = await browser.runtime.sendMessage({ method: "config.getRules" });
  209. if (rules.find(rule => rule.url == ruleUrlInput.value)) {
  210. ruleAddButton.disabled = true;
  211. }
  212. };
  213. ruleEditUrlInput.onclick = ruleEditUrlInput.onkeyup = ruleEditUrlInput.onchange = async () => {
  214. ruleEditButton.disabled = !ruleEditUrlInput.value;
  215. const rules = await browser.runtime.sendMessage({ method: "config.getRules" });
  216. if (rules.find(rule => rule.url == ruleEditUrlInput.value)) {
  217. ruleEditButton.disabled = true;
  218. }
  219. };
  220. if (getLocalStorageItem("optionShowAutoSaveProfile")) {
  221. showAutoSaveProfileInput.checked = true;
  222. rulesContainerElement.classList.remove("compact");
  223. }
  224. showAutoSaveProfileInput.addEventListener("click", () => {
  225. if (showAutoSaveProfileInput.checked) {
  226. setLocalStorageItem("optionShowAutoSaveProfile", 1);
  227. rulesContainerElement.classList.remove("compact");
  228. } else {
  229. removeLocalStorageItem("optionShowAutoSaveProfile");
  230. rulesContainerElement.classList.add("compact");
  231. }
  232. }, false);
  233. if (getLocalStorageItem("optionShowAllProfiles")) {
  234. showAllProfilesInput.checked = true;
  235. }
  236. showAllProfilesInput.addEventListener("click", () => {
  237. if (showAllProfilesInput.checked) {
  238. setLocalStorageItem("optionShowAllProfiles", 1);
  239. } else {
  240. removeLocalStorageItem("optionShowAllProfiles");
  241. }
  242. }, false);
  243. addProfileButton.addEventListener("click", async () => {
  244. const profileName = await prompt(browser.i18n.getMessage("profileAddPrompt"), "flex-start");
  245. if (profileName) {
  246. try {
  247. await browser.runtime.sendMessage({ method: "config.createProfile", profileName, fromProfileName: profileNamesInput.value });
  248. } catch (error) {
  249. // ignored
  250. }
  251. if (sidePanelDisplay) {
  252. await refresh();
  253. } else {
  254. await refresh(profileName);
  255. }
  256. await refreshExternalComponents();
  257. }
  258. }, false);
  259. deleteProfileButton.addEventListener("click", async () => {
  260. if (await confirm(browser.i18n.getMessage("profileDeleteConfirm"), "flex-start")) {
  261. try {
  262. await browser.runtime.sendMessage({ method: "config.deleteProfile", profileName: profileNamesInput.value });
  263. } catch (error) {
  264. // ignored
  265. }
  266. profileNamesInput.value = null;
  267. await refresh();
  268. await refreshExternalComponents();
  269. }
  270. }, false);
  271. renameProfileButton.addEventListener("click", async () => {
  272. const profileName = await prompt(browser.i18n.getMessage("profileRenamePrompt"), "flex-start", profileNamesInput.value);
  273. if (profileName) {
  274. try {
  275. await browser.runtime.sendMessage({ method: "config.renameProfile", profileName: profileNamesInput.value, newProfileName: profileName });
  276. } catch (error) {
  277. // ignored
  278. }
  279. await refresh(profileName);
  280. await refreshExternalComponents();
  281. }
  282. }, false);
  283. resetButton.addEventListener("click", async () => {
  284. const choice = await reset();
  285. if (choice) {
  286. if (choice == "all") {
  287. await browser.runtime.sendMessage({ method: "config.resetProfiles" });
  288. await refresh(DEFAULT_PROFILE_NAME);
  289. await refreshExternalComponents();
  290. }
  291. if (choice == "current") {
  292. await browser.runtime.sendMessage({ method: "config.resetProfile", profileName: profileNamesInput.value });
  293. await refresh();
  294. await refreshExternalComponents();
  295. }
  296. await update();
  297. }
  298. }, false);
  299. exportButton.addEventListener("click", async () => {
  300. await browser.runtime.sendMessage({ method: "config.exportConfig" });
  301. }, false);
  302. importButton.addEventListener("click", () => {
  303. fileInput.onchange = async () => {
  304. if (fileInput.files.length) {
  305. const reader = new FileReader();
  306. reader.readAsText(fileInput.files[0]);
  307. const serializedConfig = await new Promise((resolve, reject) => {
  308. reader.addEventListener("load", () => resolve(reader.result), false);
  309. reader.addEventListener("error", reject, false);
  310. });
  311. const config = JSON.parse(serializedConfig);
  312. await browser.runtime.sendMessage({ method: "config.importConfig", config });
  313. await refresh(DEFAULT_PROFILE_NAME);
  314. await refreshExternalComponents();
  315. fileInput.value = "";
  316. }
  317. };
  318. fileInput.click();
  319. }, false);
  320. autoSaveUnloadInput.addEventListener("click", async () => {
  321. if (!autoSaveLoadInput.checked && !autoSaveUnloadInput.checked) {
  322. autoSaveLoadOrUnloadInput.checked = true;
  323. }
  324. }, false);
  325. autoSaveLoadInput.addEventListener("click", async () => {
  326. if (!autoSaveLoadInput.checked && !autoSaveUnloadInput.checked) {
  327. autoSaveLoadOrUnloadInput.checked = true;
  328. }
  329. }, false);
  330. autoSaveLoadOrUnloadInput.addEventListener("click", async () => {
  331. if (autoSaveLoadOrUnloadInput.checked) {
  332. autoSaveUnloadInput.checked = autoSaveLoadInput.checked = false;
  333. } else {
  334. autoSaveUnloadInput.checked = false;
  335. autoSaveLoadInput.checked = true;
  336. }
  337. }, false);
  338. expandAllButton.addEventListener("click", () => {
  339. if (expandAllButton.className) {
  340. expandAllButton.className = "";
  341. } else {
  342. expandAllButton.className = "opened";
  343. }
  344. document.querySelectorAll("details").forEach(detailElement => detailElement.open = Boolean(expandAllButton.className));
  345. }, false);
  346. removeScriptsInput.addEventListener("click", () => {
  347. if (!removeScriptsInput.checked) {
  348. removeHiddenElementsInput.checked = false;
  349. removeUnusedStylesInput.checked = false;
  350. }
  351. }, false);
  352. saveToGDriveInput.addEventListener("click", async () => {
  353. if (!saveToGDriveInput.checked) {
  354. await browser.runtime.sendMessage({ method: "downloads.disableGDrive" });
  355. }
  356. }, false);
  357. synchronizeInput.checked = (await browser.runtime.sendMessage({ method: "config.isSync" })).sync;
  358. synchronizeInput.addEventListener("click", async () => {
  359. if (synchronizeInput.checked) {
  360. await browser.runtime.sendMessage({ method: "config.enableSync" });
  361. await refresh(DEFAULT_PROFILE_NAME);
  362. } else {
  363. await browser.runtime.sendMessage({ method: "config.disableSync" });
  364. await refresh();
  365. }
  366. }, false);
  367. document.body.onchange = async event => {
  368. let target = event.target;
  369. if (target != ruleUrlInput && target != ruleProfileInput && target != ruleAutoSaveProfileInput && target != ruleEditUrlInput && target != ruleEditProfileInput && target != ruleEditAutoSaveProfileInput && target != showAutoSaveProfileInput) {
  370. if (target != profileNamesInput && target != showAllProfilesInput) {
  371. await update();
  372. }
  373. if (target == profileNamesInput) {
  374. await refresh(profileNamesInput.value);
  375. if (sidePanelDisplay) {
  376. const tabsData = await browser.runtime.sendMessage({ method: "tabsData.get" });
  377. tabsData.profileName = profileNamesInput.value;
  378. await browser.runtime.sendMessage({ method: "tabsData.set", tabsData: tabsData });
  379. await browser.runtime.sendMessage({ method: "ui.refreshMenu" });
  380. }
  381. } else {
  382. if (target == contextMenuEnabledInput) {
  383. await browser.runtime.sendMessage({ method: "ui.refreshMenu" });
  384. }
  385. if (target == openEditorInput) {
  386. await browser.runtime.sendMessage({ method: "ui.refreshMenu" });
  387. }
  388. await refresh();
  389. }
  390. }
  391. };
  392. addProfileButton.title = browser.i18n.getMessage("profileAddButtonTooltip");
  393. deleteProfileButton.title = browser.i18n.getMessage("profileDeleteButtonTooltip");
  394. renameProfileButton.title = browser.i18n.getMessage("profileRenameButtonTooltip");
  395. removeHiddenElementsLabel.textContent = browser.i18n.getMessage("optionRemoveHiddenElements");
  396. removeUnusedStylesLabel.textContent = browser.i18n.getMessage("optionRemoveUnusedStyles");
  397. removeUnusedFontsLabel.textContent = browser.i18n.getMessage("optionRemoveUnusedFonts");
  398. removeFramesLabel.textContent = browser.i18n.getMessage("optionRemoveFrames");
  399. removeImportsLabel.textContent = browser.i18n.getMessage("optionRemoveImports");
  400. removeScriptsLabel.textContent = browser.i18n.getMessage("optionRemoveScripts");
  401. saveRawPageLabel.textContent = browser.i18n.getMessage("optionSaveRawPage");
  402. saveToClipboardLabel.textContent = browser.i18n.getMessage("optionSaveToClipboard");
  403. saveToGDriveLabel.textContent = browser.i18n.getMessage("optionSaveToGDrive");
  404. compressHTMLLabel.textContent = browser.i18n.getMessage("optionCompressHTML");
  405. compressCSSLabel.textContent = browser.i18n.getMessage("optionCompressCSS");
  406. loadDeferredImagesLabel.textContent = browser.i18n.getMessage("optionLoadDeferredImages");
  407. loadDeferredImagesMaxIdleTimeLabel.textContent = browser.i18n.getMessage("optionLoadDeferredImagesMaxIdleTime");
  408. addMenuEntryLabel.textContent = browser.i18n.getMessage("optionAddMenuEntry");
  409. filenameTemplateLabel.textContent = browser.i18n.getMessage("optionFilenameTemplate");
  410. shadowEnabledLabel.textContent = browser.i18n.getMessage("optionDisplayShadow");
  411. setMaxResourceSizeLabel.textContent = browser.i18n.getMessage("optionSetMaxResourceSize");
  412. maxResourceSizeLabel.textContent = browser.i18n.getMessage("optionMaxResourceSize");
  413. confirmFilenameLabel.textContent = browser.i18n.getMessage("optionConfirmFilename");
  414. filenameConflictActionLabel.textContent = browser.i18n.getMessage("optionFilenameConflictAction");
  415. filenameConflictActionUniquifyLabel.textContent = browser.i18n.getMessage("optionFilenameConflictActionUniquify");
  416. filenameConflictActionOverwriteLabel.textContent = browser.i18n.getMessage("optionFilenameConflictActionOverwrite");
  417. filenameConflictActionPromptLabel.textContent = browser.i18n.getMessage("optionFilenameConflictActionPrompt");
  418. removeAudioLabel.textContent = browser.i18n.getMessage("optionRemoveAudio");
  419. removeVideoLabel.textContent = browser.i18n.getMessage("optionRemoveVideo");
  420. displayInfobarLabel.textContent = browser.i18n.getMessage("optionDisplayInfobar");
  421. displayStatsLabel.textContent = browser.i18n.getMessage("optionDisplayStats");
  422. backgroundSaveLabel.textContent = browser.i18n.getMessage("optionBackgroundSave");
  423. autoSaveDelayLabel.textContent = browser.i18n.getMessage("optionAutoSaveDelay");
  424. autoSaveLoadLabel.textContent = browser.i18n.getMessage("optionAutoSaveLoad");
  425. autoSaveUnloadLabel.textContent = browser.i18n.getMessage("optionAutoSaveUnload");
  426. autoSaveLoadOrUnloadLabel.textContent = browser.i18n.getMessage("optionAutoSaveLoadOrUnload");
  427. autoSaveRepeatLabel.textContent = browser.i18n.getMessage("optionAutoSaveRepeat");
  428. autoSaveRepeatDelayLabel.textContent = browser.i18n.getMessage("optionAutoSaveRepeatDelay");
  429. removeAlternativeFontsLabel.textContent = browser.i18n.getMessage("optionRemoveAlternativeFonts");
  430. removeAlternativeImagesLabel.textContent = browser.i18n.getMessage("optionRemoveAlternativeImages");
  431. removeAlternativeMediasLabel.textContent = browser.i18n.getMessage("optionRemoveAlternativeMedias");
  432. groupDuplicateImagesLabel.textContent = browser.i18n.getMessage("optionGroupDuplicateImages");
  433. titleLabel.textContent = browser.i18n.getMessage("optionsTitle");
  434. userInterfaceLabel.textContent = browser.i18n.getMessage("optionsUserInterfaceSubTitle");
  435. filenameLabel.textContent = browser.i18n.getMessage("optionsFileNameSubTitle");
  436. htmlContentLabel.textContent = browser.i18n.getMessage("optionsHTMLContentSubTitle");
  437. imagesLabel.textContent = browser.i18n.getMessage("optionsImagesSubTitle");
  438. stylesheetsLabel.textContent = browser.i18n.getMessage("optionsStylesheetsSubTitle");
  439. fontsLabel.textContent = browser.i18n.getMessage("optionsFontsSubTitle");
  440. otherResourcesLabel.textContent = browser.i18n.getMessage("optionsOtherResourcesSubTitle");
  441. autoSaveLabel.textContent = browser.i18n.getMessage("optionsAutoSaveSubTitle");
  442. miscLabel.textContent = browser.i18n.getMessage("optionsMiscSubTitle");
  443. helpLabel.textContent = browser.i18n.getMessage("optionsHelpLink");
  444. infobarTemplateLabel.textContent = browser.i18n.getMessage("optionInfobarTemplate");
  445. includeInfobarLabel.textContent = browser.i18n.getMessage("optionIncludeInfobar");
  446. confirmInfobarLabel.textContent = browser.i18n.getMessage("optionConfirmInfobar");
  447. autoCloseLabel.textContent = browser.i18n.getMessage("optionAutoClose");
  448. openEditorLabel.textContent = browser.i18n.getMessage("optionOpenEditor");
  449. autoOpenEditorLabel.textContent = browser.i18n.getMessage("optionAutoOpenEditor");
  450. resetButton.textContent = browser.i18n.getMessage("optionsResetButton");
  451. exportButton.textContent = browser.i18n.getMessage("optionsExportButton");
  452. importButton.textContent = browser.i18n.getMessage("optionsImportButton");
  453. resetButton.title = browser.i18n.getMessage("optionsResetTooltip");
  454. autoSettingsLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsSubTitle");
  455. autoSettingsUrlLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsUrl");
  456. autoSettingsProfileLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsProfile");
  457. autoSettingsAutoSaveProfileLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsAutoSaveProfile");
  458. ruleAddButton.title = browser.i18n.getMessage("optionsAddRuleTooltip");
  459. ruleEditButton.title = browser.i18n.getMessage("optionsValidateChangesTooltip");
  460. rulesDeleteAllButton.title = browser.i18n.getMessage("optionsDeleteRulesTooltip");
  461. showAllProfilesLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsShowAllProfiles");
  462. showAutoSaveProfileLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsShowAutoSaveProfile");
  463. ruleUrlInput.placeholder = ruleEditUrlInput.placeholder = browser.i18n.getMessage("optionsAutoSettingsUrlPlaceholder");
  464. synchronizeLabel.textContent = browser.i18n.getMessage("optionSynchronize");
  465. resetAllButton.textContent = browser.i18n.getMessage("optionsResetAllButton");
  466. resetCurrentButton.textContent = browser.i18n.getMessage("optionsResetCurrentButton");
  467. resetCancelButton.textContent = promptCancelButton.textContent = cancelButton.textContent = browser.i18n.getMessage("optionsCancelButton");
  468. confirmButton.textContent = promptConfirmButton.textContent = browser.i18n.getMessage("optionsOKButton");
  469. document.getElementById("resetConfirmLabel").textContent = browser.i18n.getMessage("optionsResetConfirm");
  470. if (location.href.endsWith("#")) {
  471. document.querySelector(".new-window-link").remove();
  472. document.documentElement.classList.add("maximized");
  473. }
  474. const tabsData = await browser.runtime.sendMessage({ method: "tabsData.get" });
  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. })();