ui-options.js 53 KB

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