ui-options.js 44 KB

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