extra.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-3.0
  2. 'use strict';
  3. /* globals context, openNotification, openPopupWithSource, xmlHttpRequestJson */
  4. function fix_popup_preview_selector() {
  5. const link = document.getElementById('popup-preview-selector');
  6. if (!link) {
  7. return;
  8. }
  9. link.addEventListener('click', function (ev) {
  10. const selector_entries = document.getElementById('path_entries').value;
  11. const href = link.href.replace('selector-token', encodeURIComponent(selector_entries));
  12. openPopupWithSource(href);
  13. ev.preventDefault();
  14. });
  15. }
  16. // <crypto form (Web login)>
  17. function poormanSalt() { // If crypto.getRandomValues is not available
  18. const base = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789/abcdefghijklmnopqrstuvwxyz';
  19. let text = '$2a$04$';
  20. for (let i = 22; i > 0; i--) {
  21. text += base.charAt(Math.floor(Math.random() * 64));
  22. }
  23. return text;
  24. }
  25. function forgetOpenCategories() {
  26. localStorage.removeItem('FreshRSS_open_categories');
  27. }
  28. function init_crypto_form() {
  29. /* globals dcodeIO */
  30. const crypto_form = document.getElementById('crypto-form');
  31. if (!crypto_form) {
  32. return;
  33. }
  34. if (!(window.dcodeIO)) {
  35. if (window.console) {
  36. console.log('FreshRSS waiting for bcrypt.js…');
  37. }
  38. setTimeout(init_crypto_form, 100);
  39. return;
  40. }
  41. forgetOpenCategories();
  42. const submit_button = document.getElementById('loginButton');
  43. submit_button.disabled = false;
  44. crypto_form.onsubmit = function (e) {
  45. submit_button.disabled = true;
  46. let success = false;
  47. const req = new XMLHttpRequest();
  48. req.open('GET', './?c=javascript&a=nonce&user=' + document.getElementById('username').value, false);
  49. req.onerror = function () {
  50. openNotification('Communication error!', 'bad');
  51. };
  52. req.send();
  53. if (req.status == 200) {
  54. const json = xmlHttpRequestJson(req);
  55. if (!json.salt1 || !json.nonce) {
  56. openNotification('Invalid user!', 'bad');
  57. } else {
  58. try {
  59. const strong = window.Uint32Array && window.crypto && (typeof window.crypto.getRandomValues === 'function');
  60. const s = dcodeIO.bcrypt.hashSync(document.getElementById('passwordPlain').value, json.salt1);
  61. const c = dcodeIO.bcrypt.hashSync(json.nonce + s, strong ? dcodeIO.bcrypt.genSaltSync(4) : poormanSalt());
  62. document.getElementById('challenge').value = c;
  63. if (!s || !c) {
  64. openNotification('Crypto error!', 'bad');
  65. } else {
  66. success = true;
  67. }
  68. } catch (ex) {
  69. openNotification('Crypto exception! ' + ex, 'bad');
  70. }
  71. }
  72. } else {
  73. req.onerror();
  74. }
  75. submit_button.disabled = false;
  76. return success;
  77. };
  78. }
  79. // </crypto form (Web login)>
  80. function init_password_observers() {
  81. document.querySelectorAll('.toggle-password').forEach(function (a) {
  82. a.onmousedown = function (ev) {
  83. const passwordField = document.getElementById(this.getAttribute('data-toggle'));
  84. passwordField.setAttribute('type', 'text');
  85. this.classList.add('active');
  86. return false;
  87. };
  88. a.onmouseup = function (ev) {
  89. const passwordField = document.getElementById(this.getAttribute('data-toggle'));
  90. passwordField.setAttribute('type', 'password');
  91. this.classList.remove('active');
  92. return false;
  93. };
  94. });
  95. }
  96. function init_select_observers() {
  97. document.querySelectorAll('.select-change').forEach(function (s) {
  98. s.onchange = function (ev) {
  99. const opt = s.options[s.selectedIndex];
  100. const url = opt.getAttribute('data-url');
  101. if (url) {
  102. s.disabled = true;
  103. s.value = '';
  104. if (s.form) {
  105. s.form.querySelectorAll('[type=submit]').forEach(function (b) {
  106. b.disabled = true;
  107. });
  108. }
  109. location.href = url;
  110. }
  111. };
  112. });
  113. }
  114. function init_slider_observers() {
  115. const slider = document.getElementById('slider');
  116. const closer = document.getElementById('close-slider');
  117. if (!slider) {
  118. return;
  119. }
  120. document.querySelector('.post').onclick = function (ev) {
  121. const a = ev.target.closest('.open-slider');
  122. if (a) {
  123. if (!context.ajax_loading) {
  124. context.ajax_loading = true;
  125. const req = new XMLHttpRequest();
  126. req.open('GET', a.href + '&ajax=1', true);
  127. req.responseType = 'document';
  128. req.onload = function (e) {
  129. slider.innerHTML = this.response.body.innerHTML;
  130. slider.classList.add('active');
  131. closer.classList.add('active');
  132. context.ajax_loading = false;
  133. fix_popup_preview_selector();
  134. };
  135. req.send();
  136. return false;
  137. }
  138. }
  139. };
  140. closer.onclick = function (ev) {
  141. if (data_leave_validation() || confirm(context.i18n.confirmation_default)) {
  142. slider.querySelectorAll('form').forEach(function (f) { f.reset(); });
  143. closer.classList.remove('active');
  144. slider.classList.remove('active');
  145. return true;
  146. } else {
  147. return false;
  148. }
  149. };
  150. }
  151. function data_leave_validation() {
  152. const ds = document.querySelectorAll('[data-leave-validation]');
  153. for (let i = ds.length - 1; i >= 0; i--) {
  154. const input = ds[i];
  155. if (input.type === 'checkbox' || input.type === 'radio') {
  156. if (input.checked != input.getAttribute('data-leave-validation')) {
  157. return false;
  158. }
  159. } else if (input.value != input.getAttribute('data-leave-validation')) {
  160. return false;
  161. }
  162. }
  163. return true;
  164. }
  165. function init_configuration_alert() {
  166. window.onsubmit = function (e) {
  167. window.hasSubmit = true;
  168. };
  169. window.onbeforeunload = function (e) {
  170. if (window.hasSubmit) {
  171. return;
  172. }
  173. if (!data_leave_validation()) {
  174. return false;
  175. }
  176. };
  177. }
  178. function init_extra() {
  179. if (!window.context) {
  180. if (window.console) {
  181. console.log('FreshRSS extra waiting for JS…');
  182. }
  183. window.setTimeout(init_extra, 50); // Wait for all js to be loaded
  184. return;
  185. }
  186. init_crypto_form();
  187. init_password_observers();
  188. init_select_observers();
  189. init_slider_observers();
  190. init_configuration_alert();
  191. fix_popup_preview_selector();
  192. }
  193. if (document.readyState && document.readyState !== 'loading') {
  194. init_extra();
  195. } else {
  196. document.addEventListener('DOMContentLoaded', function () {
  197. if (window.console) {
  198. console.log('FreshRSS extra waiting for DOMContentLoaded…');
  199. }
  200. init_extra();
  201. }, false);
  202. }
  203. // @license-end