extra.js 8.0 KB

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