ui-options.js 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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. addProofInput.addEventListener("click", async () => {
  359. if (addProofInput.checked) {
  360. addProofInput.checked = false;
  361. if (await confirm(browser.i18n.getMessage("optionsAddProofConfirm"))) {
  362. addProofInput.checked = true;
  363. }
  364. await update();
  365. }
  366. });
  367. synchronizeInput.checked = (await browser.runtime.sendMessage({ method: "config.isSync" })).sync;
  368. synchronizeInput.addEventListener("click", async () => {
  369. if (synchronizeInput.checked) {
  370. await browser.runtime.sendMessage({ method: "config.enableSync" });
  371. await refresh(DEFAULT_PROFILE_NAME);
  372. } else {
  373. await browser.runtime.sendMessage({ method: "config.disableSync" });
  374. await refresh();
  375. }
  376. }, false);
  377. document.body.onchange = async event => {
  378. let target = event.target;
  379. if (target != ruleUrlInput && target != ruleProfileInput && target != ruleAutoSaveProfileInput && target != ruleEditUrlInput && target != ruleEditProfileInput && target != ruleEditAutoSaveProfileInput && target != showAutoSaveProfileInput) {
  380. if (target != profileNamesInput && target != showAllProfilesInput) {
  381. await update();
  382. }
  383. if (target == profileNamesInput) {
  384. await refresh(profileNamesInput.value);
  385. if (sidePanelDisplay) {
  386. const tabsData = await browser.runtime.sendMessage({ method: "tabsData.get" });
  387. tabsData.profileName = profileNamesInput.value;
  388. await browser.runtime.sendMessage({ method: "tabsData.set", tabsData: tabsData });
  389. await browser.runtime.sendMessage({ method: "ui.refreshMenu" });
  390. }
  391. } else {
  392. if (target == contextMenuEnabledInput) {
  393. await browser.runtime.sendMessage({ method: "ui.refreshMenu" });
  394. }
  395. if (target == openEditorInput) {
  396. await browser.runtime.sendMessage({ method: "ui.refreshMenu" });
  397. }
  398. await refresh();
  399. }
  400. }
  401. };
  402. addProfileButton.title = browser.i18n.getMessage("profileAddButtonTooltip");
  403. deleteProfileButton.title = browser.i18n.getMessage("profileDeleteButtonTooltip");
  404. renameProfileButton.title = browser.i18n.getMessage("profileRenameButtonTooltip");
  405. removeHiddenElementsLabel.textContent = browser.i18n.getMessage("optionRemoveHiddenElements");
  406. removeUnusedStylesLabel.textContent = browser.i18n.getMessage("optionRemoveUnusedStyles");
  407. removeUnusedFontsLabel.textContent = browser.i18n.getMessage("optionRemoveUnusedFonts");
  408. removeFramesLabel.textContent = browser.i18n.getMessage("optionRemoveFrames");
  409. removeImportsLabel.textContent = browser.i18n.getMessage("optionRemoveImports");
  410. removeScriptsLabel.textContent = browser.i18n.getMessage("optionRemoveScripts");
  411. saveRawPageLabel.textContent = browser.i18n.getMessage("optionSaveRawPage");
  412. saveToClipboardLabel.textContent = browser.i18n.getMessage("optionSaveToClipboard");
  413. addProofLabel.textContent = browser.i18n.getMessage("optionAddProof");
  414. saveToGDriveLabel.textContent = browser.i18n.getMessage("optionSaveToGDrive");
  415. compressHTMLLabel.textContent = browser.i18n.getMessage("optionCompressHTML");
  416. compressCSSLabel.textContent = browser.i18n.getMessage("optionCompressCSS");
  417. loadDeferredImagesLabel.textContent = browser.i18n.getMessage("optionLoadDeferredImages");
  418. loadDeferredImagesMaxIdleTimeLabel.textContent = browser.i18n.getMessage("optionLoadDeferredImagesMaxIdleTime");
  419. addMenuEntryLabel.textContent = browser.i18n.getMessage("optionAddMenuEntry");
  420. filenameTemplateLabel.textContent = browser.i18n.getMessage("optionFilenameTemplate");
  421. shadowEnabledLabel.textContent = browser.i18n.getMessage("optionDisplayShadow");
  422. setMaxResourceSizeLabel.textContent = browser.i18n.getMessage("optionSetMaxResourceSize");
  423. maxResourceSizeLabel.textContent = browser.i18n.getMessage("optionMaxResourceSize");
  424. confirmFilenameLabel.textContent = browser.i18n.getMessage("optionConfirmFilename");
  425. filenameConflictActionLabel.textContent = browser.i18n.getMessage("optionFilenameConflictAction");
  426. filenameConflictActionUniquifyLabel.textContent = browser.i18n.getMessage("optionFilenameConflictActionUniquify");
  427. filenameConflictActionOverwriteLabel.textContent = browser.i18n.getMessage("optionFilenameConflictActionOverwrite");
  428. filenameConflictActionPromptLabel.textContent = browser.i18n.getMessage("optionFilenameConflictActionPrompt");
  429. removeAudioLabel.textContent = browser.i18n.getMessage("optionRemoveAudio");
  430. removeVideoLabel.textContent = browser.i18n.getMessage("optionRemoveVideo");
  431. displayInfobarLabel.textContent = browser.i18n.getMessage("optionDisplayInfobar");
  432. displayStatsLabel.textContent = browser.i18n.getMessage("optionDisplayStats");
  433. backgroundSaveLabel.textContent = browser.i18n.getMessage("optionBackgroundSave");
  434. autoSaveDelayLabel.textContent = browser.i18n.getMessage("optionAutoSaveDelay");
  435. autoSaveLoadLabel.textContent = browser.i18n.getMessage("optionAutoSaveLoad");
  436. autoSaveUnloadLabel.textContent = browser.i18n.getMessage("optionAutoSaveUnload");
  437. autoSaveLoadOrUnloadLabel.textContent = browser.i18n.getMessage("optionAutoSaveLoadOrUnload");
  438. autoSaveRepeatLabel.textContent = browser.i18n.getMessage("optionAutoSaveRepeat");
  439. autoSaveRepeatDelayLabel.textContent = browser.i18n.getMessage("optionAutoSaveRepeatDelay");
  440. removeAlternativeFontsLabel.textContent = browser.i18n.getMessage("optionRemoveAlternativeFonts");
  441. removeAlternativeImagesLabel.textContent = browser.i18n.getMessage("optionRemoveAlternativeImages");
  442. removeAlternativeMediasLabel.textContent = browser.i18n.getMessage("optionRemoveAlternativeMedias");
  443. groupDuplicateImagesLabel.textContent = browser.i18n.getMessage("optionGroupDuplicateImages");
  444. titleLabel.textContent = browser.i18n.getMessage("optionsTitle");
  445. userInterfaceLabel.textContent = browser.i18n.getMessage("optionsUserInterfaceSubTitle");
  446. filenameLabel.textContent = browser.i18n.getMessage("optionsFileNameSubTitle");
  447. htmlContentLabel.textContent = browser.i18n.getMessage("optionsHTMLContentSubTitle");
  448. imagesLabel.textContent = browser.i18n.getMessage("optionsImagesSubTitle");
  449. stylesheetsLabel.textContent = browser.i18n.getMessage("optionsStylesheetsSubTitle");
  450. fontsLabel.textContent = browser.i18n.getMessage("optionsFontsSubTitle");
  451. otherResourcesLabel.textContent = browser.i18n.getMessage("optionsOtherResourcesSubTitle");
  452. autoSaveLabel.textContent = browser.i18n.getMessage("optionsAutoSaveSubTitle");
  453. miscLabel.textContent = browser.i18n.getMessage("optionsMiscSubTitle");
  454. helpLabel.textContent = browser.i18n.getMessage("optionsHelpLink");
  455. infobarTemplateLabel.textContent = browser.i18n.getMessage("optionInfobarTemplate");
  456. includeInfobarLabel.textContent = browser.i18n.getMessage("optionIncludeInfobar");
  457. confirmInfobarLabel.textContent = browser.i18n.getMessage("optionConfirmInfobar");
  458. autoCloseLabel.textContent = browser.i18n.getMessage("optionAutoClose");
  459. openEditorLabel.textContent = browser.i18n.getMessage("optionOpenEditor");
  460. autoOpenEditorLabel.textContent = browser.i18n.getMessage("optionAutoOpenEditor");
  461. resetButton.textContent = browser.i18n.getMessage("optionsResetButton");
  462. exportButton.textContent = browser.i18n.getMessage("optionsExportButton");
  463. importButton.textContent = browser.i18n.getMessage("optionsImportButton");
  464. resetButton.title = browser.i18n.getMessage("optionsResetTooltip");
  465. autoSettingsLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsSubTitle");
  466. autoSettingsUrlLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsUrl");
  467. autoSettingsProfileLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsProfile");
  468. autoSettingsAutoSaveProfileLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsAutoSaveProfile");
  469. ruleAddButton.title = browser.i18n.getMessage("optionsAddRuleTooltip");
  470. ruleEditButton.title = browser.i18n.getMessage("optionsValidateChangesTooltip");
  471. rulesDeleteAllButton.title = browser.i18n.getMessage("optionsDeleteRulesTooltip");
  472. showAllProfilesLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsShowAllProfiles");
  473. showAutoSaveProfileLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsShowAutoSaveProfile");
  474. ruleUrlInput.placeholder = ruleEditUrlInput.placeholder = browser.i18n.getMessage("optionsAutoSettingsUrlPlaceholder");
  475. synchronizeLabel.textContent = browser.i18n.getMessage("optionSynchronize");
  476. resetAllButton.textContent = browser.i18n.getMessage("optionsResetAllButton");
  477. resetCurrentButton.textContent = browser.i18n.getMessage("optionsResetCurrentButton");
  478. resetCancelButton.textContent = promptCancelButton.textContent = cancelButton.textContent = browser.i18n.getMessage("optionsCancelButton");
  479. confirmButton.textContent = promptConfirmButton.textContent = browser.i18n.getMessage("optionsOKButton");
  480. document.getElementById("resetConfirmLabel").textContent = browser.i18n.getMessage("optionsResetConfirm");
  481. if (location.href.endsWith("#")) {
  482. document.querySelector(".new-window-link").remove();
  483. document.documentElement.classList.add("maximized");
  484. }
  485. const tabsData = await browser.runtime.sendMessage({ method: "tabsData.get" });
  486. getHelpContents();
  487. refresh(tabsData.profileName);
  488. async function refresh(profileName) {
  489. const [profiles, rules] = await Promise.all([browser.runtime.sendMessage({ method: "config.getProfiles" }), browser.runtime.sendMessage({ method: "config.getRules" })]);
  490. const selectedProfileName = profileName || profileNamesInput.value || DEFAULT_PROFILE_NAME;
  491. Array.from(profileNamesInput.childNodes).forEach(node => node.remove());
  492. profileNamesInput.options.length = 0;
  493. ruleProfileInput.options.length = 0;
  494. ruleAutoSaveProfileInput.options.length = 0;
  495. ruleEditProfileInput.options.length = 0;
  496. ruleEditAutoSaveProfileInput.options.length = 0;
  497. let optionElement = document.createElement("option");
  498. optionElement.value = DEFAULT_PROFILE_NAME;
  499. optionElement.textContent = browser.i18n.getMessage("profileDefaultSettings");
  500. [CURRENT_PROFILE_NAME].concat(...Object.keys(profiles)).forEach(profileName => {
  501. const optionElement = document.createElement("option");
  502. optionElement.value = optionElement.textContent = profileName;
  503. if (profileName == DEFAULT_PROFILE_NAME) {
  504. optionElement.textContent = browser.i18n.getMessage("profileDefaultSettings");
  505. }
  506. if (profileName != CURRENT_PROFILE_NAME) {
  507. profileNamesInput.appendChild(optionElement);
  508. }
  509. ruleProfileInput.appendChild(optionElement.cloneNode(true));
  510. ruleAutoSaveProfileInput.appendChild(optionElement.cloneNode(true));
  511. ruleEditProfileInput.appendChild(optionElement.cloneNode(true));
  512. ruleEditAutoSaveProfileInput.appendChild(optionElement.cloneNode(true));
  513. });
  514. profileNamesInput.disabled = profileNamesInput.options.length == 1;
  515. optionElement = document.createElement("option");
  516. optionElement.value = DISABLED_PROFILE_NAME;
  517. optionElement.textContent = browser.i18n.getMessage("profileDisabled");
  518. ruleAutoSaveProfileInput.appendChild(optionElement);
  519. ruleEditAutoSaveProfileInput.appendChild(optionElement.cloneNode(true));
  520. const rulesDataElement = rulesElement.querySelector(".rules-data");
  521. Array.from(rulesDataElement.childNodes).forEach(node => (!node.className || !node.className.includes("rule-edit")) && node.remove());
  522. const editURLElement = rulesElement.querySelector(".rule-edit");
  523. createURLElement.hidden = false;
  524. editURLElement.hidden = true;
  525. ruleProfileInput.value = ruleAutoSaveProfileInput.value = selectedProfileName;
  526. let rulesDisplayed;
  527. rules.forEach(rule => {
  528. if (showAllProfilesInput.checked || selectedProfileName == rule.profile || selectedProfileName == rule.autoSaveProfile) {
  529. rulesDisplayed = true;
  530. const ruleElement = rulesElement.querySelector(".rule-view").cloneNode(true);
  531. const ruleUrlElement = ruleElement.querySelector(".rule-url");
  532. const ruleProfileElement = ruleElement.querySelector(".rule-profile");
  533. const ruleAutoSaveProfileElement = ruleElement.querySelector(".rule-autosave-profile");
  534. ruleUrlElement.textContent = ruleUrlElement.title = rule.url;
  535. ruleProfileElement.textContent = ruleProfileElement.title = getProfileText(rule.profile);
  536. ruleAutoSaveProfileElement.textContent = ruleAutoSaveProfileElement.title = getProfileText(rule.autoSaveProfile);
  537. ruleElement.hidden = false;
  538. ruleElement.className = "tr data";
  539. rulesDataElement.appendChild(ruleElement);
  540. const ruleDeleteButton = ruleElement.querySelector(".rule-delete-button");
  541. const ruleUpdateButton = ruleElement.querySelector(".rule-update-button");
  542. ruleDeleteButton.title = browser.i18n.getMessage("optionsDeleteRuleTooltip");
  543. ruleDeleteButton.addEventListener("click", async () => {
  544. if (await confirm(browser.i18n.getMessage("optionsDeleteRuleConfirm"), "flex-end")) {
  545. await browser.runtime.sendMessage({ method: "config.deleteRule", url: rule.url });
  546. await refresh();
  547. await refreshExternalComponents();
  548. }
  549. }, false);
  550. ruleUpdateButton.title = browser.i18n.getMessage("optionsUpdateRuleTooltip");
  551. ruleUpdateButton.addEventListener("click", async () => {
  552. if (editURLElement.hidden) {
  553. createURLElement.hidden = true;
  554. editURLElement.hidden = false;
  555. rulesDataElement.replaceChild(editURLElement, ruleElement);
  556. ruleEditUrlInput.value = rule.url;
  557. ruleEditProfileInput.value = rule.profile;
  558. ruleEditAutoSaveProfileInput.value = rule.autoSaveProfile;
  559. ruleEditUrlInput.focus();
  560. editURLElement.onsubmit = async event => {
  561. event.preventDefault();
  562. rulesElement.appendChild(editURLElement);
  563. await browser.runtime.sendMessage({ method: "config.updateRule", url: rule.url, newUrl: ruleEditUrlInput.value, profileName: ruleEditProfileInput.value, autoSaveProfileName: ruleEditAutoSaveProfileInput.value });
  564. await refresh();
  565. await refreshExternalComponents();
  566. ruleUrlInput.focus();
  567. };
  568. }
  569. }, false);
  570. }
  571. });
  572. rulesDeleteAllButton.disabled = !rulesDisplayed;
  573. rulesElement.appendChild(createURLElement);
  574. profileNamesInput.value = selectedProfileName;
  575. renameProfileButton.disabled = deleteProfileButton.disabled = profileNamesInput.value == DEFAULT_PROFILE_NAME;
  576. const profileOptions = profiles[selectedProfileName];
  577. removeHiddenElementsInput.checked = profileOptions.removeHiddenElements || profileOptions.saveRawPage;
  578. removeHiddenElementsInput.disabled = profileOptions.saveRawPage;
  579. removeUnusedStylesInput.checked = profileOptions.removeUnusedStyles;
  580. removeUnusedFontsInput.checked = profileOptions.removeUnusedFonts;
  581. removeFramesInput.checked = profileOptions.removeFrames || profileOptions.saveRawPage;
  582. removeFramesInput.disabled = profileOptions.saveRawPage;
  583. removeImportsInput.checked = profileOptions.removeImports;
  584. removeScriptsInput.checked = profileOptions.removeScripts;
  585. saveRawPageInput.checked = profileOptions.saveRawPage;
  586. saveToClipboardInput.checked = profileOptions.saveToClipboard && !profileOptions.saveToGDrive;
  587. saveToClipboardInput.disabled = profileOptions.saveToGDrive;
  588. addProofInput.checked = profileOptions.addProof;
  589. saveToGDriveInput.checked = profileOptions.saveToGDrive && !profileOptions.saveToClipboard;
  590. saveToGDriveInput.disabled = profileOptions.saveToClipboard;
  591. compressHTMLInput.checked = profileOptions.compressHTML;
  592. compressCSSInput.checked = profileOptions.compressCSS;
  593. loadDeferredImagesInput.checked = profileOptions.loadDeferredImages && !profileOptions.saveRawPage;
  594. loadDeferredImagesInput.disabled = profileOptions.saveRawPage;
  595. loadDeferredImagesMaxIdleTimeInput.value = profileOptions.loadDeferredImagesMaxIdleTime;
  596. loadDeferredImagesMaxIdleTimeInput.disabled = !profileOptions.loadDeferredImages || profileOptions.saveRawPage;
  597. contextMenuEnabledInput.checked = profileOptions.contextMenuEnabled;
  598. filenameTemplateInput.value = profileOptions.filenameTemplate;
  599. filenameTemplateInput.disabled = profileOptions.saveToClipboard;
  600. shadowEnabledInput.checked = profileOptions.shadowEnabled;
  601. maxResourceSizeEnabledInput.checked = profileOptions.maxResourceSizeEnabled;
  602. maxResourceSizeInput.value = profileOptions.maxResourceSize;
  603. maxResourceSizeInput.disabled = !profileOptions.maxResourceSizeEnabled;
  604. confirmFilenameInput.checked = profileOptions.confirmFilename;
  605. confirmFilenameInput.disabled = profileOptions.saveToClipboard;
  606. filenameConflictActionInput.value = profileOptions.filenameConflictAction;
  607. filenameConflictActionInput.disabled = profileOptions.saveToClipboard || profileOptions.saveToGDrive;
  608. removeAudioSrcInput.checked = profileOptions.removeAudioSrc;
  609. removeVideoSrcInput.checked = profileOptions.removeVideoSrc;
  610. displayInfobarInput.checked = profileOptions.displayInfobar;
  611. displayStatsInput.checked = profileOptions.displayStats;
  612. backgroundSaveInput.checked = profileOptions.backgroundSave;
  613. backgroundSaveInput.disabled = profileOptions.saveToGDrive;
  614. autoSaveDelayInput.value = profileOptions.autoSaveDelay;
  615. autoSaveDelayInput.disabled = !profileOptions.autoSaveLoadOrUnload && !profileOptions.autoSaveLoad;
  616. autoSaveLoadInput.checked = !profileOptions.autoSaveLoadOrUnload && profileOptions.autoSaveLoad;
  617. autoSaveLoadOrUnloadInput.checked = profileOptions.autoSaveLoadOrUnload;
  618. autoSaveUnloadInput.checked = !profileOptions.autoSaveLoadOrUnload && profileOptions.autoSaveUnload;
  619. autoSaveLoadInput.disabled = profileOptions.autoSaveLoadOrUnload;
  620. autoSaveUnloadInput.disabled = profileOptions.autoSaveLoadOrUnload;
  621. autoSaveRepeatInput.checked = profileOptions.autoSaveRepeat;
  622. autoSaveRepeatInput.disabled = !profileOptions.autoSaveLoadOrUnload && !profileOptions.autoSaveLoad;
  623. autoSaveRepeatDelayInput.value = profileOptions.autoSaveRepeatDelay;
  624. autoSaveRepeatDelayInput.disabled = !profileOptions.autoSaveRepeat;
  625. removeAlternativeFontsInput.checked = profileOptions.removeAlternativeFonts;
  626. removeAlternativeImagesInput.checked = profileOptions.removeAlternativeImages;
  627. groupDuplicateImagesInput.checked = profileOptions.groupDuplicateImages;
  628. removeAlternativeMediasInput.checked = profileOptions.removeAlternativeMedias;
  629. infobarTemplateInput.value = profileOptions.infobarTemplate;
  630. includeInfobarInput.checked = profileOptions.includeInfobar;
  631. confirmInfobarInput.checked = profileOptions.confirmInfobarContent;
  632. autoCloseInput.checked = profileOptions.autoClose;
  633. openEditorInput.checked = profileOptions.openEditor;
  634. autoOpenEditorInput.checked = profileOptions.autoOpenEditor;
  635. removeFramesInput.disabled = saveRawPageInput.checked;
  636. removeFramesInput.checked = removeFramesInput.checked || saveRawPageInput.checked;
  637. loadDeferredImagesInput.disabled = saveRawPageInput.checked;
  638. if (saveRawPageInput.checked) {
  639. loadDeferredImagesInput.checked = false;
  640. }
  641. }
  642. function getProfileText(profileName) {
  643. return profileName == DEFAULT_PROFILE_NAME ? browser.i18n.getMessage("profileDefaultSettings") : profileName == DISABLED_PROFILE_NAME ? browser.i18n.getMessage("profileDisabled") : profileName;
  644. }
  645. async function update() {
  646. await pendingSave;
  647. pendingSave = browser.runtime.sendMessage({
  648. method: "config.updateProfile",
  649. profileName: profileNamesInput.value,
  650. profile: {
  651. removeHiddenElements: removeHiddenElementsInput.checked,
  652. removeUnusedStyles: removeUnusedStylesInput.checked,
  653. removeUnusedFonts: removeUnusedFontsInput.checked,
  654. removeFrames: removeFramesInput.checked,
  655. removeImports: removeImportsInput.checked,
  656. removeScripts: removeScriptsInput.checked,
  657. saveRawPage: saveRawPageInput.checked,
  658. saveToClipboard: saveToClipboardInput.checked,
  659. addProof: addProofInput.checked,
  660. saveToGDrive: saveToGDriveInput.checked,
  661. compressHTML: compressHTMLInput.checked,
  662. compressCSS: compressCSSInput.checked,
  663. loadDeferredImages: loadDeferredImagesInput.checked,
  664. loadDeferredImagesMaxIdleTime: Math.max(loadDeferredImagesMaxIdleTimeInput.value, 0),
  665. contextMenuEnabled: contextMenuEnabledInput.checked,
  666. filenameTemplate: filenameTemplateInput.value,
  667. shadowEnabled: shadowEnabledInput.checked,
  668. maxResourceSizeEnabled: maxResourceSizeEnabledInput.checked,
  669. maxResourceSize: Math.max(maxResourceSizeInput.value, 0),
  670. confirmFilename: confirmFilenameInput.checked,
  671. filenameConflictAction: filenameConflictActionInput.value,
  672. removeAudioSrc: removeAudioSrcInput.checked,
  673. removeVideoSrc: removeVideoSrcInput.checked,
  674. displayInfobar: displayInfobarInput.checked,
  675. displayStats: displayStatsInput.checked,
  676. backgroundSave: backgroundSaveInput.checked,
  677. autoSaveDelay: Math.max(autoSaveDelayInput.value, 0),
  678. autoSaveLoad: autoSaveLoadInput.checked,
  679. autoSaveUnload: autoSaveUnloadInput.checked,
  680. autoSaveLoadOrUnload: autoSaveLoadOrUnloadInput.checked,
  681. autoSaveRepeat: autoSaveRepeatInput.checked,
  682. autoSaveRepeatDelay: Math.max(autoSaveRepeatDelayInput.value, 1),
  683. removeAlternativeFonts: removeAlternativeFontsInput.checked,
  684. removeAlternativeImages: removeAlternativeImagesInput.checked,
  685. removeAlternativeMedias: removeAlternativeMediasInput.checked,
  686. groupDuplicateImages: groupDuplicateImagesInput.checked,
  687. infobarTemplate: infobarTemplateInput.value,
  688. includeInfobar: includeInfobarInput.checked,
  689. confirmInfobarContent: confirmInfobarInput.checked,
  690. autoClose: autoCloseInput.checked,
  691. openEditor: openEditorInput.checked,
  692. autoOpenEditor: autoOpenEditorInput.checked
  693. }
  694. });
  695. await pendingSave;
  696. }
  697. async function refreshExternalComponents() {
  698. try {
  699. await browser.runtime.sendMessage({ method: "ui.refreshMenu" });
  700. if (sidePanelDisplay) {
  701. await browser.runtime.sendMessage({ method: "options.refresh", profileName: profileNamesInput.value });
  702. } else {
  703. await browser.runtime.sendMessage({ method: "options.refreshPanel", profileName: profileNamesInput.value });
  704. }
  705. } catch (error) {
  706. // ignored
  707. }
  708. }
  709. async function confirm(message, position = "center") {
  710. document.getElementById("confirmLabel").textContent = message;
  711. document.getElementById("formConfirmContainer").style.setProperty("display", "flex");
  712. document.querySelector("#formConfirmContainer .popup-content").style.setProperty("align-self", position);
  713. confirmButton.focus();
  714. document.body.style.setProperty("overflow-y", "hidden");
  715. return new Promise(resolve => {
  716. confirmButton.onclick = event => hideAndResolve(event, true);
  717. cancelButton.onclick = event => hideAndResolve(event);
  718. window.onkeyup = event => {
  719. if (event.key == "Escape") {
  720. hideAndResolve(event);
  721. }
  722. };
  723. function hideAndResolve(event, value) {
  724. event.preventDefault();
  725. document.getElementById("formConfirmContainer").style.setProperty("display", "none");
  726. document.body.style.setProperty("overflow-y", "");
  727. resolve(value);
  728. }
  729. });
  730. }
  731. async function reset() {
  732. document.getElementById("formResetContainer").style.setProperty("display", "flex");
  733. resetCancelButton.focus();
  734. document.body.style.setProperty("overflow-y", "hidden");
  735. return new Promise(resolve => {
  736. resetAllButton.onclick = event => hideAndResolve(event, "all");
  737. resetCurrentButton.onclick = event => hideAndResolve(event, "current");
  738. resetCancelButton.onclick = event => hideAndResolve(event);
  739. window.onkeyup = event => {
  740. if (event.key == "Escape") {
  741. hideAndResolve(event);
  742. }
  743. };
  744. function hideAndResolve(event, value) {
  745. event.preventDefault();
  746. document.getElementById("formResetContainer").style.setProperty("display", "none");
  747. document.body.style.setProperty("overflow-y", "");
  748. resolve(value);
  749. }
  750. });
  751. }
  752. async function prompt(message, position = "center", defaultValue = "") {
  753. document.getElementById("promptLabel").textContent = message;
  754. document.getElementById("formPromptContainer").style.setProperty("display", "flex");
  755. document.querySelector("#formPromptContainer .popup-content").style.setProperty("align-self", position);
  756. promptInput.value = defaultValue;
  757. promptInput.focus();
  758. document.body.style.setProperty("overflow-y", "hidden");
  759. return new Promise(resolve => {
  760. promptConfirmButton.onclick = event => hideAndResolve(event, promptInput.value);
  761. promptCancelButton.onclick = event => hideAndResolve(event);
  762. window.onkeyup = event => {
  763. if (event.key == "Escape") {
  764. hideAndResolve(event);
  765. }
  766. };
  767. function hideAndResolve(event, value) {
  768. event.preventDefault();
  769. document.getElementById("formPromptContainer").style.setProperty("display", "none");
  770. document.body.style.setProperty("overflow-y", "");
  771. resolve(value);
  772. }
  773. });
  774. }
  775. async function getHelpContents() {
  776. const helpPage = await fetch(browser.runtime.getURL(HELP_PAGE_PATH));
  777. const content = new TextDecoder().decode(await helpPage.arrayBuffer());
  778. const doc = (new DOMParser()).parseFromString(content, "text/html");
  779. const items = doc.querySelectorAll("[data-options-label]");
  780. items.forEach(itemElement => {
  781. const optionLabel = document.getElementById(itemElement.dataset.optionsLabel);
  782. const helpIconContainer = document.createElement("span");
  783. const helpIcon = document.createElement("img");
  784. helpIcon.src = HELP_ICON_URL;
  785. helpIconContainer.className = "help-icon";
  786. helpIconContainer.onclick = () => {
  787. helpContent.hidden = !helpContent.hidden;
  788. return false;
  789. };
  790. helpIcon.tabIndex = 0;
  791. helpIconContainer.onkeyup = event => {
  792. if (event.code == "Enter") {
  793. helpContent.hidden = !helpContent.hidden;
  794. return false;
  795. }
  796. };
  797. helpIconContainer.appendChild(helpIcon);
  798. optionLabel.appendChild(helpIconContainer);
  799. const helpContent = document.createElement("div");
  800. helpContent.hidden = true;
  801. helpContent.className = "help-content";
  802. itemElement.childNodes.forEach(node => {
  803. if (node instanceof HTMLElement && node.className != "option") {
  804. helpContent.appendChild(document.importNode(node, true));
  805. }
  806. });
  807. helpContent.querySelectorAll("a[href]").forEach(linkElement => {
  808. const hrefValue = linkElement.getAttribute("href");
  809. if (hrefValue.startsWith("#")) {
  810. linkElement.href = browser.runtime.getURL(HELP_PAGE_PATH + linkElement.getAttribute("href"));
  811. linkElement.target = "_blank";
  812. }
  813. });
  814. optionLabel.parentElement.insertAdjacentElement("afterEnd", helpContent);
  815. });
  816. }
  817. function getLocalStorageItem(key) {
  818. try {
  819. return localStorage.getItem(key);
  820. } catch (error) {
  821. // ignored
  822. }
  823. }
  824. function setLocalStorageItem(key, value) {
  825. try {
  826. return localStorage.setItem(key, value);
  827. } catch (error) {
  828. // ignored
  829. }
  830. }
  831. function removeLocalStorageItem(key) {
  832. try {
  833. return localStorage.removeItem(key);
  834. } catch (error) {
  835. // ignored
  836. }
  837. }
  838. })();