extra.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. url = opt.getAttribute('data-url');
  135. if (url) {
  136. s.form.querySelectorAll('[type=submit]').forEach(function (b) {
  137. b.disabled = true;
  138. });
  139. location.href = url;
  140. }
  141. };
  142. });
  143. }
  144. function init_slider_observers() {
  145. const slider = document.getElementById('slider'),
  146. closer = document.getElementById('close-slider');
  147. if (!slider) {
  148. return;
  149. }
  150. document.querySelector('.post').onclick = function (ev) {
  151. const a = ev.target.closest('.open-slider');
  152. if (a) {
  153. if (!context.ajax_loading) {
  154. context.ajax_loading = true;
  155. const req = new XMLHttpRequest();
  156. req.open('GET', a.href + '&ajax=1', true);
  157. req.responseType = 'document';
  158. req.onload = function (e) {
  159. slider.innerHTML = this.response.body.innerHTML;
  160. slider.classList.add('active');
  161. closer.classList.add('active');
  162. context.ajax_loading = false;
  163. };
  164. req.send();
  165. return false;
  166. }
  167. }
  168. };
  169. closer.onclick = function (ev) {
  170. closer.classList.remove('active');
  171. slider.classList.remove('active');
  172. return false;
  173. };
  174. }
  175. function init_configuration_alert() {
  176. window.onsubmit = function (e) {
  177. window.hasSubmit = true;
  178. };
  179. window.onbeforeunload = function (e) {
  180. if (window.hasSubmit) {
  181. return;
  182. }
  183. const ds = document.querySelectorAll('[data-leave-validation]');
  184. for (let i = ds.length - 1; i >= 0; i--) {
  185. const input = ds[i];
  186. if (input.type === 'checkbox' || input.type === 'radio') {
  187. if (input.checked != input.getAttribute('data-leave-validation')) {
  188. return false;
  189. }
  190. } else if (input.value != input.getAttribute('data-leave-validation')) {
  191. return false;
  192. }
  193. }
  194. };
  195. }
  196. function init_extra() {
  197. if (!window.context) {
  198. if (window.console) {
  199. console.log('FreshRSS extra waiting for JS…');
  200. }
  201. window.setTimeout(init_extra, 50); //Wait for all js to be loaded
  202. return;
  203. }
  204. init_crypto_form();
  205. init_share_observers();
  206. init_remove_observers();
  207. init_feed_observers();
  208. init_password_observers();
  209. init_select_observers();
  210. init_slider_observers();
  211. init_configuration_alert();
  212. }
  213. if (document.readyState && document.readyState !== 'loading') {
  214. init_extra();
  215. } else {
  216. document.addEventListener('DOMContentLoaded', function () {
  217. if (window.console) {
  218. console.log('FreshRSS extra waiting for DOMContentLoaded…');
  219. }
  220. init_extra();
  221. }, false);
  222. }