ui-options.js 58 KB

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