extra.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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(ev) {
  77. const id_passwordField = this.getAttribute('data-toggle');
  78. if (this.classList.contains('active')) {
  79. hidePW(id_passwordField);
  80. } else {
  81. if (ev.type === 'click' || ev.buttons || ev.key === ' ' || ev.key.toUpperCase() === 'ENTER') {
  82. showPW(id_passwordField);
  83. }
  84. }
  85. return false;
  86. }
  87. function showPW(id_passwordField) {
  88. const passwordField = document.getElementById(id_passwordField);
  89. passwordField.setAttribute('type', 'text');
  90. passwordField.nextElementSibling.classList.add('active');
  91. clearTimeout(timeoutHide);
  92. timeoutHide = setTimeout(function () { hidePW(id_passwordField); }, 5000);
  93. return false;
  94. }
  95. function hidePW(id_passwordField) {
  96. clearTimeout(timeoutHide);
  97. const passwordField = document.getElementById(id_passwordField);
  98. passwordField.setAttribute('type', 'password');
  99. passwordField.nextElementSibling.classList.remove('active');
  100. return false;
  101. }
  102. function init_password_observers(parent) {
  103. parent.querySelectorAll('.toggle-password').forEach(function (btn) {
  104. btn.addEventListener('click', showPW_this);
  105. });
  106. }
  107. // </show password>
  108. function init_archiving(parent) {
  109. parent.addEventListener('change', function (e) {
  110. if (e.target.id === 'use_default_purge_options') {
  111. parent.querySelectorAll('.archiving').forEach(function (element) {
  112. element.hidden = e.target.checked;
  113. if (!e.target.checked) element.style.visibility = 'visible'; // Help for Edge 44
  114. });
  115. }
  116. });
  117. parent.addEventListener('click', function (e) {
  118. if (e.target.closest('button[type=reset]')) {
  119. const archiving = document.getElementById('use_default_purge_options');
  120. if (archiving) {
  121. parent.querySelectorAll('.archiving').forEach(function (element) {
  122. element.hidden = archiving.getAttribute('data-leave-validation') == 1;
  123. });
  124. }
  125. }
  126. });
  127. }
  128. // <slider>
  129. const freshrssSliderLoadEvent = new Event('freshrss:slider-load');
  130. function open_slider_listener(ev) {
  131. const a = ev.target.closest('.open-slider');
  132. if (a) {
  133. if (!context.ajax_loading) {
  134. location.href = '#slider'; // close menu/dropdown
  135. context.ajax_loading = true;
  136. const req = new XMLHttpRequest();
  137. req.open('GET', a.href + '&ajax=1', true);
  138. req.responseType = 'document';
  139. req.onload = function (e) {
  140. const slider = document.getElementById('slider');
  141. slider.scrollTop = 0;
  142. slider.innerHTML = this.response.body.innerHTML;
  143. context.ajax_loading = false;
  144. slider.dispatchEvent(freshrssSliderLoadEvent);
  145. };
  146. req.send();
  147. return false;
  148. }
  149. }
  150. }
  151. function init_slider(slider) {
  152. window.onclick = open_slider_listener;
  153. const closer = document.getElementById('close-slider');
  154. closer.addEventListener('click', function (ev) {
  155. if (data_leave_validation(slider) || confirm(context.i18n.confirmation_default)) {
  156. slider.querySelectorAll('form').forEach(function (f) { f.reset(); });
  157. return true;
  158. } else {
  159. return false;
  160. }
  161. });
  162. if (slider.children.length > 0) {
  163. slider.dispatchEvent(freshrssSliderLoadEvent);
  164. }
  165. }
  166. // </slider>
  167. // overwrites the href attribute from the url input
  168. function updateHref(ev) {
  169. const urlField = document.getElementById(this.getAttribute('data-input'));
  170. const url = urlField.value;
  171. if (url.length > 0) {
  172. this.href = url;
  173. return true;
  174. } else {
  175. urlField.focus();
  176. this.removeAttribute('href');
  177. ev.preventDefault();
  178. return false;
  179. }
  180. }
  181. // set event listener on "show url" buttons
  182. function init_url_observers() {
  183. document.querySelectorAll('.open-url').forEach(function (btn) {
  184. btn.addEventListener('mouseover', updateHref);
  185. btn.addEventListener('click', updateHref);
  186. });
  187. }
  188. function init_select_observers() {
  189. document.querySelectorAll('.select-change').forEach(function (s) {
  190. s.onchange = function (ev) {
  191. const opt = s.options[s.selectedIndex];
  192. const url = opt.getAttribute('data-url');
  193. if (url) {
  194. s.disabled = true;
  195. s.value = '';
  196. if (s.form) {
  197. s.form.querySelectorAll('[type=submit]').forEach(function (b) {
  198. b.disabled = true;
  199. });
  200. }
  201. location.href = url;
  202. }
  203. };
  204. });
  205. }
  206. /**
  207. * Returns true when no input element is changed, false otherwise.
  208. * When excludeForm is defined, will only report changes outside the specified form.
  209. */
  210. function data_leave_validation(parent, excludeForm = null) {
  211. const ds = parent.querySelectorAll('[data-leave-validation]');
  212. for (let i = ds.length - 1; i >= 0; i--) {
  213. const input = ds[i];
  214. if (excludeForm && excludeForm === input.form) {
  215. continue;
  216. }
  217. if (input.type === 'checkbox' || input.type === 'radio') {
  218. if (input.checked != input.getAttribute('data-leave-validation')) {
  219. return false;
  220. }
  221. } else if (input.value != input.getAttribute('data-leave-validation')) {
  222. return false;
  223. }
  224. }
  225. return true;
  226. }
  227. function init_configuration_alert() {
  228. window.onsubmit = function (e) {
  229. window.hasSubmit = data_leave_validation(document.body, e.submitter ? e.submitter.form : null);
  230. };
  231. window.onbeforeunload = function (e) {
  232. if (window.hasSubmit) {
  233. return;
  234. }
  235. if (!data_leave_validation(document.body)) {
  236. return false;
  237. }
  238. };
  239. }
  240. function init_extra_afterDOM() {
  241. if (!window.context) {
  242. if (window.console) {
  243. console.log('FreshRSS extra waiting for JS…');
  244. }
  245. setTimeout(init_extra_afterDOM, 50);
  246. return;
  247. }
  248. if (!['normal', 'global', 'reader'].includes(context.current_view)) {
  249. init_crypto_form();
  250. init_password_observers(document.body);
  251. init_url_observers();
  252. init_select_observers();
  253. init_configuration_alert();
  254. const slider = document.getElementById('slider');
  255. if (slider) {
  256. init_slider(slider);
  257. init_archiving(slider);
  258. } else {
  259. init_archiving(document.body);
  260. }
  261. }
  262. if (window.console) {
  263. console.log('FreshRSS extra init done.');
  264. }
  265. }
  266. if (document.readyState && document.readyState !== 'loading') {
  267. init_extra_afterDOM();
  268. } else {
  269. document.addEventListener('DOMContentLoaded', function () {
  270. if (window.console) {
  271. console.log('FreshRSS extra waiting for DOMContentLoaded…');
  272. }
  273. init_extra_afterDOM();
  274. }, false);
  275. }
  276. // @license-end