ui-options.js 45 KB

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