ui-options.js 44 KB

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