extra.js 7.3 KB

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