ui-options.js 58 KB

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