ui-options.js 52 KB

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