extra.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. if (submit_button) {
  44. submit_button.disabled = false;
  45. }
  46. crypto_form.onsubmit = function (e) {
  47. if (submit_button) {
  48. submit_button.disabled = true;
  49. }
  50. let success = false;
  51. const req = new XMLHttpRequest();
  52. req.open('GET', './?c=javascript&a=nonce&user=' + document.getElementById('username').value, false);
  53. req.onerror = function () {
  54. openNotification('Communication error!', 'bad');
  55. };
  56. req.send();
  57. if (req.status == 200) {
  58. const json = xmlHttpRequestJson(req);
  59. if (!json.salt1 || !json.nonce) {
  60. openNotification('Invalid user!', 'bad');
  61. } else {
  62. try {
  63. const strong = window.Uint32Array && window.crypto && (typeof window.crypto.getRandomValues === 'function');
  64. const s = dcodeIO.bcrypt.hashSync(document.getElementById('passwordPlain').value, json.salt1);
  65. const c = dcodeIO.bcrypt.hashSync(json.nonce + s, strong ? dcodeIO.bcrypt.genSaltSync(4) : poormanSalt());
  66. document.getElementById('challenge').value = c;
  67. if (!s || !c) {
  68. openNotification('Crypto error!', 'bad');
  69. } else {
  70. success = true;
  71. }
  72. } catch (ex) {
  73. openNotification('Crypto exception! ' + ex, 'bad');
  74. }
  75. }
  76. } else {
  77. req.onerror();
  78. }
  79. if (submit_button) {
  80. submit_button.disabled = false;
  81. }
  82. return success;
  83. };
  84. }
  85. // </crypto form (Web login)>
  86. function init_password_observers() {
  87. document.querySelectorAll('.toggle-password').forEach(function (a) {
  88. a.onmousedown = function (ev) {
  89. const passwordField = document.getElementById(this.getAttribute('data-toggle'));
  90. passwordField.setAttribute('type', 'text');
  91. this.classList.add('active');
  92. return false;
  93. };
  94. a.onmouseup = function (ev) {
  95. const passwordField = document.getElementById(this.getAttribute('data-toggle'));
  96. passwordField.setAttribute('type', 'password');
  97. this.classList.remove('active');
  98. return false;
  99. };
  100. });
  101. }
  102. function init_select_observers() {
  103. document.querySelectorAll('.select-change').forEach(function (s) {
  104. s.onchange = function (ev) {
  105. const opt = s.options[s.selectedIndex];
  106. const url = opt.getAttribute('data-url');
  107. if (url) {
  108. s.disabled = true;
  109. s.value = '';
  110. if (s.form) {
  111. s.form.querySelectorAll('[type=submit]').forEach(function (b) {
  112. b.disabled = true;
  113. });
  114. }
  115. location.href = url;
  116. }
  117. };
  118. });
  119. }
  120. function init_slider_observers() {
  121. const slider = document.getElementById('slider');
  122. const closer = document.getElementById('close-slider');
  123. if (!slider) {
  124. return;
  125. }
  126. document.querySelector('.post').onclick = function (ev) {
  127. const a = ev.target.closest('.open-slider');
  128. if (a) {
  129. if (!context.ajax_loading) {
  130. context.ajax_loading = true;
  131. const req = new XMLHttpRequest();
  132. req.open('GET', a.href + '&ajax=1', true);
  133. req.responseType = 'document';
  134. req.onload = function (e) {
  135. slider.innerHTML = this.response.body.innerHTML;
  136. slider.classList.add('active');
  137. closer.classList.add('active');
  138. context.ajax_loading = false;
  139. fix_popup_preview_selector();
  140. init_extra();
  141. };
  142. req.send();
  143. return false;
  144. }
  145. }
  146. };
  147. closer.onclick = function (ev) {
  148. if (data_leave_validation() || confirm(context.i18n.confirmation_default)) {
  149. slider.querySelectorAll('form').forEach(function (f) { f.reset(); });
  150. closer.classList.remove('active');
  151. slider.classList.remove('active');
  152. return true;
  153. } else {
  154. return false;
  155. }
  156. };
  157. }
  158. function data_leave_validation() {
  159. const ds = document.querySelectorAll('[data-leave-validation]');
  160. for (let i = ds.length - 1; i >= 0; i--) {
  161. const input = ds[i];
  162. if (input.type === 'checkbox' || input.type === 'radio') {
  163. if (input.checked != input.getAttribute('data-leave-validation')) {
  164. return false;
  165. }
  166. } else if (input.value != input.getAttribute('data-leave-validation')) {
  167. return false;
  168. }
  169. }
  170. return true;
  171. }
  172. function init_configuration_alert() {
  173. window.onsubmit = function (e) {
  174. window.hasSubmit = true;
  175. };
  176. window.onbeforeunload = function (e) {
  177. if (window.hasSubmit) {
  178. return;
  179. }
  180. if (!data_leave_validation()) {
  181. return false;
  182. }
  183. };
  184. }
  185. function init_extra() {
  186. if (!window.context) {
  187. if (window.console) {
  188. console.log('FreshRSS extra waiting for JS…');
  189. }
  190. window.setTimeout(init_extra, 50); // Wait for all js to be loaded
  191. return;
  192. }
  193. init_crypto_form();
  194. init_password_observers();
  195. init_select_observers();
  196. init_slider_observers();
  197. init_configuration_alert();
  198. fix_popup_preview_selector();
  199. }
  200. if (document.readyState && document.readyState !== 'loading') {
  201. init_extra();
  202. } else {
  203. document.addEventListener('DOMContentLoaded', function () {
  204. if (window.console) {
  205. console.log('FreshRSS extra waiting for DOMContentLoaded…');
  206. }
  207. init_extra();
  208. }, false);
  209. }
  210. // @license-end