extra.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. /* jshint esversion:6, strict:global */
  5. function fix_popup_preview_selector() {
  6. const link = document.getElementById('popup-preview-selector');
  7. if (!link) {
  8. return;
  9. }
  10. link.addEventListener('click', function (ev) {
  11. const selector_entries = document.getElementById('path_entries').value;
  12. const href = link.href.replace('selector-token', encodeURIComponent(selector_entries));
  13. openPopupWithSource(href);
  14. ev.preventDefault();
  15. });
  16. }
  17. //<crypto form (Web login)>
  18. function poormanSalt() { //If crypto.getRandomValues is not available
  19. const base = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789/abcdefghijklmnopqrstuvwxyz';
  20. let text = '$2a$04$';
  21. for (let i = 22; i > 0; i--) {
  22. text += base.charAt(Math.floor(Math.random() * 64));
  23. }
  24. return text;
  25. }
  26. function init_crypto_form() {
  27. /* globals dcodeIO */
  28. const crypto_form = document.getElementById('crypto-form');
  29. if (!crypto_form) {
  30. return;
  31. }
  32. if (!(window.dcodeIO)) {
  33. if (window.console) {
  34. console.log('FreshRSS waiting for bcrypt.js…');
  35. }
  36. setTimeout(init_crypto_form, 100);
  37. return;
  38. }
  39. crypto_form.onsubmit = function (e) {
  40. const submit_button = this.querySelector('button[type="submit"]');
  41. submit_button.disabled = true;
  42. let success = false;
  43. const req = new XMLHttpRequest();
  44. req.open('GET', './?c=javascript&a=nonce&user=' + document.getElementById('username').value, false);
  45. req.onerror = function () {
  46. openNotification('Communication error!', 'bad');
  47. };
  48. req.send();
  49. if (req.status == 200) {
  50. const json = xmlHttpRequestJson(req);
  51. if (!json.salt1 || !json.nonce) {
  52. openNotification('Invalid user!', 'bad');
  53. } else {
  54. try {
  55. const strong = window.Uint32Array && window.crypto && (typeof window.crypto.getRandomValues === 'function'),
  56. s = dcodeIO.bcrypt.hashSync(document.getElementById('passwordPlain').value, json.salt1),
  57. c = dcodeIO.bcrypt.hashSync(json.nonce + s, strong ? dcodeIO.bcrypt.genSaltSync(4) : poormanSalt());
  58. document.getElementById('challenge').value = c;
  59. if (!s || !c) {
  60. openNotification('Crypto error!', 'bad');
  61. } else {
  62. success = true;
  63. }
  64. } catch (ex) {
  65. openNotification('Crypto exception! ' + ex, 'bad');
  66. }
  67. }
  68. } else {
  69. req.onerror();
  70. }
  71. submit_button.disabled = false;
  72. return success;
  73. };
  74. }
  75. //</crypto form (Web login)>
  76. function init_share_observers() {
  77. let shares = document.querySelectorAll('.group-share').length;
  78. const shareAdd = document.querySelector('.share.add');
  79. if (shareAdd) {
  80. shareAdd.onclick = function (ev) {
  81. const s = this.parentElement.querySelector('select'),
  82. opt = s.options[s.selectedIndex];
  83. let row = this.closest('form').getAttribute('data-' + opt.getAttribute('data-form'));
  84. row = row.replace(/##label##/g, opt.text);
  85. row = row.replace(/##type##/g, opt.value);
  86. row = row.replace(/##help##/g, opt.getAttribute('data-help'));
  87. row = row.replace(/##key##/g, shares);
  88. row = row.replace(/##method##/g, opt.getAttribute('data-method'));
  89. row = row.replace(/##field##/g, opt.getAttribute('data-field'));
  90. this.closest('.form-group').insertAdjacentHTML('beforebegin', row);
  91. shares++;
  92. return false;
  93. };
  94. }
  95. }
  96. function init_remove_observers() {
  97. document.querySelectorAll('.post').forEach(function (div) {
  98. div.onclick = function (ev) {
  99. const a = ev.target.closest('a.remove');
  100. if (a) {
  101. const remove_what = a.getAttribute('data-remove');
  102. if (remove_what !== undefined) {
  103. const d = document.getElementById(remove_what);
  104. if (d) {
  105. d.remove();
  106. }
  107. }
  108. return false;
  109. }
  110. };
  111. });
  112. }
  113. function init_feed_observers() {
  114. const s = document.getElementById('category');
  115. if (s && s.matches('select')) {
  116. s.onchange = function (ev) {
  117. const detail = document.getElementById('new_category_name').parentElement;
  118. if (this.value === 'nc') {
  119. detail.setAttribute('aria-hidden', 'false');
  120. detail.querySelector('input').focus();
  121. } else {
  122. detail.setAttribute('aria-hidden', 'true');
  123. }
  124. };
  125. }
  126. }
  127. function init_password_observers() {
  128. document.querySelectorAll('.toggle-password').forEach(function (a) {
  129. a.onmousedown = function (ev) {
  130. const passwordField = document.getElementById(this.getAttribute('data-toggle'));
  131. passwordField.setAttribute('type', 'text');
  132. this.classList.add('active');
  133. return false;
  134. };
  135. a.onmouseup = function (ev) {
  136. const passwordField = document.getElementById(this.getAttribute('data-toggle'));
  137. passwordField.setAttribute('type', 'password');
  138. this.classList.remove('active');
  139. return false;
  140. };
  141. });
  142. }
  143. function init_select_observers() {
  144. document.querySelectorAll('.select-change').forEach(function (s) {
  145. s.onchange = function (ev) {
  146. const opt = s.options[s.selectedIndex],
  147. url = opt.getAttribute('data-url');
  148. if (url) {
  149. s.form.querySelectorAll('[type=submit]').forEach(function (b) {
  150. b.disabled = true;
  151. });
  152. location.href = url;
  153. }
  154. };
  155. });
  156. }
  157. function init_slider_observers() {
  158. const slider = document.getElementById('slider'),
  159. closer = document.getElementById('close-slider');
  160. if (!slider) {
  161. return;
  162. }
  163. document.querySelector('.post').onclick = function (ev) {
  164. const a = ev.target.closest('.open-slider');
  165. if (a) {
  166. if (!context.ajax_loading) {
  167. context.ajax_loading = true;
  168. const req = new XMLHttpRequest();
  169. req.open('GET', a.href + '&ajax=1', true);
  170. req.responseType = 'document';
  171. req.onload = function (e) {
  172. slider.innerHTML = this.response.body.innerHTML;
  173. slider.classList.add('active');
  174. closer.classList.add('active');
  175. context.ajax_loading = false;
  176. fix_popup_preview_selector();
  177. };
  178. req.send();
  179. return false;
  180. }
  181. }
  182. };
  183. closer.onclick = function (ev) {
  184. if (data_leave_validation() || confirm(context.i18n.confirmation_default)) {
  185. slider.querySelectorAll('form').forEach(function (f) { f.reset(); });
  186. closer.classList.remove('active');
  187. slider.classList.remove('active');
  188. return true;
  189. } else {
  190. return false;
  191. }
  192. };
  193. }
  194. function data_leave_validation() {
  195. const ds = document.querySelectorAll('[data-leave-validation]');
  196. for (let i = ds.length - 1; i >= 0; i--) {
  197. const input = ds[i];
  198. if (input.type === 'checkbox' || input.type === 'radio') {
  199. if (input.checked != input.getAttribute('data-leave-validation')) {
  200. return false;
  201. }
  202. } else if (input.value != input.getAttribute('data-leave-validation')) {
  203. return false;
  204. }
  205. }
  206. return true;
  207. }
  208. function init_configuration_alert() {
  209. window.onsubmit = function (e) {
  210. window.hasSubmit = true;
  211. };
  212. window.onbeforeunload = function (e) {
  213. if (window.hasSubmit) {
  214. return;
  215. }
  216. if (!data_leave_validation()) {
  217. return false;
  218. }
  219. };
  220. }
  221. function init_extra() {
  222. if (!window.context) {
  223. if (window.console) {
  224. console.log('FreshRSS extra waiting for JS…');
  225. }
  226. window.setTimeout(init_extra, 50); //Wait for all js to be loaded
  227. return;
  228. }
  229. init_crypto_form();
  230. init_share_observers();
  231. init_remove_observers();
  232. init_feed_observers();
  233. init_password_observers();
  234. init_select_observers();
  235. init_slider_observers();
  236. init_configuration_alert();
  237. fix_popup_preview_selector();
  238. }
  239. if (document.readyState && document.readyState !== 'loading') {
  240. init_extra();
  241. } else {
  242. document.addEventListener('DOMContentLoaded', function () {
  243. if (window.console) {
  244. console.log('FreshRSS extra waiting for DOMContentLoaded…');
  245. }
  246. init_extra();
  247. }, false);
  248. }
  249. // @license-end