ui-options.js 44 KB

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