extra.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 showPW(ev) {
  87. if (ev.buttons || ev.key == ' ' || ev.key.toUpperCase() == 'ENTER') {
  88. const passwordField = document.getElementById(this.getAttribute('data-toggle'));
  89. passwordField.setAttribute('type', 'text');
  90. this.classList.add('active');
  91. }
  92. return false;
  93. }
  94. function hidePW() {
  95. const passwordField = document.getElementById(this.getAttribute('data-toggle'));
  96. passwordField.setAttribute('type', 'password');
  97. this.classList.remove('active');
  98. return false;
  99. }
  100. function init_password_observers() {
  101. document.querySelectorAll('.toggle-password').forEach(function (btn) {
  102. btn.addEventListener('mousedown', showPW);
  103. btn.addEventListener('keydown', showPW);
  104. btn.addEventListener('mouseup', hidePW);
  105. btn.addEventListener('keyup', hidePW);
  106. });
  107. }
  108. function init_select_observers() {
  109. document.querySelectorAll('.select-change').forEach(function (s) {
  110. s.onchange = function (ev) {
  111. const opt = s.options[s.selectedIndex];
  112. const url = opt.getAttribute('data-url');
  113. if (url) {
  114. s.disabled = true;
  115. s.value = '';
  116. if (s.form) {
  117. s.form.querySelectorAll('[type=submit]').forEach(function (b) {
  118. b.disabled = true;
  119. });
  120. }
  121. location.href = url;
  122. }
  123. };
  124. });
  125. }
  126. function init_slider_observers() {
  127. const slider = document.getElementById('slider');
  128. const closer = document.getElementById('close-slider');
  129. if (!slider) {
  130. return;
  131. }
  132. document.querySelector('.post').onclick = function (ev) {
  133. const a = ev.target.closest('.open-slider');
  134. if (a) {
  135. if (!context.ajax_loading) {
  136. context.ajax_loading = true;
  137. const req = new XMLHttpRequest();
  138. req.open('GET', a.href + '&ajax=1', true);
  139. req.responseType = 'document';
  140. req.onload = function (e) {
  141. slider.innerHTML = this.response.body.innerHTML;
  142. slider.classList.add('active');
  143. closer.classList.add('active');
  144. context.ajax_loading = false;
  145. fix_popup_preview_selector();
  146. init_extra();
  147. };
  148. req.send();
  149. return false;
  150. }
  151. }
  152. };
  153. closer.onclick = function (ev) {
  154. if (data_leave_validation() || confirm(context.i18n.confirmation_default)) {
  155. slider.querySelectorAll('form').forEach(function (f) { f.reset(); });
  156. closer.classList.remove('active');
  157. slider.classList.remove('active');
  158. return true;
  159. } else {
  160. return false;
  161. }
  162. };
  163. }
  164. function data_leave_validation() {
  165. const ds = document.querySelectorAll('[data-leave-validation]');
  166. for (let i = ds.length - 1; i >= 0; i--) {
  167. const input = ds[i];
  168. if (input.type === 'checkbox' || input.type === 'radio') {
  169. if (input.checked != input.getAttribute('data-leave-validation')) {
  170. return false;
  171. }
  172. } else if (input.value != input.getAttribute('data-leave-validation')) {
  173. return false;
  174. }
  175. }
  176. return true;
  177. }
  178. function init_configuration_alert() {
  179. window.onsubmit = function (e) {
  180. window.hasSubmit = true;
  181. };
  182. window.onbeforeunload = function (e) {
  183. if (window.hasSubmit) {
  184. return;
  185. }
  186. if (!data_leave_validation()) {
  187. return false;
  188. }
  189. };
  190. }
  191. /**
  192. * Allow a <select class="select-show"> to hide/show elements defined by <option data-show="elem-id"></option>
  193. */
  194. function init_select_show() {
  195. const listener = (select) => {
  196. const options = select.querySelectorAll('option[data-show]');
  197. for (const option of options) {
  198. const elem = document.getElementById(option.dataset.show);
  199. if (elem) {
  200. elem.style.display = option.selected ? 'block' : 'none';
  201. }
  202. }
  203. };
  204. const selects = document.querySelectorAll('select.select-show');
  205. for (const select of selects) {
  206. select.addEventListener('change', (e) => listener(e.target));
  207. listener(select);
  208. }
  209. }
  210. /**
  211. * Automatically validate XPath textarea fields
  212. */
  213. function init_valid_xpath() {
  214. const listener = (textarea) => {
  215. const evaluator = new XPathEvaluator();
  216. try {
  217. if (textarea.value === '' || evaluator.createExpression(textarea.value) != null) {
  218. textarea.setCustomValidity('');
  219. }
  220. } catch (ex) {
  221. textarea.setCustomValidity(ex);
  222. }
  223. };
  224. const textareas = document.querySelectorAll('textarea.valid-xpath');
  225. for (const textarea of textareas) {
  226. textarea.addEventListener('change', (e) => listener(e.target));
  227. listener(textarea);
  228. }
  229. }
  230. function init_extra() {
  231. if (!window.context) {
  232. if (window.console) {
  233. console.log('FreshRSS extra waiting for JS…');
  234. }
  235. window.setTimeout(init_extra, 50); // Wait for all js to be loaded
  236. return;
  237. }
  238. init_crypto_form();
  239. init_password_observers();
  240. init_select_observers();
  241. init_slider_observers();
  242. init_configuration_alert();
  243. fix_popup_preview_selector();
  244. init_select_show();
  245. init_valid_xpath();
  246. }
  247. if (document.readyState && document.readyState !== 'loading') {
  248. init_extra();
  249. } else {
  250. document.addEventListener('DOMContentLoaded', function () {
  251. if (window.console) {
  252. console.log('FreshRSS extra waiting for DOMContentLoaded…');
  253. }
  254. init_extra();
  255. }, false);
  256. }
  257. // @license-end