1
0

ui-options.js 42 KB

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