extra.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 bcrypt */
  18. const crypto_form = document.getElementById('crypto-form');
  19. if (!crypto_form) {
  20. return;
  21. }
  22. if (!(window.bcrypt)) {
  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 = crypto_form.querySelector('[type="submit"]');
  31. if (submit_button) {
  32. submit_button.disabled = false;
  33. }
  34. crypto_form.onsubmit = function (e) {
  35. e.preventDefault();
  36. if (!submit_button) {
  37. return false;
  38. }
  39. submit_button.disabled = true;
  40. const req = new XMLHttpRequest();
  41. req.open('GET', './?c=javascript&a=nonce&user=' + document.getElementById('username').value, true);
  42. req.onerror = function () {
  43. openNotification('Communication error!', 'bad');
  44. submit_button.disabled = false;
  45. };
  46. req.onload = function () {
  47. if (req.status == 200) {
  48. const json = xmlHttpRequestJson(req);
  49. if (!json.salt1 || !json.nonce) {
  50. openNotification('Invalid user!', 'bad');
  51. } else {
  52. try {
  53. const strong = window.Uint32Array && window.crypto && (typeof window.crypto.getRandomValues === 'function');
  54. const s = bcrypt.hashSync(document.getElementById('passwordPlain').value, json.salt1);
  55. const c = bcrypt.hashSync(json.nonce + s, strong ? bcrypt.genSaltSync(4) : poormanSalt());
  56. document.getElementById('challenge').value = c;
  57. if (!s || !c) {
  58. openNotification('Crypto error!', 'bad');
  59. } else {
  60. crypto_form.removeEventListener('submit', crypto_form.onsubmit);
  61. crypto_form.submit();
  62. }
  63. } catch (ex) {
  64. openNotification('Crypto exception! ' + ex, 'bad');
  65. }
  66. }
  67. } else {
  68. req.onerror();
  69. }
  70. submit_button.disabled = false;
  71. };
  72. req.send();
  73. };
  74. }
  75. // </crypto form (Web login)>
  76. // <show password>
  77. let timeoutHide;
  78. function showPW_this() {
  79. const id_passwordField = this.getAttribute('data-toggle');
  80. if (this.classList.contains('active')) {
  81. hidePW(id_passwordField);
  82. } else {
  83. showPW(id_passwordField);
  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. if (ev.ctrlKey || ev.shiftKey) {
  132. return;
  133. }
  134. const a = ev.target.closest('.open-slider');
  135. if (a) {
  136. if (!context.ajax_loading) {
  137. context.ajax_loading = true;
  138. const slider = document.getElementById('slider');
  139. const slider_content = document.getElementById('slider-content');
  140. const req = new XMLHttpRequest();
  141. slider_content.innerHTML = '';
  142. slider.classList.add('sliding');
  143. const ahref = a.href + '&ajax=1#slider';
  144. req.open('GET', ahref, true);
  145. req.responseType = 'document';
  146. req.onload = function (e) {
  147. location.href = '#slider'; // close menu/dropdown
  148. document.documentElement.classList.add('slider-active');
  149. slider.classList.add('active');
  150. slider.scrollTop = 0;
  151. slider_content.innerHTML = this.response.body.innerHTML;
  152. slider_content.querySelectorAll('form').forEach(function (f) {
  153. f.insertAdjacentHTML('afterbegin', '<input type="hidden" name="slider" value="1" />');
  154. });
  155. context.ajax_loading = false;
  156. slider.dispatchEvent(freshrssSliderLoadEvent);
  157. };
  158. req.send();
  159. return false;
  160. }
  161. }
  162. }
  163. function init_slider(slider) {
  164. window.onclick = open_slider_listener;
  165. document.getElementById('close-slider').addEventListener('click', close_slider_listener);
  166. document.querySelector('#slider .toggle_aside').addEventListener('click', close_slider_listener);
  167. if (slider.children.length > 0) {
  168. slider.dispatchEvent(freshrssSliderLoadEvent);
  169. }
  170. }
  171. function close_slider_listener(ev) {
  172. const slider = document.getElementById('slider');
  173. if (data_leave_validation(slider) || confirm(context.i18n.confirmation_default)) {
  174. slider.querySelectorAll('form').forEach(function (f) { f.reset(); });
  175. document.documentElement.classList.remove('slider-active');
  176. return true;
  177. } else {
  178. return false;
  179. }
  180. }
  181. // </slider>
  182. // overwrites the href attribute from the url input
  183. function updateHref(ev) {
  184. const urlField = document.getElementById(this.getAttribute('data-input'));
  185. const url = urlField.value;
  186. if (url.length > 0) {
  187. this.href = url;
  188. return true;
  189. } else {
  190. urlField.focus();
  191. this.removeAttribute('href');
  192. ev.preventDefault();
  193. return false;
  194. }
  195. }
  196. // set event listener on "show url" buttons
  197. function init_url_observers(parent) {
  198. parent.querySelectorAll('.open-url').forEach(function (btn) {
  199. btn.addEventListener('mouseover', updateHref);
  200. btn.addEventListener('click', updateHref);
  201. });
  202. }
  203. function init_select_observers() {
  204. document.querySelectorAll('.select-change').forEach(function (s) {
  205. s.onchange = function (ev) {
  206. const opt = s.options[s.selectedIndex];
  207. const url = opt.getAttribute('data-url');
  208. if (url) {
  209. s.disabled = true;
  210. s.value = '';
  211. if (s.form) {
  212. s.form.querySelectorAll('[type=submit]').forEach(function (b) {
  213. b.disabled = true;
  214. });
  215. }
  216. location.href = url;
  217. }
  218. };
  219. });
  220. }
  221. /**
  222. * Returns true when no input element is changed, false otherwise.
  223. * When excludeForm is defined, will only report changes outside the specified form.
  224. */
  225. function data_leave_validation(parent, excludeForm = null) {
  226. const ds = parent.querySelectorAll('[data-leave-validation]');
  227. for (let i = ds.length - 1; i >= 0; i--) {
  228. const input = ds[i];
  229. if (excludeForm && excludeForm === input.form) {
  230. continue;
  231. }
  232. if (input.type === 'checkbox' || input.type === 'radio') {
  233. if (input.checked != input.getAttribute('data-leave-validation')) {
  234. return false;
  235. }
  236. } else if (input.value != input.getAttribute('data-leave-validation')) {
  237. return false;
  238. }
  239. }
  240. return true;
  241. }
  242. function init_2stateButton() {
  243. const btns = document.getElementsByClassName('btn-state1');
  244. Array.prototype.forEach.call(btns, function (el) {
  245. el.addEventListener('click', function () {
  246. const btnState2 = document.getElementById(el.dataset.state2Id);
  247. btnState2.classList.add('show');
  248. this.classList.add('hide');
  249. });
  250. });
  251. }
  252. function init_configuration_alert() {
  253. window.onsubmit = function (e) {
  254. window.hasSubmit = data_leave_validation(document.body, e.target);
  255. };
  256. window.onbeforeunload = function (e) {
  257. if (window.hasSubmit) {
  258. return;
  259. }
  260. if (!data_leave_validation(document.body)) {
  261. return false;
  262. }
  263. };
  264. }
  265. function init_extra_afterDOM() {
  266. if (!window.context) {
  267. if (window.console) {
  268. console.log('FreshRSS extra waiting for JS…');
  269. }
  270. setTimeout(init_extra_afterDOM, 50);
  271. return;
  272. }
  273. if (!['normal', 'global', 'reader'].includes(context.current_view)) {
  274. init_crypto_form();
  275. init_password_observers(document.body);
  276. init_select_observers();
  277. init_configuration_alert();
  278. init_2stateButton();
  279. const slider = document.getElementById('slider');
  280. if (slider) {
  281. slider.addEventListener('freshrss:slider-load', function (e) {
  282. init_password_observers(slider);
  283. });
  284. init_slider(slider);
  285. init_archiving(slider);
  286. init_url_observers(slider);
  287. } else {
  288. init_archiving(document.body);
  289. init_url_observers(document.body);
  290. }
  291. }
  292. if (window.console) {
  293. console.log('FreshRSS extra init done.');
  294. }
  295. }
  296. if (document.readyState && document.readyState !== 'loading') {
  297. init_extra_afterDOM();
  298. } else {
  299. document.addEventListener('DOMContentLoaded', function () {
  300. if (window.console) {
  301. console.log('FreshRSS extra waiting for DOMContentLoaded…');
  302. }
  303. init_extra_afterDOM();
  304. }, false);
  305. }
  306. // @license-end