extra.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. "use strict";
  2. /* globals context, openNotification, xmlHttpRequestJson */
  3. /* jshint esversion:6, strict:global */
  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 init_crypto_form() {
  14. /* globals dcodeIO */
  15. const crypto_form = document.getElementById('crypto-form');
  16. if (!crypto_form) {
  17. return;
  18. }
  19. if (!(window.dcodeIO)) {
  20. if (window.console) {
  21. console.log('FreshRSS waiting for bcrypt.js…');
  22. }
  23. setTimeout(init_crypto_form, 100);
  24. return;
  25. }
  26. crypto_form.onsubmit = function (e) {
  27. const submit_button = this.querySelector('button[type="submit"]');
  28. submit_button.disabled = true;
  29. let success = false;
  30. const req = new XMLHttpRequest();
  31. req.open('GET', './?c=javascript&a=nonce&user=' + document.getElementById('username').value, false);
  32. req.onerror = function () {
  33. openNotification('Communication error!', 'bad');
  34. };
  35. req.send();
  36. if (req.status == 200) {
  37. const json = xmlHttpRequestJson(req);
  38. if (!json.salt1 || !json.nonce) {
  39. openNotification('Invalid user!', 'bad');
  40. } else {
  41. try {
  42. const strong = window.Uint32Array && window.crypto && (typeof window.crypto.getRandomValues === 'function'),
  43. s = dcodeIO.bcrypt.hashSync(document.getElementById('passwordPlain').value, json.salt1),
  44. c = dcodeIO.bcrypt.hashSync(json.nonce + s, strong ? dcodeIO.bcrypt.genSaltSync(4) : poormanSalt());
  45. document.getElementById('challenge').value = c;
  46. if (!s || !c) {
  47. openNotification('Crypto error!', 'bad');
  48. } else {
  49. success = true;
  50. }
  51. } catch (ex) {
  52. openNotification('Crypto exception! ' + ex, 'bad');
  53. }
  54. }
  55. } else {
  56. req.onerror();
  57. }
  58. submit_button.disabled = false;
  59. return success;
  60. };
  61. }
  62. //</crypto form (Web login)>
  63. function init_share_observers() {
  64. let shares = document.querySelectorAll('.group-share').length;
  65. const shareAdd = document.querySelector('.share.add');
  66. if (shareAdd) {
  67. shareAdd.onclick = function (ev) {
  68. const s = this.parentElement.querySelector('select'),
  69. opt = s.options[s.selectedIndex];
  70. let row = this.closest('form').getAttribute('data-' + opt.getAttribute('data-form'));
  71. row = row.replace(/##label##/g, opt.text);
  72. row = row.replace(/##type##/g, opt.value);
  73. row = row.replace(/##help##/g, opt.getAttribute('data-help'));
  74. row = row.replace(/##key##/g, shares);
  75. row = row.replace(/##method##/g, opt.getAttribute('data-method'));
  76. row = row.replace(/##field##/g, opt.getAttribute('data-field'));
  77. this.closest('.form-group').insertAdjacentHTML('beforebegin', row);
  78. shares++;
  79. return false;
  80. };
  81. }
  82. }
  83. function init_remove_observers() {
  84. document.querySelectorAll('.post').forEach(function (div) {
  85. div.onclick = function (ev) {
  86. const a = ev.target.closest('a.remove');
  87. if (a) {
  88. const remove_what = a.getAttribute('data-remove');
  89. if (remove_what !== undefined) {
  90. const d = document.getElementById(remove_what);
  91. if (d) {
  92. d.remove();
  93. }
  94. }
  95. return false;
  96. }
  97. };
  98. });
  99. }
  100. function init_feed_observers() {
  101. const s = document.getElementById('category');
  102. if (s && s.matches('select')) {
  103. s.onchange = function (ev) {
  104. const detail = document.getElementById('new_category_name').parentElement;
  105. if (this.value === 'nc') {
  106. detail.setAttribute('aria-hidden', 'false');
  107. detail.querySelector('input').focus();
  108. } else {
  109. detail.setAttribute('aria-hidden', 'true');
  110. }
  111. };
  112. }
  113. }
  114. function init_password_observers() {
  115. document.querySelectorAll('.toggle-password').forEach(function (a) {
  116. a.onmousedown = function (ev) {
  117. const passwordField = document.getElementById(this.getAttribute('data-toggle'));
  118. passwordField.setAttribute('type', 'text');
  119. this.classList.add('active');
  120. return false;
  121. };
  122. a.onmouseup = function (ev) {
  123. const passwordField = document.getElementById(this.getAttribute('data-toggle'));
  124. passwordField.setAttribute('type', 'password');
  125. this.classList.remove('active');
  126. return false;
  127. };
  128. });
  129. }
  130. function init_select_observers() {
  131. document.querySelectorAll('.select-change').forEach(function (s) {
  132. s.onchange = function (ev) {
  133. const opt = s.options[s.selectedIndex];
  134. location.href = opt.getAttribute('data-url');
  135. };
  136. });
  137. }
  138. function init_slider_observers() {
  139. const slider = document.getElementById('slider'),
  140. closer = document.getElementById('close-slider');
  141. if (!slider) {
  142. return;
  143. }
  144. document.querySelector('.post').onclick = function (ev) {
  145. const a = ev.target.closest('.open-slider');
  146. if (a) {
  147. if (!context.ajax_loading) {
  148. context.ajax_loading = true;
  149. const req = new XMLHttpRequest();
  150. req.open('GET', a.href + '&ajax=1', true);
  151. req.responseType = 'document';
  152. req.onload = function (e) {
  153. slider.innerHTML = this.response.body.innerHTML;
  154. slider.classList.add('active');
  155. closer.classList.add('active');
  156. context.ajax_loading = false;
  157. };
  158. req.send();
  159. return false;
  160. }
  161. }
  162. };
  163. closer.onclick = function (ev) {
  164. closer.classList.remove('active');
  165. slider.classList.remove('active');
  166. return false;
  167. };
  168. }
  169. function init_configuration_alert() {
  170. window.onsubmit = function (e) {
  171. window.hasSubmit = true;
  172. };
  173. window.onbeforeunload = function (e) {
  174. if (window.hasSubmit) {
  175. return;
  176. }
  177. const ds = document.querySelectorAll('[data-leave-validation]');
  178. for (let i = ds.length - 1; i >= 0; i--) {
  179. const input = ds[i];
  180. if (input.type === 'checkbox' || input.type === 'radio') {
  181. if (input.checked != input.getAttribute('data-leave-validation')) {
  182. return false;
  183. }
  184. } else if (input.value != input.getAttribute('data-leave-validation')) {
  185. return false;
  186. }
  187. }
  188. };
  189. }
  190. function init_extra() {
  191. if (!window.context) {
  192. if (window.console) {
  193. console.log('FreshRSS extra waiting for JS…');
  194. }
  195. window.setTimeout(init_extra, 50); //Wait for all js to be loaded
  196. return;
  197. }
  198. init_crypto_form();
  199. init_share_observers();
  200. init_remove_observers();
  201. init_feed_observers();
  202. init_password_observers();
  203. init_select_observers();
  204. init_slider_observers();
  205. init_configuration_alert();
  206. }
  207. if (document.readyState && document.readyState !== 'loading') {
  208. init_extra();
  209. } else {
  210. document.addEventListener('DOMContentLoaded', function () {
  211. if (window.console) {
  212. console.log('FreshRSS extra waiting for DOMContentLoaded…');
  213. }
  214. init_extra();
  215. }, false);
  216. }