extra.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. // @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-3.0
  2. 'use strict';
  3. /* globals context, openNotification, xmlHttpRequestJson */
  4. // <crypto form (Web login)>
  5. function poormanSalt() { // If crypto.getRandomValues is not available
  6. const base = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789/abcdefghijklmnopqrstuvwxyz';
  7. let text = '$2a$04$';
  8. for (let i = 22; i > 0; i--) {
  9. text += base.charAt(Math.floor(Math.random() * 64));
  10. }
  11. return text;
  12. }
  13. function forgetOpenCategories() {
  14. localStorage.removeItem('FreshRSS_open_categories');
  15. }
  16. function init_crypto_form() {
  17. /* globals dcodeIO */
  18. const crypto_form = document.getElementById('crypto-form');
  19. if (!crypto_form) {
  20. return;
  21. }
  22. if (!(window.dcodeIO)) {
  23. if (window.console) {
  24. console.log('FreshRSS waiting for bcrypt.js…');
  25. }
  26. setTimeout(init_crypto_form, 100);
  27. return;
  28. }
  29. forgetOpenCategories();
  30. const submit_button = document.getElementById('loginButton');
  31. if (submit_button) {
  32. submit_button.disabled = false;
  33. }
  34. crypto_form.onsubmit = function (e) {
  35. if (submit_button) {
  36. submit_button.disabled = true;
  37. }
  38. let success = false;
  39. const req = new XMLHttpRequest();
  40. req.open('GET', './?c=javascript&a=nonce&user=' + document.getElementById('username').value, false);
  41. req.onerror = function () {
  42. openNotification('Communication error!', 'bad');
  43. };
  44. req.send();
  45. if (req.status == 200) {
  46. const json = xmlHttpRequestJson(req);
  47. if (!json.salt1 || !json.nonce) {
  48. openNotification('Invalid user!', 'bad');
  49. } else {
  50. try {
  51. const strong = window.Uint32Array && window.crypto && (typeof window.crypto.getRandomValues === 'function');
  52. const s = dcodeIO.bcrypt.hashSync(document.getElementById('passwordPlain').value, json.salt1);
  53. const c = dcodeIO.bcrypt.hashSync(json.nonce + s, strong ? dcodeIO.bcrypt.genSaltSync(4) : poormanSalt());
  54. document.getElementById('challenge').value = c;
  55. if (!s || !c) {
  56. openNotification('Crypto error!', 'bad');
  57. } else {
  58. success = true;
  59. }
  60. } catch (ex) {
  61. openNotification('Crypto exception! ' + ex, 'bad');
  62. }
  63. }
  64. } else {
  65. req.onerror();
  66. }
  67. if (submit_button) {
  68. submit_button.disabled = false;
  69. }
  70. return success;
  71. };
  72. }
  73. // </crypto form (Web login)>
  74. // <show password>
  75. let timeoutHide;
  76. function showPW_this() {
  77. const id_passwordField = this.getAttribute('data-toggle');
  78. if (this.classList.contains('active')) {
  79. hidePW(id_passwordField);
  80. } else {
  81. showPW(id_passwordField);
  82. }
  83. return false;
  84. }
  85. function showPW(id_passwordField) {
  86. const passwordField = document.getElementById(id_passwordField);
  87. passwordField.setAttribute('type', 'text');
  88. passwordField.nextElementSibling.classList.add('active');
  89. clearTimeout(timeoutHide);
  90. timeoutHide = setTimeout(function () { hidePW(id_passwordField); }, 5000);
  91. return false;
  92. }
  93. function hidePW(id_passwordField) {
  94. clearTimeout(timeoutHide);
  95. const passwordField = document.getElementById(id_passwordField);
  96. passwordField.setAttribute('type', 'password');
  97. passwordField.nextElementSibling.classList.remove('active');
  98. return false;
  99. }
  100. function init_password_observers(parent) {
  101. parent.querySelectorAll('.toggle-password').forEach(function (btn) {
  102. btn.addEventListener('click', showPW_this);
  103. });
  104. }
  105. // </show password>
  106. function init_archiving(parent) {
  107. parent.addEventListener('change', function (e) {
  108. if (e.target.id === 'use_default_purge_options') {
  109. parent.querySelectorAll('.archiving').forEach(function (element) {
  110. element.hidden = e.target.checked;
  111. if (!e.target.checked) element.style.visibility = 'visible'; // Help for Edge 44
  112. });
  113. }
  114. });
  115. parent.addEventListener('click', function (e) {
  116. if (e.target.closest('button[type=reset]')) {
  117. const archiving = document.getElementById('use_default_purge_options');
  118. if (archiving) {
  119. parent.querySelectorAll('.archiving').forEach(function (element) {
  120. element.hidden = archiving.getAttribute('data-leave-validation') == 1;
  121. });
  122. }
  123. }
  124. });
  125. }
  126. // <slider>
  127. const freshrssSliderLoadEvent = new Event('freshrss:slider-load');
  128. function open_slider_listener(ev) {
  129. if (ev.ctrlKey || ev.shiftKey) {
  130. return;
  131. }
  132. const a = ev.target.closest('.open-slider');
  133. if (a) {
  134. if (!context.ajax_loading) {
  135. context.ajax_loading = true;
  136. const slider = document.getElementById('slider');
  137. const slider_content = document.getElementById('slider-content');
  138. const req = new XMLHttpRequest();
  139. slider_content.innerHTML = '';
  140. slider.classList.add('sliding');
  141. const ahref = a.href + '&ajax=1#slider';
  142. req.open('GET', ahref, true);
  143. req.responseType = 'document';
  144. req.onload = function (e) {
  145. location.href = '#slider'; // close menu/dropdown
  146. document.documentElement.classList.add('slider-active');
  147. slider.classList.add('active');
  148. slider.scrollTop = 0;
  149. slider_content.innerHTML = this.response.body.innerHTML;
  150. slider_content.querySelectorAll('form').forEach(function (f) {
  151. f.insertAdjacentHTML('afterbegin', '<input type="hidden" name="slider" value="1" />');
  152. });
  153. context.ajax_loading = false;
  154. slider.dispatchEvent(freshrssSliderLoadEvent);
  155. };
  156. req.send();
  157. return false;
  158. }
  159. }
  160. }
  161. function init_slider(slider) {
  162. window.onclick = open_slider_listener;
  163. document.getElementById('close-slider').addEventListener('click', close_slider_listener);
  164. document.querySelector('#slider .toggle_aside').addEventListener('click', close_slider_listener);
  165. if (slider.children.length > 0) {
  166. slider.dispatchEvent(freshrssSliderLoadEvent);
  167. }
  168. }
  169. function close_slider_listener(ev) {
  170. const slider = document.getElementById('slider');
  171. if (data_leave_validation(slider) || confirm(context.i18n.confirmation_default)) {
  172. slider.querySelectorAll('form').forEach(function (f) { f.reset(); });
  173. document.documentElement.classList.remove('slider-active');
  174. return true;
  175. } else {
  176. return false;
  177. }
  178. }
  179. // </slider>
  180. // overwrites the href attribute from the url input
  181. function updateHref(ev) {
  182. const urlField = document.getElementById(this.getAttribute('data-input'));
  183. const url = urlField.value;
  184. if (url.length > 0) {
  185. this.href = url;
  186. return true;
  187. } else {
  188. urlField.focus();
  189. this.removeAttribute('href');
  190. ev.preventDefault();
  191. return false;
  192. }
  193. }
  194. // set event listener on "show url" buttons
  195. function init_url_observers(parent) {
  196. parent.querySelectorAll('.open-url').forEach(function (btn) {
  197. btn.addEventListener('mouseover', updateHref);
  198. btn.addEventListener('click', updateHref);
  199. });
  200. }
  201. function init_select_observers() {
  202. document.querySelectorAll('.select-change').forEach(function (s) {
  203. s.onchange = function (ev) {
  204. const opt = s.options[s.selectedIndex];
  205. const url = opt.getAttribute('data-url');
  206. if (url) {
  207. s.disabled = true;
  208. s.value = '';
  209. if (s.form) {
  210. s.form.querySelectorAll('[type=submit]').forEach(function (b) {
  211. b.disabled = true;
  212. });
  213. }
  214. location.href = url;
  215. }
  216. };
  217. });
  218. }
  219. /**
  220. * Returns true when no input element is changed, false otherwise.
  221. * When excludeForm is defined, will only report changes outside the specified form.
  222. */
  223. function data_leave_validation(parent, excludeForm = null) {
  224. const ds = parent.querySelectorAll('[data-leave-validation]');
  225. for (let i = ds.length - 1; i >= 0; i--) {
  226. const input = ds[i];
  227. if (excludeForm && excludeForm === input.form) {
  228. continue;
  229. }
  230. if (input.type === 'checkbox' || input.type === 'radio') {
  231. if (input.checked != input.getAttribute('data-leave-validation')) {
  232. return false;
  233. }
  234. } else if (input.value != input.getAttribute('data-leave-validation')) {
  235. return false;
  236. }
  237. }
  238. return true;
  239. }
  240. function init_2stateButton() {
  241. const btns = document.getElementsByClassName('btn-state1');
  242. Array.prototype.forEach.call(btns, function (el) {
  243. el.addEventListener('click', function () {
  244. const btnState2 = document.getElementById(el.dataset.state2Id);
  245. btnState2.classList.add('show');
  246. this.classList.add('hide');
  247. });
  248. });
  249. }
  250. function init_configuration_alert() {
  251. window.onsubmit = function (e) {
  252. window.hasSubmit = data_leave_validation(document.body, e.submitter ? e.submitter.form : null);
  253. };
  254. window.onbeforeunload = function (e) {
  255. if (window.hasSubmit) {
  256. return;
  257. }
  258. if (!data_leave_validation(document.body)) {
  259. return false;
  260. }
  261. };
  262. }
  263. function init_extra_afterDOM() {
  264. if (!window.context) {
  265. if (window.console) {
  266. console.log('FreshRSS extra waiting for JS…');
  267. }
  268. setTimeout(init_extra_afterDOM, 50);
  269. return;
  270. }
  271. if (!['normal', 'global', 'reader'].includes(context.current_view)) {
  272. init_crypto_form();
  273. init_password_observers(document.body);
  274. init_select_observers();
  275. init_configuration_alert();
  276. init_2stateButton();
  277. const slider = document.getElementById('slider');
  278. if (slider) {
  279. slider.addEventListener('freshrss:slider-load', function (e) {
  280. init_password_observers(slider);
  281. });
  282. init_slider(slider);
  283. init_archiving(slider);
  284. init_url_observers(slider);
  285. } else {
  286. init_archiving(document.body);
  287. init_url_observers(document.body);
  288. }
  289. }
  290. if (window.console) {
  291. console.log('FreshRSS extra init done.');
  292. }
  293. }
  294. if (document.readyState && document.readyState !== 'loading') {
  295. init_extra_afterDOM();
  296. } else {
  297. document.addEventListener('DOMContentLoaded', function () {
  298. if (window.console) {
  299. console.log('FreshRSS extra waiting for DOMContentLoaded…');
  300. }
  301. init_extra_afterDOM();
  302. }, false);
  303. }
  304. // @license-end