extra.js 7.4 KB

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