ui-options.js 65 KB

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