extra.js 7.3 KB

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