extra.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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_password_observers() {
  114. document.querySelectorAll('.toggle-password').forEach(function (a) {
  115. a.onmousedown = function (ev) {
  116. const passwordField = document.getElementById(this.getAttribute('data-toggle'));
  117. passwordField.setAttribute('type', 'text');
  118. this.classList.add('active');
  119. return false;
  120. };
  121. a.onmouseup = function (ev) {
  122. const passwordField = document.getElementById(this.getAttribute('data-toggle'));
  123. passwordField.setAttribute('type', 'password');
  124. this.classList.remove('active');
  125. return false;
  126. };
  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. 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. 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. };
  168. req.send();
  169. return false;
  170. }
  171. }
  172. };
  173. closer.onclick = function (ev) {
  174. if (data_leave_validation() || confirm(context.i18n.confirmation_default)) {
  175. slider.querySelectorAll('form').forEach(function (f) { f.reset(); });
  176. closer.classList.remove('active');
  177. slider.classList.remove('active');
  178. return true;
  179. } else {
  180. return false;
  181. }
  182. };
  183. }
  184. function data_leave_validation() {
  185. const ds = document.querySelectorAll('[data-leave-validation]');
  186. for (let i = ds.length - 1; i >= 0; i--) {
  187. const input = ds[i];
  188. if (input.type === 'checkbox' || input.type === 'radio') {
  189. if (input.checked != input.getAttribute('data-leave-validation')) {
  190. return false;
  191. }
  192. } else if (input.value != input.getAttribute('data-leave-validation')) {
  193. return false;
  194. }
  195. }
  196. return true;
  197. }
  198. function init_configuration_alert() {
  199. window.onsubmit = function (e) {
  200. window.hasSubmit = true;
  201. };
  202. window.onbeforeunload = function (e) {
  203. if (window.hasSubmit) {
  204. return;
  205. }
  206. if (!data_leave_validation()) {
  207. return false;
  208. }
  209. };
  210. }
  211. function init_extra() {
  212. if (!window.context) {
  213. if (window.console) {
  214. console.log('FreshRSS extra waiting for JS…');
  215. }
  216. window.setTimeout(init_extra, 50); //Wait for all js to be loaded
  217. return;
  218. }
  219. init_crypto_form();
  220. init_share_observers();
  221. init_remove_observers();
  222. init_password_observers();
  223. init_select_observers();
  224. init_slider_observers();
  225. init_configuration_alert();
  226. fix_popup_preview_selector();
  227. }
  228. if (document.readyState && document.readyState !== 'loading') {
  229. init_extra();
  230. } else {
  231. document.addEventListener('DOMContentLoaded', function () {
  232. if (window.console) {
  233. console.log('FreshRSS extra waiting for DOMContentLoaded…');
  234. }
  235. init_extra();
  236. }, false);
  237. }
  238. // @license-end