ui-options.js 50 KB

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