ui-options.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. /*
  2. * Copyright 2010-2019 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * SingleFile is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SingleFile 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
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with SingleFile. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /* global browser, window, document, localStorage, FileReader, location */
  21. (async () => {
  22. const { DEFAULT_PROFILE_NAME, DISABLED_PROFILE_NAME } = await browser.runtime.sendMessage({ getConfigConstants: true });
  23. const removeHiddenElementsLabel = document.getElementById("removeHiddenElementsLabel");
  24. const removeUnusedStylesLabel = document.getElementById("removeUnusedStylesLabel");
  25. const removeUnusedFontsLabel = document.getElementById("removeUnusedFontsLabel");
  26. const removeFramesLabel = document.getElementById("removeFramesLabel");
  27. const removeImportsLabel = document.getElementById("removeImportsLabel");
  28. const removeScriptsLabel = document.getElementById("removeScriptsLabel");
  29. const saveRawPageLabel = document.getElementById("saveRawPageLabel");
  30. const compressHTMLLabel = document.getElementById("compressHTMLLabel");
  31. const compressCSSLabel = document.getElementById("compressCSSLabel");
  32. const loadDeferredImagesLabel = document.getElementById("loadDeferredImagesLabel");
  33. const loadDeferredImagesMaxIdleTimeLabel = document.getElementById("loadDeferredImagesMaxIdleTimeLabel");
  34. const addMenuEntryLabel = document.getElementById("addMenuEntryLabel");
  35. const filenameTemplateLabel = document.getElementById("filenameTemplateLabel");
  36. const shadowEnabledLabel = document.getElementById("shadowEnabledLabel");
  37. const setMaxResourceSizeLabel = document.getElementById("setMaxResourceSizeLabel");
  38. const maxResourceSizeLabel = document.getElementById("maxResourceSizeLabel");
  39. const confirmFilenameLabel = document.getElementById("confirmFilenameLabel");
  40. const filenameConflictActionLabel = document.getElementById("filenameConflictActionLabel");
  41. const filenameConflictActionUniquifyLabel = document.getElementById("filenameConflictActionUniquifyLabel");
  42. const filenameConflictActionOverwriteLabel = document.getElementById("filenameConflictActionOverwriteLabel");
  43. const filenameConflictActionPromptLabel = document.getElementById("filenameConflictActionPromptLabel");
  44. const removeAudioLabel = document.getElementById("removeAudioLabel");
  45. const removeVideoLabel = document.getElementById("removeVideoLabel");
  46. const displayInfobarLabel = document.getElementById("displayInfobarLabel");
  47. const displayStatsLabel = document.getElementById("displayStatsLabel");
  48. const backgroundSaveLabel = document.getElementById("backgroundSaveLabel");
  49. const autoSaveDelayLabel = document.getElementById("autoSaveDelayLabel");
  50. const autoSaveLoadLabel = document.getElementById("autoSaveLoadLabel");
  51. const autoSaveUnloadLabel = document.getElementById("autoSaveUnloadLabel");
  52. const autoSaveLoadOrUnloadLabel = document.getElementById("autoSaveLoadOrUnloadLabel");
  53. const autoSaveRepeatLabel = document.getElementById("autoSaveRepeatLabel");
  54. const autoSaveRepeatDelayLabel = document.getElementById("autoSaveRepeatDelayLabel");
  55. const removeAlternativeFontsLabel = document.getElementById("removeAlternativeFontsLabel");
  56. const removeAlternativeImagesLabel = document.getElementById("removeAlternativeImagesLabel");
  57. const removeAlternativeMediasLabel = document.getElementById("removeAlternativeMediasLabel");
  58. const titleLabel = document.getElementById("titleLabel");
  59. const userInterfaceLabel = document.getElementById("userInterfaceLabel");
  60. const filenameLabel = document.getElementById("filenameLabel");
  61. const htmlContentLabel = document.getElementById("htmlContentLabel");
  62. const imagesLabel = document.getElementById("imagesLabel");
  63. const stylesheetsLabel = document.getElementById("stylesheetsLabel");
  64. const fontsLabel = document.getElementById("fontsLabel");
  65. const otherResourcesLabel = document.getElementById("otherResourcesLabel");
  66. const autoSaveLabel = document.getElementById("autoSaveLabel");
  67. const autoSettingsLabel = document.getElementById("autoSettingsLabel");
  68. const autoSettingsUrlLabel = document.getElementById("autoSettingsUrlLabel");
  69. const autoSettingsProfileLabel = document.getElementById("autoSettingsProfileLabel");
  70. const autoSettingsAutoSaveProfileLabel = document.getElementById("autoSettingsAutoSaveProfileLabel");
  71. const showAllProfilesLabel = document.getElementById("showAllProfilesLabel");
  72. const showAutoSaveProfileLabel = document.getElementById("showAutoSaveProfileLabel");
  73. const groupDuplicateImagesLabel = document.getElementById("groupDuplicateImagesLabel");
  74. const confirmInfobarLabel = document.getElementById("confirmInfobarLabel");
  75. const infobarTemplateLabel = document.getElementById("infobarTemplateLabel");
  76. const miscLabel = document.getElementById("miscLabel");
  77. const helpLabel = document.getElementById("helpLabel");
  78. const addProfileButton = document.getElementById("addProfileButton");
  79. const deleteProfileButton = document.getElementById("deleteProfileButton");
  80. const renameProfileButton = document.getElementById("renameProfileButton");
  81. const resetButton = document.getElementById("resetButton");
  82. const exportButton = document.getElementById("exportButton");
  83. const importButton = document.getElementById("importButton");
  84. const fileInput = document.getElementById("fileInput");
  85. const profileNamesInput = document.getElementById("profileNamesInput");
  86. const removeHiddenElementsInput = document.getElementById("removeHiddenElementsInput");
  87. const removeUnusedStylesInput = document.getElementById("removeUnusedStylesInput");
  88. const removeUnusedFontsInput = document.getElementById("removeUnusedFontsInput");
  89. const removeFramesInput = document.getElementById("removeFramesInput");
  90. const removeImportsInput = document.getElementById("removeImportsInput");
  91. const removeScriptsInput = document.getElementById("removeScriptsInput");
  92. const saveRawPageInput = document.getElementById("saveRawPageInput");
  93. const compressHTMLInput = document.getElementById("compressHTMLInput");
  94. const compressCSSInput = document.getElementById("compressCSSInput");
  95. const loadDeferredImagesInput = document.getElementById("loadDeferredImagesInput");
  96. const loadDeferredImagesMaxIdleTimeInput = document.getElementById("loadDeferredImagesMaxIdleTimeInput");
  97. const contextMenuEnabledInput = document.getElementById("contextMenuEnabledInput");
  98. const filenameTemplateInput = document.getElementById("filenameTemplateInput");
  99. const shadowEnabledInput = document.getElementById("shadowEnabledInput");
  100. const maxResourceSizeInput = document.getElementById("maxResourceSizeInput");
  101. const maxResourceSizeEnabledInput = document.getElementById("maxResourceSizeEnabledInput");
  102. const confirmFilenameInput = document.getElementById("confirmFilenameInput");
  103. const filenameConflictActionInput = document.getElementById("filenameConflictActionInput");
  104. const removeAudioSrcInput = document.getElementById("removeAudioSrcInput");
  105. const removeVideoSrcInput = document.getElementById("removeVideoSrcInput");
  106. const displayInfobarInput = document.getElementById("displayInfobarInput");
  107. const displayStatsInput = document.getElementById("displayStatsInput");
  108. const backgroundSaveInput = document.getElementById("backgroundSaveInput");
  109. const autoSaveDelayInput = document.getElementById("autoSaveDelayInput");
  110. const autoSaveLoadInput = document.getElementById("autoSaveLoadInput");
  111. const autoSaveUnloadInput = document.getElementById("autoSaveUnloadInput");
  112. const autoSaveLoadOrUnloadInput = document.getElementById("autoSaveLoadOrUnloadInput");
  113. const autoSaveRepeatInput = document.getElementById("autoSaveRepeatInput");
  114. const autoSaveRepeatDelayInput = document.getElementById("autoSaveRepeatDelayInput");
  115. const removeAlternativeFontsInput = document.getElementById("removeAlternativeFontsInput");
  116. const removeAlternativeImagesInput = document.getElementById("removeAlternativeImagesInput");
  117. const removeAlternativeMediasInput = document.getElementById("removeAlternativeMediasInput");
  118. const groupDuplicateImagesInput = document.getElementById("groupDuplicateImagesInput");
  119. const infobarTemplateInput = document.getElementById("infobarTemplateInput");
  120. const confirmInfobarInput = document.getElementById("confirmInfobarInput");
  121. const expandAllButton = document.getElementById("expandAllButton");
  122. const rulesDeleteAllButton = document.getElementById("rulesDeleteAllButton");
  123. const ruleUrlInput = document.getElementById("ruleUrlInput");
  124. const ruleProfileInput = document.getElementById("ruleProfileInput");
  125. const ruleAutoSaveProfileInput = document.getElementById("ruleAutoSaveProfileInput");
  126. const ruleEditProfileInput = document.getElementById("ruleEditProfileInput");
  127. const ruleEditAutoSaveProfileInput = document.getElementById("ruleEditAutoSaveProfileInput");
  128. const ruleAddButton = document.getElementById("ruleAddButton");
  129. const rulesElement = document.querySelector(".rules-table");
  130. const rulesContainerElement = document.querySelector(".rules-table-container");
  131. const ruleEditUrlInput = document.getElementById("ruleEditUrlInput");
  132. const ruleEditButton = document.getElementById("ruleEditButton");
  133. const createURLElement = rulesElement.querySelector(".rule-create");
  134. const showAllProfilesInput = document.getElementById("showAllProfilesInput");
  135. const showAutoSaveProfileInput = document.getElementById("showAutoSaveProfileInput");
  136. const resetAllButton = document.getElementById("resetAllButton");
  137. const resetCurrentButton = document.getElementById("resetCurrentButton");
  138. const resetCancelButton = document.getElementById("resetCancelButton");
  139. const confirmButton = document.getElementById("confirmButton");
  140. const cancelButton = document.getElementById("cancelButton");
  141. const promptInput = document.getElementById("promptInput");
  142. const promptCancelButton = document.getElementById("promptCancelButton");
  143. const promptConfirmButton = document.getElementById("promptConfirmButton");
  144. let sidePanelDisplay;
  145. browser.runtime.onMessage.addListener(message => {
  146. if (message.refreshOptions || (message.refreshOptionsPanel && sidePanelDisplay)) {
  147. refresh(message.profileName);
  148. }
  149. });
  150. let pendingSave = Promise.resolve();
  151. let autoSaveProfileChanged;
  152. ruleProfileInput.onchange = () => {
  153. if (!autoSaveProfileChanged) {
  154. ruleAutoSaveProfileInput.value = ruleProfileInput.value;
  155. }
  156. };
  157. ruleAutoSaveProfileInput.onchange = () => {
  158. autoSaveProfileChanged = true;
  159. };
  160. rulesDeleteAllButton.addEventListener("click", async () => {
  161. if (await confirm(browser.i18n.getMessage("optionsDeleteDisplayedRulesConfirm"))) {
  162. await browser.runtime.sendMessage({ deleteRules: true, profileName: !showAllProfilesInput.checked && profileNamesInput.value });
  163. await refresh();
  164. refreshExternalComponents();
  165. }
  166. }, false);
  167. createURLElement.onsubmit = async event => {
  168. event.preventDefault();
  169. try {
  170. await browser.runtime.sendMessage({ addRule: true, url: ruleUrlInput.value, profileName: ruleProfileInput.value, autoSaveProfileName: ruleAutoSaveProfileInput.value });
  171. ruleUrlInput.value = "";
  172. ruleProfileInput.value = ruleAutoSaveProfileInput.value = DEFAULT_PROFILE_NAME;
  173. autoSaveProfileChanged = false;
  174. await refresh();
  175. refreshExternalComponents();
  176. ruleUrlInput.focus();
  177. } catch (error) {
  178. // ignored
  179. }
  180. };
  181. ruleUrlInput.onclick = ruleUrlInput.onkeyup = ruleUrlInput.onchange = async () => {
  182. ruleAddButton.disabled = !ruleUrlInput.value;
  183. const rules = await browser.runtime.sendMessage({ getRules: true });
  184. if (rules.find(rule => rule.url == ruleUrlInput.value)) {
  185. ruleAddButton.disabled = true;
  186. }
  187. };
  188. ruleEditUrlInput.onclick = ruleEditUrlInput.onkeyup = ruleEditUrlInput.onchange = async () => {
  189. ruleEditButton.disabled = !ruleEditUrlInput.value;
  190. const rules = await browser.runtime.sendMessage({ getRules: true });
  191. if (rules.find(rule => rule.url == ruleEditUrlInput.value)) {
  192. ruleEditButton.disabled = true;
  193. }
  194. };
  195. if (localStorage.getItem("optionShowAutoSaveProfile")) {
  196. showAutoSaveProfileInput.checked = true;
  197. rulesContainerElement.classList.remove("compact");
  198. }
  199. showAutoSaveProfileInput.addEventListener("click", () => {
  200. if (showAutoSaveProfileInput.checked) {
  201. localStorage.setItem("optionShowAutoSaveProfile", 1);
  202. rulesContainerElement.classList.remove("compact");
  203. } else {
  204. localStorage.removeItem("optionShowAutoSaveProfile");
  205. rulesContainerElement.classList.add("compact");
  206. }
  207. }, false);
  208. if (localStorage.getItem("optionShowAllProfiles")) {
  209. showAllProfilesInput.checked = true;
  210. }
  211. showAllProfilesInput.addEventListener("click", () => {
  212. if (showAllProfilesInput.checked) {
  213. localStorage.setItem("optionShowAllProfiles", 1);
  214. } else {
  215. localStorage.removeItem("optionShowAllProfiles");
  216. }
  217. }, false);
  218. addProfileButton.addEventListener("click", async () => {
  219. const profileName = await prompt(browser.i18n.getMessage("profileAddPrompt"));
  220. if (profileName) {
  221. try {
  222. await browser.runtime.sendMessage({ createProfile: true, profileName });
  223. await refresh(profileName);
  224. refreshExternalComponents();
  225. } catch (error) {
  226. // ignored
  227. }
  228. }
  229. }, false);
  230. deleteProfileButton.addEventListener("click", async () => {
  231. if (await confirm(browser.i18n.getMessage("profileDeleteConfirm"))) {
  232. try {
  233. await browser.runtime.sendMessage({ deleteProfile: true, profileName: profileNamesInput.value });
  234. profileNamesInput.value = null;
  235. await refresh();
  236. refreshExternalComponents();
  237. } catch (error) {
  238. // ignored
  239. }
  240. }
  241. }, false);
  242. renameProfileButton.addEventListener("click", async () => {
  243. const profileName = await prompt(browser.i18n.getMessage("profileRenamePrompt"), profileNamesInput.value);
  244. if (profileName) {
  245. try {
  246. await browser.runtime.sendMessage({ renameProfile: true, profileName: profileNamesInput.value, newProfileName: profileName });
  247. await refresh(profileName);
  248. refreshExternalComponents();
  249. } catch (error) {
  250. // ignored
  251. }
  252. }
  253. }, false);
  254. resetButton.addEventListener("click", async () => {
  255. const choice = await reset();
  256. if (choice) {
  257. if (choice == "all") {
  258. await browser.runtime.sendMessage({ resetProfiles: true });
  259. await refresh(DEFAULT_PROFILE_NAME);
  260. refreshExternalComponents();
  261. }
  262. if (choice == "current") {
  263. await browser.runtime.sendMessage({ resetProfile: true, profileName: profileNamesInput.value });
  264. await refresh();
  265. refreshExternalComponents();
  266. }
  267. await update();
  268. }
  269. }, false);
  270. exportButton.addEventListener("click", async () => {
  271. await browser.runtime.sendMessage({ exportConfig: true });
  272. }, false);
  273. importButton.addEventListener("click", () => {
  274. fileInput.onchange = async () => {
  275. if (fileInput.files.length) {
  276. const reader = new FileReader();
  277. reader.readAsText(fileInput.files[0]);
  278. const serializedConfig = await new Promise((resolve, reject) => {
  279. reader.addEventListener("load", () => resolve(reader.result), false);
  280. reader.addEventListener("error", reject, false);
  281. });
  282. const config = JSON.parse(serializedConfig);
  283. await browser.runtime.sendMessage({ importConfig: true, config });
  284. await refresh(DEFAULT_PROFILE_NAME);
  285. fileInput.value = "";
  286. }
  287. };
  288. fileInput.click();
  289. }, false);
  290. autoSaveUnloadInput.addEventListener("click", async () => {
  291. if (!autoSaveLoadInput.checked && !autoSaveUnloadInput.checked) {
  292. autoSaveLoadOrUnloadInput.checked = true;
  293. }
  294. }, false);
  295. autoSaveLoadInput.addEventListener("click", async () => {
  296. if (!autoSaveLoadInput.checked && !autoSaveUnloadInput.checked) {
  297. autoSaveLoadOrUnloadInput.checked = true;
  298. }
  299. }, false);
  300. autoSaveLoadOrUnloadInput.addEventListener("click", async () => {
  301. if (autoSaveLoadOrUnloadInput.checked) {
  302. autoSaveUnloadInput.checked = autoSaveLoadInput.checked = false;
  303. } else {
  304. autoSaveUnloadInput.checked = false;
  305. autoSaveLoadInput.checked = true;
  306. }
  307. }, false);
  308. expandAllButton.addEventListener("click", () => {
  309. if (expandAllButton.className) {
  310. expandAllButton.className = "";
  311. } else {
  312. expandAllButton.className = "opened";
  313. }
  314. document.querySelectorAll("details").forEach(detailElement => detailElement.open = Boolean(expandAllButton.className));
  315. }, false);
  316. removeScriptsInput.addEventListener("click", () => {
  317. if (!removeScriptsInput.checked) {
  318. removeHiddenElementsInput.checked = false;
  319. removeUnusedStylesInput.checked = false;
  320. }
  321. }, false);
  322. document.body.onchange = async event => {
  323. let target = event.target;
  324. if (target != ruleUrlInput && target != ruleProfileInput && target != ruleAutoSaveProfileInput && target != ruleEditUrlInput && target != ruleEditProfileInput && target != ruleEditAutoSaveProfileInput && target != showAutoSaveProfileInput) {
  325. if (target != profileNamesInput && target != showAllProfilesInput) {
  326. await update();
  327. }
  328. if (target == profileNamesInput) {
  329. await refresh(profileNamesInput.value);
  330. if (sidePanelDisplay) {
  331. const tabsData = await browser.runtime.sendMessage({ getTabsData: true });
  332. tabsData.profileName = profileNamesInput.value;
  333. await browser.runtime.sendMessage({ setTabsData: true, tabsData });
  334. await browser.runtime.sendMessage({ refreshMenu: true });
  335. }
  336. } else {
  337. await refresh();
  338. }
  339. }
  340. };
  341. addProfileButton.title = browser.i18n.getMessage("profileAddButtonTooltip");
  342. deleteProfileButton.title = browser.i18n.getMessage("profileDeleteButtonTooltip");
  343. renameProfileButton.title = browser.i18n.getMessage("profileRenameButtonTooltip");
  344. removeHiddenElementsLabel.textContent = browser.i18n.getMessage("optionRemoveHiddenElements");
  345. removeUnusedStylesLabel.textContent = browser.i18n.getMessage("optionRemoveUnusedStyles");
  346. removeUnusedFontsLabel.textContent = browser.i18n.getMessage("optionRemoveUnusedFonts");
  347. removeFramesLabel.textContent = browser.i18n.getMessage("optionRemoveFrames");
  348. removeImportsLabel.textContent = browser.i18n.getMessage("optionRemoveImports");
  349. removeScriptsLabel.textContent = browser.i18n.getMessage("optionRemoveScripts");
  350. saveRawPageLabel.textContent = browser.i18n.getMessage("optionSaveRawPage");
  351. compressHTMLLabel.textContent = browser.i18n.getMessage("optionCompressHTML");
  352. compressCSSLabel.textContent = browser.i18n.getMessage("optionCompressCSS");
  353. loadDeferredImagesLabel.textContent = browser.i18n.getMessage("optionLoadDeferredImages");
  354. loadDeferredImagesMaxIdleTimeLabel.textContent = browser.i18n.getMessage("optionLoadDeferredImagesMaxIdleTime");
  355. addMenuEntryLabel.textContent = browser.i18n.getMessage("optionAddMenuEntry");
  356. filenameTemplateLabel.textContent = browser.i18n.getMessage("optionFilenameTemplate");
  357. shadowEnabledLabel.textContent = browser.i18n.getMessage("optionDisplayShadow");
  358. setMaxResourceSizeLabel.textContent = browser.i18n.getMessage("optionSetMaxResourceSize");
  359. maxResourceSizeLabel.textContent = browser.i18n.getMessage("optionMaxResourceSize");
  360. confirmFilenameLabel.textContent = browser.i18n.getMessage("optionConfirmFilename");
  361. filenameConflictActionLabel.textContent = browser.i18n.getMessage("optionFilenameConflictAction");
  362. filenameConflictActionUniquifyLabel.textContent = browser.i18n.getMessage("optionFilenameConflictActionUniquify");
  363. filenameConflictActionOverwriteLabel.textContent = browser.i18n.getMessage("optionFilenameConflictActionOverwrite");
  364. filenameConflictActionPromptLabel.textContent = browser.i18n.getMessage("optionFilenameConflictActionPrompt");
  365. removeAudioLabel.textContent = browser.i18n.getMessage("optionRemoveAudio");
  366. removeVideoLabel.textContent = browser.i18n.getMessage("optionRemoveVideo");
  367. displayInfobarLabel.textContent = browser.i18n.getMessage("optionDisplayInfobar");
  368. displayStatsLabel.textContent = browser.i18n.getMessage("optionDisplayStats");
  369. backgroundSaveLabel.textContent = browser.i18n.getMessage("optionBackgroundSave");
  370. autoSaveDelayLabel.textContent = browser.i18n.getMessage("optionAutoSaveDelay");
  371. autoSaveLoadLabel.textContent = browser.i18n.getMessage("optionAutoSaveLoad");
  372. autoSaveUnloadLabel.textContent = browser.i18n.getMessage("optionAutoSaveUnload");
  373. autoSaveLoadOrUnloadLabel.textContent = browser.i18n.getMessage("optionAutoSaveLoadOrUnload");
  374. autoSaveRepeatLabel.textContent = browser.i18n.getMessage("optionAutoSaveRepeat");
  375. autoSaveRepeatDelayLabel.textContent = browser.i18n.getMessage("optionAutoSaveRepeatDelay");
  376. removeAlternativeFontsLabel.textContent = browser.i18n.getMessage("optionRemoveAlternativeFonts");
  377. removeAlternativeImagesLabel.textContent = browser.i18n.getMessage("optionRemoveAlternativeImages");
  378. removeAlternativeMediasLabel.textContent = browser.i18n.getMessage("optionRemoveAlternativeMedias");
  379. groupDuplicateImagesLabel.textContent = browser.i18n.getMessage("optionGroupDuplicateImages");
  380. titleLabel.textContent = browser.i18n.getMessage("optionsTitle");
  381. userInterfaceLabel.textContent = browser.i18n.getMessage("optionsUserInterfaceSubTitle");
  382. filenameLabel.textContent = browser.i18n.getMessage("optionsFileNameSubTitle");
  383. htmlContentLabel.textContent = browser.i18n.getMessage("optionsHTMLContentSubTitle");
  384. imagesLabel.textContent = browser.i18n.getMessage("optionsImagesSubTitle");
  385. stylesheetsLabel.textContent = browser.i18n.getMessage("optionsStylesheetsSubTitle");
  386. fontsLabel.textContent = browser.i18n.getMessage("optionsFontsSubTitle");
  387. otherResourcesLabel.textContent = browser.i18n.getMessage("optionsOtherResourcesSubTitle");
  388. autoSaveLabel.textContent = browser.i18n.getMessage("optionsAutoSaveSubTitle");
  389. miscLabel.textContent = browser.i18n.getMessage("optionsMiscSubTitle");
  390. helpLabel.textContent = browser.i18n.getMessage("optionsHelpLink");
  391. infobarTemplateLabel.textContent = browser.i18n.getMessage("optionInfobarTemplate");
  392. confirmInfobarLabel.textContent = browser.i18n.getMessage("optionConfirmInfobar");
  393. resetButton.textContent = browser.i18n.getMessage("optionsResetButton");
  394. exportButton.textContent = browser.i18n.getMessage("optionsExportButton");
  395. importButton.textContent = browser.i18n.getMessage("optionsImportButton");
  396. resetButton.title = browser.i18n.getMessage("optionsResetTooltip");
  397. autoSettingsLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsSubTitle");
  398. autoSettingsUrlLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsUrl");
  399. autoSettingsProfileLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsProfile");
  400. autoSettingsAutoSaveProfileLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsAutoSaveProfile");
  401. ruleAddButton.title = browser.i18n.getMessage("optionsAddRuleTooltip");
  402. ruleEditButton.title = browser.i18n.getMessage("optionsValidateChangesTooltip");
  403. rulesDeleteAllButton.title = browser.i18n.getMessage("optionsDeleteRulesTooltip");
  404. showAllProfilesLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsShowAllProfiles");
  405. showAutoSaveProfileLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsShowAutoSaveProfile");
  406. ruleUrlInput.placeholder = ruleEditUrlInput.placeholder = browser.i18n.getMessage("optionsAutoSettingsUrlPlaceholder");
  407. resetAllButton.textContent = browser.i18n.getMessage("optionsResetAllButton");
  408. resetCurrentButton.textContent = browser.i18n.getMessage("optionsResetCurrentButton");
  409. resetCancelButton.textContent = promptCancelButton.textContent = cancelButton.textContent = browser.i18n.getMessage("optionsCancelButton");
  410. confirmButton.textContent = promptConfirmButton.textContent = browser.i18n.getMessage("optionsOKButton");
  411. document.getElementById("resetConfirmLabel").textContent = browser.i18n.getMessage("optionsResetConfirm");
  412. if (location.href.endsWith("#")) {
  413. document.querySelector(".new-window-link").remove();
  414. }
  415. if (location.href.endsWith("#side-panel")) {
  416. sidePanelDisplay = true;
  417. document.querySelector(".options-title").remove();
  418. document.documentElement.classList.add("side-panel");
  419. const tabsData = await browser.runtime.sendMessage({ getTabsData: true });
  420. refresh(tabsData.profileName);
  421. } else {
  422. refresh();
  423. }
  424. async function refresh(profileName) {
  425. const [profiles, rules] = await Promise.all([browser.runtime.sendMessage({ getProfiles: true }), browser.runtime.sendMessage({ getRules: true })]);
  426. const selectedProfileName = profileName || profileNamesInput.value || DEFAULT_PROFILE_NAME;
  427. Array.from(profileNamesInput.childNodes).forEach(node => node.remove());
  428. const profileNames = Object.keys(profiles);
  429. profileNamesInput.options.length = 0;
  430. ruleProfileInput.options.length = 0;
  431. ruleAutoSaveProfileInput.options.length = 0;
  432. ruleEditProfileInput.options.length = 0;
  433. ruleEditAutoSaveProfileInput.options.length = 0;
  434. let optionElement = document.createElement("option");
  435. optionElement.value = DEFAULT_PROFILE_NAME;
  436. optionElement.textContent = browser.i18n.getMessage("profileDefaultSettings");
  437. profileNamesInput.appendChild(optionElement);
  438. ruleProfileInput.appendChild(optionElement.cloneNode(true));
  439. ruleAutoSaveProfileInput.appendChild(optionElement.cloneNode(true));
  440. ruleEditProfileInput.appendChild(optionElement.cloneNode(true));
  441. ruleEditAutoSaveProfileInput.appendChild(optionElement.cloneNode(true));
  442. profileNames.forEach(profileName => {
  443. if (profileName != DEFAULT_PROFILE_NAME) {
  444. const optionElement = document.createElement("option");
  445. optionElement.value = optionElement.textContent = profileName;
  446. profileNamesInput.appendChild(optionElement);
  447. ruleProfileInput.appendChild(optionElement.cloneNode(true));
  448. ruleAutoSaveProfileInput.appendChild(optionElement.cloneNode(true));
  449. ruleEditProfileInput.appendChild(optionElement.cloneNode(true));
  450. ruleEditAutoSaveProfileInput.appendChild(optionElement.cloneNode(true));
  451. }
  452. });
  453. profileNamesInput.disabled = profileNamesInput.options.length == 1;
  454. optionElement = document.createElement("option");
  455. optionElement.value = DISABLED_PROFILE_NAME;
  456. optionElement.textContent = browser.i18n.getMessage("profileDisabled");
  457. ruleAutoSaveProfileInput.appendChild(optionElement);
  458. ruleEditAutoSaveProfileInput.appendChild(optionElement.cloneNode(true));
  459. const rulesDataElement = rulesElement.querySelector(".rules-data");
  460. Array.from(rulesDataElement.childNodes).forEach(node => (!node.className || !node.className.includes("rule-edit")) && node.remove());
  461. const editURLElement = rulesElement.querySelector(".rule-edit");
  462. createURLElement.hidden = false;
  463. editURLElement.hidden = true;
  464. ruleProfileInput.value = ruleAutoSaveProfileInput.value = selectedProfileName;
  465. let rulesDisplayed;
  466. rules.forEach(rule => {
  467. if (showAllProfilesInput.checked || selectedProfileName == rule.profile || selectedProfileName == rule.autoSaveProfile) {
  468. rulesDisplayed = true;
  469. const ruleElement = rulesElement.querySelector(".rule-view").cloneNode(true);
  470. const ruleUrlElement = ruleElement.querySelector(".rule-url");
  471. const ruleProfileElement = ruleElement.querySelector(".rule-profile");
  472. const ruleAutoSaveProfileElement = ruleElement.querySelector(".rule-autosave-profile");
  473. ruleUrlElement.textContent = ruleUrlElement.title = rule.url;
  474. ruleProfileElement.textContent = ruleProfileElement.title = getProfileText(rule.profile);
  475. ruleAutoSaveProfileElement.textContent = ruleAutoSaveProfileElement.title = getProfileText(rule.autoSaveProfile);
  476. ruleElement.hidden = false;
  477. ruleElement.className = "tr data";
  478. rulesDataElement.appendChild(ruleElement);
  479. const ruleDeleteButton = ruleElement.querySelector(".rule-delete-button");
  480. const ruleUpdateButton = ruleElement.querySelector(".rule-update-button");
  481. ruleDeleteButton.title = browser.i18n.getMessage("optionsDeleteRuleTooltip");
  482. ruleDeleteButton.addEventListener("click", async () => {
  483. if (await confirm(browser.i18n.getMessage("optionsDeleteRuleConfirm"))) {
  484. await browser.runtime.sendMessage({ deleteRule: true, url: rule.url });
  485. await refresh();
  486. refreshExternalComponents();
  487. }
  488. }, false);
  489. ruleUpdateButton.title = browser.i18n.getMessage("optionsUpdateRuleTooltip");
  490. ruleUpdateButton.addEventListener("click", async () => {
  491. if (editURLElement.hidden) {
  492. createURLElement.hidden = true;
  493. editURLElement.hidden = false;
  494. rulesDataElement.replaceChild(editURLElement, ruleElement);
  495. ruleEditUrlInput.value = rule.url;
  496. ruleEditProfileInput.value = rule.profile;
  497. ruleEditAutoSaveProfileInput.value = rule.autoSaveProfile;
  498. ruleEditUrlInput.focus();
  499. editURLElement.onsubmit = async event => {
  500. event.preventDefault();
  501. rulesElement.appendChild(editURLElement);
  502. await browser.runtime.sendMessage({ updateRule: true, url: rule.url, newUrl: ruleEditUrlInput.value, profileName: ruleEditProfileInput.value, autoSaveProfileName: ruleEditAutoSaveProfileInput.value });
  503. await refresh();
  504. refreshExternalComponents();
  505. ruleUrlInput.focus();
  506. };
  507. }
  508. }, false);
  509. }
  510. });
  511. rulesDeleteAllButton.disabled = !rulesDisplayed;
  512. rulesElement.appendChild(createURLElement);
  513. profileNamesInput.value = selectedProfileName;
  514. renameProfileButton.disabled = deleteProfileButton.disabled = profileNamesInput.value == DEFAULT_PROFILE_NAME;
  515. const profileOptions = profiles[selectedProfileName];
  516. removeHiddenElementsInput.checked = profileOptions.removeHiddenElements;
  517. removeUnusedStylesInput.checked = profileOptions.removeUnusedStyles;
  518. removeUnusedFontsInput.checked = profileOptions.removeUnusedFonts;
  519. removeFramesInput.checked = profileOptions.removeFrames;
  520. removeImportsInput.checked = profileOptions.removeImports;
  521. removeScriptsInput.checked = profileOptions.removeScripts;
  522. saveRawPageInput.checked = profileOptions.saveRawPage;
  523. compressHTMLInput.checked = profileOptions.compressHTML;
  524. compressCSSInput.checked = profileOptions.compressCSS;
  525. loadDeferredImagesInput.checked = profileOptions.loadDeferredImages;
  526. loadDeferredImagesMaxIdleTimeInput.value = profileOptions.loadDeferredImagesMaxIdleTime;
  527. loadDeferredImagesMaxIdleTimeInput.disabled = !profileOptions.loadDeferredImages;
  528. contextMenuEnabledInput.checked = profileOptions.contextMenuEnabled;
  529. filenameTemplateInput.value = profileOptions.filenameTemplate;
  530. shadowEnabledInput.checked = profileOptions.shadowEnabled;
  531. maxResourceSizeEnabledInput.checked = profileOptions.maxResourceSizeEnabled;
  532. maxResourceSizeInput.value = profileOptions.maxResourceSize;
  533. maxResourceSizeInput.disabled = !profileOptions.maxResourceSizeEnabled;
  534. confirmFilenameInput.checked = profileOptions.confirmFilename;
  535. filenameConflictActionInput.value = profileOptions.filenameConflictAction;
  536. removeAudioSrcInput.checked = profileOptions.removeAudioSrc;
  537. removeVideoSrcInput.checked = profileOptions.removeVideoSrc;
  538. displayInfobarInput.checked = profileOptions.displayInfobar;
  539. displayStatsInput.checked = profileOptions.displayStats;
  540. backgroundSaveInput.checked = profileOptions.backgroundSave;
  541. autoSaveDelayInput.value = profileOptions.autoSaveDelay;
  542. autoSaveDelayInput.disabled = !profileOptions.autoSaveLoadOrUnload && !profileOptions.autoSaveLoad;
  543. autoSaveLoadInput.checked = !profileOptions.autoSaveLoadOrUnload && profileOptions.autoSaveLoad;
  544. autoSaveLoadOrUnloadInput.checked = profileOptions.autoSaveLoadOrUnload;
  545. autoSaveUnloadInput.checked = !profileOptions.autoSaveLoadOrUnload && profileOptions.autoSaveUnload;
  546. autoSaveLoadInput.disabled = profileOptions.autoSaveLoadOrUnload;
  547. autoSaveUnloadInput.disabled = profileOptions.autoSaveLoadOrUnload;
  548. autoSaveRepeatInput.checked = profileOptions.autoSaveRepeat;
  549. autoSaveRepeatInput.disabled = !profileOptions.autoSaveLoadOrUnload && !profileOptions.autoSaveLoad;
  550. autoSaveRepeatDelayInput.value = profileOptions.autoSaveRepeatDelay;
  551. autoSaveRepeatDelayInput.disabled = !profileOptions.autoSaveRepeat;
  552. removeAlternativeFontsInput.checked = profileOptions.removeAlternativeFonts;
  553. removeAlternativeImagesInput.checked = profileOptions.removeAlternativeImages;
  554. groupDuplicateImagesInput.checked = profileOptions.groupDuplicateImages;
  555. removeAlternativeMediasInput.checked = profileOptions.removeAlternativeMedias;
  556. infobarTemplateInput.value = profileOptions.infobarTemplate;
  557. confirmInfobarInput.checked = profileOptions.confirmInfobarContent;
  558. removeFramesInput.disabled = saveRawPageInput.checked;
  559. removeFramesInput.checked = removeFramesInput.checked || saveRawPageInput.checked;
  560. loadDeferredImagesInput.disabled = saveRawPageInput.checked;
  561. loadDeferredImagesMaxIdleTimeInput.disabled = saveRawPageInput.checked;
  562. if (saveRawPageInput.checked) {
  563. loadDeferredImagesInput.checked = false;
  564. }
  565. }
  566. function getProfileText(profileName) {
  567. return profileName == DEFAULT_PROFILE_NAME ? browser.i18n.getMessage("profileDefaultSettings") : profileName == DISABLED_PROFILE_NAME ? browser.i18n.getMessage("profileDisabled") : profileName;
  568. }
  569. async function update() {
  570. await pendingSave;
  571. pendingSave = browser.runtime.sendMessage({
  572. updateProfile: true,
  573. profileName: profileNamesInput.value,
  574. profile: {
  575. removeHiddenElements: removeHiddenElementsInput.checked,
  576. removeUnusedStyles: removeUnusedStylesInput.checked,
  577. removeUnusedFonts: removeUnusedFontsInput.checked,
  578. removeFrames: removeFramesInput.checked,
  579. removeImports: removeImportsInput.checked,
  580. removeScripts: removeScriptsInput.checked,
  581. saveRawPage: saveRawPageInput.checked,
  582. compressHTML: compressHTMLInput.checked,
  583. compressCSS: compressCSSInput.checked,
  584. loadDeferredImages: loadDeferredImagesInput.checked,
  585. loadDeferredImagesMaxIdleTime: Math.max(loadDeferredImagesMaxIdleTimeInput.value, 0),
  586. contextMenuEnabled: contextMenuEnabledInput.checked,
  587. filenameTemplate: filenameTemplateInput.value,
  588. shadowEnabled: shadowEnabledInput.checked,
  589. maxResourceSizeEnabled: maxResourceSizeEnabledInput.checked,
  590. maxResourceSize: Math.max(maxResourceSizeInput.value, 0),
  591. confirmFilename: confirmFilenameInput.checked,
  592. filenameConflictAction: filenameConflictActionInput.value,
  593. removeAudioSrc: removeAudioSrcInput.checked,
  594. removeVideoSrc: removeVideoSrcInput.checked,
  595. displayInfobar: displayInfobarInput.checked,
  596. displayStats: displayStatsInput.checked,
  597. backgroundSave: backgroundSaveInput.checked,
  598. autoSaveDelay: Math.max(autoSaveDelayInput.value, 0),
  599. autoSaveLoad: autoSaveLoadInput.checked,
  600. autoSaveUnload: autoSaveUnloadInput.checked,
  601. autoSaveLoadOrUnload: autoSaveLoadOrUnloadInput.checked,
  602. autoSaveRepeat: autoSaveRepeatInput.checked,
  603. autoSaveRepeatDelay: Math.max(autoSaveRepeatDelayInput.value, 1),
  604. removeAlternativeFonts: removeAlternativeFontsInput.checked,
  605. removeAlternativeImages: removeAlternativeImagesInput.checked,
  606. removeAlternativeMedias: removeAlternativeMediasInput.checked,
  607. groupDuplicateImages: groupDuplicateImagesInput.checked,
  608. infobarTemplate: infobarTemplateInput.value,
  609. confirmInfobarContent: confirmInfobarInput.checked
  610. }
  611. });
  612. await pendingSave;
  613. }
  614. async function refreshExternalComponents() {
  615. await browser.runtime.sendMessage({ refreshMenu: true });
  616. if (sidePanelDisplay) {
  617. await browser.runtime.sendMessage({ refreshOptions: true, profileName: profileNamesInput.value });
  618. } else {
  619. await browser.runtime.sendMessage({ refreshOptionsPanel: true, profileName: profileNamesInput.value });
  620. }
  621. }
  622. async function confirm(message) {
  623. document.getElementById("confirmLabel").textContent = message;
  624. document.getElementById("formConfirmContainer").hidden = false;
  625. confirmButton.focus();
  626. document.body.style.setProperty("overflow-y", "hidden");
  627. return new Promise(resolve => {
  628. confirmButton.onclick = event => hideAndResolve(event, true);
  629. cancelButton.onclick = event => hideAndResolve(event);
  630. window.onkeyup = event => {
  631. if (event.key == "Escape") {
  632. hideAndResolve(event);
  633. }
  634. };
  635. function hideAndResolve(event, value) {
  636. event.preventDefault();
  637. document.getElementById("formConfirmContainer").hidden = true;
  638. document.body.style.setProperty("overflow-y", "");
  639. resolve(value);
  640. }
  641. });
  642. }
  643. async function reset() {
  644. document.getElementById("formResetContainer").hidden = false;
  645. resetCancelButton.focus();
  646. document.body.style.setProperty("overflow-y", "hidden");
  647. return new Promise(resolve => {
  648. resetAllButton.onclick = event => hideAndResolve(event, "all");
  649. resetCurrentButton.onclick = event => hideAndResolve(event, "current");
  650. resetCancelButton.onclick = event => hideAndResolve(event);
  651. window.onkeyup = event => {
  652. if (event.key == "Escape") {
  653. hideAndResolve(event);
  654. }
  655. };
  656. function hideAndResolve(event, value) {
  657. event.preventDefault();
  658. document.getElementById("formResetContainer").hidden = true;
  659. document.body.style.setProperty("overflow-y", "");
  660. resolve(value);
  661. }
  662. });
  663. }
  664. async function prompt(message, defaultValue = "") {
  665. document.getElementById("promptLabel").textContent = message;
  666. document.getElementById("formPromptContainer").hidden = false;
  667. promptInput.value = defaultValue;
  668. promptInput.focus();
  669. document.body.style.setProperty("overflow-y", "hidden");
  670. return new Promise(resolve => {
  671. promptConfirmButton.onclick = event => hideAndResolve(event, promptInput.value);
  672. promptCancelButton.onclick = event => hideAndResolve(event);
  673. window.onkeyup = event => {
  674. if (event.key == "Escape") {
  675. hideAndResolve(event);
  676. }
  677. };
  678. function hideAndResolve(event, value) {
  679. event.preventDefault();
  680. document.getElementById("formPromptContainer").hidden = true;
  681. document.body.style.setProperty("overflow-y", "");
  682. resolve(value);
  683. }
  684. });
  685. }
  686. })();