ui-options.js 42 KB

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