ui-options.js 38 KB

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