ui-options.js 43 KB

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