ui-options.js 52 KB

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