ui-options.js 59 KB

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