extra.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. // @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-3.0
  2. 'use strict';
  3. /* globals context, openNotification, xmlHttpRequestJson */
  4. // <crypto form (Web login)>
  5. function poormanSalt() { // If crypto.getRandomValues is not available
  6. const base = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789/abcdefghijklmnopqrstuvwxyz';
  7. let text = '$2a$04$';
  8. for (let i = 22; i > 0; i--) {
  9. text += base.charAt(Math.floor(Math.random() * 64));
  10. }
  11. return text;
  12. }
  13. function forgetOpenCategories() {
  14. localStorage.removeItem('FreshRSS_open_categories');
  15. }
  16. function init_crypto_form() {
  17. /* globals dcodeIO */
  18. const crypto_form = document.getElementById('crypto-form');
  19. if (!crypto_form) {
  20. return;
  21. }
  22. if (!(window.dcodeIO)) {
  23. if (window.console) {
  24. console.log('FreshRSS waiting for bcrypt.js…');
  25. }
  26. setTimeout(init_crypto_form, 100);
  27. return;
  28. }
  29. forgetOpenCategories();
  30. const submit_button = document.getElementById('loginButton');
  31. if (submit_button) {
  32. submit_button.disabled = false;
  33. }
  34. crypto_form.onsubmit = function (e) {
  35. e.preventDefault();
  36. if (!submit_button) {
  37. return false;
  38. }
  39. submit_button.disabled = true;
  40. if (document.getElementById('challenge').value) {
  41. // Already computed
  42. return true;
  43. }
  44. const req = new XMLHttpRequest();
  45. req.open('GET', './?c=javascript&a=nonce&user=' + document.getElementById('username').value, true);
  46. req.onerror = function () {
  47. openNotification('Communication error!', 'bad');
  48. submit_button.disabled = false;
  49. };
  50. req.onload = function () {
  51. if (req.status == 200) {
  52. const json = xmlHttpRequestJson(req);
  53. if (!json.salt1 || !json.nonce) {
  54. openNotification('Invalid user!', 'bad');
  55. } else {
  56. try {
  57. const strong = window.Uint32Array && window.crypto && (typeof window.crypto.getRandomValues === 'function');
  58. const s = dcodeIO.bcrypt.hashSync(document.getElementById('passwordPlain').value, json.salt1);
  59. const c = dcodeIO.bcrypt.hashSync(json.nonce + s, strong ? dcodeIO.bcrypt.genSaltSync(4) : poormanSalt());
  60. document.getElementById('challenge').value = c;
  61. if (!s || !c) {
  62. openNotification('Crypto error!', 'bad');
  63. } else {
  64. crypto_form.removeEventListener('submit', crypto_form.onsubmit);
  65. crypto_form.submit();
  66. }
  67. } catch (ex) {
  68. openNotification('Crypto exception! ' + ex, 'bad');
  69. }
  70. }
  71. } else {
  72. req.onerror();
  73. }
  74. };
  75. req.send();
  76. };
  77. }
  78. // </crypto form (Web login)>
  79. // <show password>
  80. let timeoutHide;
  81. function showPW_this() {
  82. const id_passwordField = this.getAttribute('data-toggle');
  83. if (this.classList.contains('active')) {
  84. hidePW(id_passwordField);
  85. } else {
  86. showPW(id_passwordField);
  87. }
  88. return false;
  89. }
  90. function showPW(id_passwordField) {
  91. const passwordField = document.getElementById(id_passwordField);
  92. passwordField.setAttribute('type', 'text');
  93. passwordField.nextElementSibling.classList.add('active');
  94. clearTimeout(timeoutHide);
  95. timeoutHide = setTimeout(function () { hidePW(id_passwordField); }, 5000);
  96. return false;
  97. }
  98. function hidePW(id_passwordField) {
  99. clearTimeout(timeoutHide);
  100. const passwordField = document.getElementById(id_passwordField);
  101. passwordField.setAttribute('type', 'password');
  102. passwordField.nextElementSibling.classList.remove('active');
  103. return false;
  104. }
  105. function init_password_observers(parent) {
  106. parent.querySelectorAll('.toggle-password').forEach(function (btn) {
  107. btn.addEventListener('click', showPW_this);
  108. });
  109. }
  110. // </show password>
  111. function init_archiving(parent) {
  112. parent.addEventListener('change', function (e) {
  113. if (e.target.id === 'use_default_purge_options') {
  114. parent.querySelectorAll('.archiving').forEach(function (element) {
  115. element.hidden = e.target.checked;
  116. if (!e.target.checked) element.style.visibility = 'visible'; // Help for Edge 44
  117. });
  118. }
  119. });
  120. parent.addEventListener('click', function (e) {
  121. if (e.target.closest('button[type=reset]')) {
  122. const archiving = document.getElementById('use_default_purge_options');
  123. if (archiving) {
  124. parent.querySelectorAll('.archiving').forEach(function (element) {
  125. element.hidden = archiving.getAttribute('data-leave-validation') == 1;
  126. });
  127. }
  128. }
  129. });
  130. }
  131. // <slider>
  132. const freshrssSliderLoadEvent = new Event('freshrss:slider-load');
  133. function open_slider_listener(ev) {
  134. if (ev.ctrlKey || ev.shiftKey) {
  135. return;
  136. }
  137. const a = ev.target.closest('.open-slider');
  138. if (a) {
  139. if (!context.ajax_loading) {
  140. context.ajax_loading = true;
  141. const slider = document.getElementById('slider');
  142. const slider_content = document.getElementById('slider-content');
  143. const req = new XMLHttpRequest();
  144. slider_content.innerHTML = '';
  145. slider.classList.add('sliding');
  146. const ahref = a.href + '&ajax=1#slider';
  147. req.open('GET', ahref, true);
  148. req.responseType = 'document';
  149. req.onload = function (e) {
  150. location.href = '#slider'; // close menu/dropdown
  151. document.documentElement.classList.add('slider-active');
  152. slider.classList.add('active');
  153. slider.scrollTop = 0;
  154. slider_content.innerHTML = this.response.body.innerHTML;
  155. slider_content.querySelectorAll('form').forEach(function (f) {
  156. f.insertAdjacentHTML('afterbegin', '<input type="hidden" name="slider" value="1" />');
  157. });
  158. context.ajax_loading = false;
  159. slider.dispatchEvent(freshrssSliderLoadEvent);
  160. };
  161. req.send();
  162. return false;
  163. }
  164. }
  165. }
  166. function init_slider(slider) {
  167. window.onclick = open_slider_listener;
  168. document.getElementById('close-slider').addEventListener('click', close_slider_listener);
  169. document.querySelector('#slider .toggle_aside').addEventListener('click', close_slider_listener);
  170. if (slider.children.length > 0) {
  171. slider.dispatchEvent(freshrssSliderLoadEvent);
  172. }
  173. }
  174. function close_slider_listener(ev) {
  175. const slider = document.getElementById('slider');
  176. if (data_leave_validation(slider) || confirm(context.i18n.confirmation_default)) {
  177. slider.querySelectorAll('form').forEach(function (f) { f.reset(); });
  178. document.documentElement.classList.remove('slider-active');
  179. return true;
  180. } else {
  181. return false;
  182. }
  183. }
  184. // </slider>
  185. // overwrites the href attribute from the url input
  186. function updateHref(ev) {
  187. const urlField = document.getElementById(this.getAttribute('data-input'));
  188. const url = urlField.value;
  189. if (url.length > 0) {
  190. this.href = url;
  191. return true;
  192. } else {
  193. urlField.focus();
  194. this.removeAttribute('href');
  195. ev.preventDefault();
  196. return false;
  197. }
  198. }
  199. // set event listener on "show url" buttons
  200. function init_url_observers(parent) {
  201. parent.querySelectorAll('.open-url').forEach(function (btn) {
  202. btn.addEventListener('mouseover', updateHref);
  203. btn.addEventListener('click', updateHref);
  204. });
  205. }
  206. function init_select_observers() {
  207. document.querySelectorAll('.select-change').forEach(function (s) {
  208. s.onchange = function (ev) {
  209. const opt = s.options[s.selectedIndex];
  210. const url = opt.getAttribute('data-url');
  211. if (url) {
  212. s.disabled = true;
  213. s.value = '';
  214. if (s.form) {
  215. s.form.querySelectorAll('[type=submit]').forEach(function (b) {
  216. b.disabled = true;
  217. });
  218. }
  219. location.href = url;
  220. }
  221. };
  222. });
  223. }
  224. /**
  225. * Returns true when no input element is changed, false otherwise.
  226. * When excludeForm is defined, will only report changes outside the specified form.
  227. */
  228. function data_leave_validation(parent, excludeForm = null) {
  229. const ds = parent.querySelectorAll('[data-leave-validation]');
  230. for (let i = ds.length - 1; i >= 0; i--) {
  231. const input = ds[i];
  232. if (excludeForm && excludeForm === input.form) {
  233. continue;
  234. }
  235. if (input.type === 'checkbox' || input.type === 'radio') {
  236. if (input.checked != input.getAttribute('data-leave-validation')) {
  237. return false;
  238. }
  239. } else if (input.value != input.getAttribute('data-leave-validation')) {
  240. return false;
  241. }
  242. }
  243. return true;
  244. }
  245. function init_2stateButton() {
  246. const btns = document.getElementsByClassName('btn-state1');
  247. Array.prototype.forEach.call(btns, function (el) {
  248. el.addEventListener('click', function () {
  249. const btnState2 = document.getElementById(el.dataset.state2Id);
  250. btnState2.classList.add('show');
  251. this.classList.add('hide');
  252. });
  253. });
  254. }
  255. function init_configuration_alert() {
  256. window.onsubmit = function (e) {
  257. window.hasSubmit = data_leave_validation(document.body, e.target);
  258. };
  259. window.onbeforeunload = function (e) {
  260. if (window.hasSubmit) {
  261. return;
  262. }
  263. if (!data_leave_validation(document.body)) {
  264. return false;
  265. }
  266. };
  267. }
  268. function init_extra_afterDOM() {
  269. if (!window.context) {
  270. if (window.console) {
  271. console.log('FreshRSS extra waiting for JS…');
  272. }
  273. setTimeout(init_extra_afterDOM, 50);
  274. return;
  275. }
  276. if (!['normal', 'global', 'reader'].includes(context.current_view)) {
  277. init_crypto_form();
  278. init_password_observers(document.body);
  279. init_select_observers();
  280. init_configuration_alert();
  281. init_2stateButton();
  282. const slider = document.getElementById('slider');
  283. if (slider) {
  284. slider.addEventListener('freshrss:slider-load', function (e) {
  285. init_password_observers(slider);
  286. });
  287. init_slider(slider);
  288. init_archiving(slider);
  289. init_url_observers(slider);
  290. } else {
  291. init_archiving(document.body);
  292. init_url_observers(document.body);
  293. }
  294. }
  295. if (window.console) {
  296. console.log('FreshRSS extra init done.');
  297. }
  298. }
  299. if (document.readyState && document.readyState !== 'loading') {
  300. init_extra_afterDOM();
  301. } else {
  302. document.addEventListener('DOMContentLoaded', function () {
  303. if (window.console) {
  304. console.log('FreshRSS extra waiting for DOMContentLoaded…');
  305. }
  306. init_extra_afterDOM();
  307. }, false);
  308. }
  309. // @license-end