main.js 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945
  1. // @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-3.0
  2. 'use strict';
  3. // <Polyfills>
  4. if (!document.scrollingElement) document.scrollingElement = document.documentElement;
  5. if (!NodeList.prototype.forEach) NodeList.prototype.forEach = Array.prototype.forEach;
  6. if (!Element.prototype.matches) {
  7. Element.prototype.matches = Element.prototype.msMatchesSelector || Element.prototype.mozMatchesSelector || Element.prototype.webkitMatchesSelector;
  8. }
  9. if (!Element.prototype.closest) {
  10. Element.prototype.closest = function (s) {
  11. let el = this;
  12. do {
  13. if (el.matches(s)) return el;
  14. el = el.parentElement;
  15. } while (el);
  16. return null;
  17. };
  18. }
  19. if (!Element.prototype.remove) Element.prototype.remove = function () { if (this.parentNode) this.parentNode.removeChild(this); };
  20. // </Polyfills>
  21. // <Utils>
  22. function xmlHttpRequestJson(req) {
  23. let json = req.response;
  24. if (req.responseType !== 'json') { // IE11
  25. try {
  26. json = JSON.parse(req.responseText);
  27. } catch (ex) {
  28. json = null;
  29. }
  30. }
  31. return json;
  32. }
  33. // </Utils>
  34. // <Global context>
  35. /* eslint-disable no-var */
  36. var context;
  37. /* eslint-enable no-var */
  38. (function parseJsonVars() {
  39. const jsonVars = document.getElementById('jsonVars');
  40. const json = JSON.parse(jsonVars.innerHTML);
  41. jsonVars.outerHTML = '';
  42. context = json.context;
  43. context.ajax_loading = false;
  44. context.i18n = json.i18n;
  45. context.shortcuts = json.shortcuts;
  46. context.urls = json.urls;
  47. context.icons = json.icons;
  48. context.icons.read = decodeURIComponent(context.icons.read);
  49. context.icons.unread = decodeURIComponent(context.icons.unread);
  50. context.extensions = json.extensions;
  51. }());
  52. // </Global context>
  53. function badAjax(reload) {
  54. openNotification(context.i18n.notif_request_failed, 'bad');
  55. if (reload) {
  56. setTimeout(function () { location.reload(); }, 2000);
  57. }
  58. return true;
  59. }
  60. function needsScroll(elem) {
  61. const winBottom = document.scrollingElement.scrollTop + document.scrollingElement.clientHeight;
  62. const elemTop = elem.offsetParent.offsetTop + elem.offsetTop;
  63. const elemBottom = elemTop + elem.offsetHeight;
  64. if (elemTop < document.scrollingElement.scrollTop || elemBottom > winBottom) {
  65. return elemTop - (document.scrollingElement.clientHeight / 2);
  66. }
  67. return 0;
  68. }
  69. function str2int(str) {
  70. if (!str) {
  71. return 0;
  72. }
  73. return parseInt(str.replace(/\D/g, ''), 10) || 0;
  74. }
  75. function numberFormat(nStr) {
  76. if (nStr < 0) {
  77. return 0;
  78. }
  79. // http://www.mredkj.com/javascript/numberFormat.html
  80. nStr += '';
  81. const x = nStr.split('.');
  82. const x2 = x.length > 1 ? '.' + x[1] : '';
  83. const rgx = /(\d+)(\d{3})/;
  84. let x1 = x[0];
  85. while (rgx.test(x1)) {
  86. x1 = x1.replace(rgx, '$1 $2');
  87. }
  88. return x1 + x2;
  89. }
  90. function incLabel(p, inc, spaceAfter) {
  91. const i = str2int(p) + inc;
  92. return i > 0 ? ((spaceAfter ? '' : ' ') + '(' + numberFormat(i) + ')' + (spaceAfter ? ' ' : '')) : '';
  93. }
  94. function incUnreadsFeed(article, feed_id, nb) {
  95. // Update unread: feed
  96. let elem = document.getElementById(feed_id);
  97. let feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  98. const feed_priority = elem ? str2int(elem.getAttribute('data-priority')) : 0;
  99. if (elem) {
  100. elem.setAttribute('data-unread', feed_unreads + nb);
  101. elem = elem.querySelector('.item-title');
  102. if (elem) {
  103. elem.setAttribute('data-unread', numberFormat(feed_unreads + nb));
  104. }
  105. }
  106. // Update unread: category
  107. elem = document.getElementById(feed_id);
  108. elem = elem ? elem.closest('.category') : null;
  109. if (elem) {
  110. feed_unreads = str2int(elem.getAttribute('data-unread'));
  111. elem.setAttribute('data-unread', feed_unreads + nb);
  112. elem = elem.querySelector('.title');
  113. if (elem) {
  114. elem.setAttribute('data-unread', numberFormat(feed_unreads + nb));
  115. }
  116. }
  117. // Update unread: all
  118. if (feed_priority > 0) {
  119. elem = document.querySelector('#aside_feed .all .title');
  120. if (elem) {
  121. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  122. elem.setAttribute('data-unread', numberFormat(feed_unreads + nb));
  123. }
  124. }
  125. // Update unread: favourites
  126. if (article && article.closest('div').classList.contains('favorite')) {
  127. elem = document.querySelector('#aside_feed .favorites .title');
  128. if (elem) {
  129. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  130. elem.setAttribute('data-unread', numberFormat(feed_unreads + nb));
  131. }
  132. }
  133. let isCurrentView = false;
  134. // Update unread: title
  135. document.title = document.title.replace(/^((?:\([\s0-9]+\) )?)/, function (m, p1) {
  136. const feed = document.getElementById(feed_id);
  137. if (article || (feed && feed.closest('.active'))) {
  138. isCurrentView = true;
  139. return incLabel(p1, nb, true);
  140. } else if (document.querySelector('.all.active')) {
  141. isCurrentView = feed_priority > 0;
  142. return incLabel(p1, feed_priority > 0 ? nb : 0, true);
  143. } else {
  144. return p1;
  145. }
  146. });
  147. return isCurrentView;
  148. }
  149. function incUnreadsTag(tag_id, nb) {
  150. let t = document.getElementById(tag_id);
  151. if (t) {
  152. const unreads = str2int(t.getAttribute('data-unread'));
  153. t.setAttribute('data-unread', unreads + nb);
  154. t.querySelector('.item-title').setAttribute('data-unread', numberFormat(unreads + nb));
  155. }
  156. t = document.querySelector('.category.tags .title');
  157. if (t) {
  158. const unreads = str2int(t.getAttribute('data-unread'));
  159. t.setAttribute('data-unread', numberFormat(unreads + nb));
  160. }
  161. }
  162. function removeArticle(div) {
  163. if (!div || div.classList.contains('not_read') || (context.auto_mark_article && div.classList.contains('active'))) {
  164. return;
  165. }
  166. let scrollTop = box_to_follow.scrollTop;
  167. let dirty = false;
  168. const p = div.previousElementSibling;
  169. const n = div.nextElementSibling;
  170. if (p && p.classList.contains('day') && n && n.classList.contains('day')) {
  171. scrollTop -= p.offsetHeight;
  172. dirty = true;
  173. p.remove();
  174. }
  175. if (div.offsetHeight > 0 && div.offsetParent.offsetTop + div.offsetTop + div.offsetHeight < scrollTop) {
  176. scrollTop -= div.offsetHeight;
  177. dirty = true;
  178. }
  179. div.remove();
  180. if (dirty) {
  181. box_to_follow.scrollTop = scrollTop;
  182. }
  183. }
  184. const pending_entries = {};
  185. let mark_read_queue = [];
  186. function send_mark_read_queue(queue, asRead, callback) {
  187. if (!queue || queue.length === 0) {
  188. if (callback) {
  189. callback();
  190. }
  191. return;
  192. }
  193. const req = new XMLHttpRequest();
  194. req.open('POST', '.?c=entry&a=read' + (asRead ? '' : '&is_read=0'), true);
  195. req.responseType = 'json';
  196. req.onerror = function (e) {
  197. for (let i = queue.length - 1; i >= 0; i--) {
  198. delete pending_entries['flux_' + queue[i]];
  199. }
  200. badAjax(this.status == 403);
  201. };
  202. req.onload = function (e) {
  203. if (this.status != 200) {
  204. return req.onerror(e);
  205. }
  206. const json = xmlHttpRequestJson(this);
  207. if (!json) {
  208. return req.onerror(e);
  209. }
  210. for (let i = queue.length - 1; i >= 0; i--) {
  211. const div = document.getElementById('flux_' + queue[i]);
  212. const myIcons = context.icons;
  213. let inc = 0;
  214. if (div.classList.contains('not_read')) {
  215. div.classList.remove('not_read');
  216. div.querySelectorAll('a.read').forEach(function (a) {
  217. a.href = a.href.replace('&is_read=0', '') + '&is_read=1';
  218. });
  219. div.querySelectorAll('a.read > .icon').forEach(function (img) { img.outerHTML = myIcons.read; });
  220. inc--;
  221. if (context.auto_remove_article) {
  222. removeArticle(div);
  223. }
  224. } else {
  225. div.classList.add('not_read');
  226. div.classList.add('keep_unread'); // Split for IE11
  227. div.querySelectorAll('a.read').forEach(function (a) {
  228. a.href = a.href.replace('&is_read=1', '');
  229. });
  230. div.querySelectorAll('a.read > .icon').forEach(function (img) { img.outerHTML = myIcons.unread; });
  231. inc++;
  232. }
  233. const feed_link = div.querySelector('.website > a, a.website');
  234. if (feed_link) {
  235. const feed_url = feed_link.href;
  236. const feed_id = feed_url.substr(feed_url.lastIndexOf('f_'));
  237. incUnreadsFeed(div, feed_id, inc);
  238. }
  239. delete pending_entries['flux_' + queue[i]];
  240. }
  241. faviconNbUnread();
  242. if (json.tags) {
  243. const tagIds = Object.keys(json.tags);
  244. for (let i = tagIds.length - 1; i >= 0; i--) {
  245. const tagId = tagIds[i];
  246. incUnreadsTag(tagId, (asRead ? -1 : 1) * json.tags[tagId].length);
  247. }
  248. }
  249. toggle_bigMarkAsRead_button();
  250. onScroll();
  251. if (callback) {
  252. callback();
  253. }
  254. };
  255. req.setRequestHeader('Content-Type', 'application/json');
  256. req.send(JSON.stringify({
  257. ajax: true,
  258. _csrf: context.csrf,
  259. id: queue,
  260. }));
  261. }
  262. let send_mark_read_queue_timeout = 0;
  263. function send_mark_queue_tick(callback) {
  264. send_mark_read_queue_timeout = 0;
  265. const queue = mark_read_queue.slice(0);
  266. mark_read_queue = [];
  267. send_mark_read_queue(queue, true, callback);
  268. }
  269. const delayedFunction = send_mark_queue_tick;
  270. function delayedClick(a) {
  271. if (a) {
  272. delayedFunction(function () { a.click(); });
  273. }
  274. }
  275. function mark_read(div, only_not_read, asBatch) {
  276. if (!div || !div.id || context.anonymous ||
  277. (only_not_read && !div.classList.contains('not_read'))) {
  278. return false;
  279. }
  280. if (pending_entries[div.id]) {
  281. return false;
  282. }
  283. pending_entries[div.id] = true;
  284. const asRead = div.classList.contains('not_read');
  285. const entryId = div.id.replace(/^flux_/, '');
  286. if (asRead && asBatch) {
  287. mark_read_queue.push(entryId);
  288. if (send_mark_read_queue_timeout == 0) {
  289. send_mark_read_queue_timeout = setTimeout(function () { send_mark_queue_tick(null); }, 1000);
  290. }
  291. } else {
  292. const queue = [entryId];
  293. send_mark_read_queue(queue, asRead);
  294. }
  295. }
  296. function mark_previous_read(div) {
  297. while (div) {
  298. mark_read(div, true, true);
  299. div = div.previousElementSibling;
  300. }
  301. }
  302. function mark_favorite(div) {
  303. if (!div) {
  304. return false;
  305. }
  306. const a = div.querySelector('a.bookmark');
  307. const url = a ? a.href : '';
  308. if (!url) {
  309. return false;
  310. }
  311. if (pending_entries[div.id]) {
  312. return false;
  313. }
  314. pending_entries[div.id] = true;
  315. const req = new XMLHttpRequest();
  316. req.open('POST', url, true);
  317. req.responseType = 'json';
  318. req.onerror = function (e) {
  319. delete pending_entries[div.id];
  320. badAjax(this.status == 403);
  321. };
  322. req.onload = function (e) {
  323. if (this.status != 200) {
  324. return req.onerror(e);
  325. }
  326. const json = xmlHttpRequestJson(this);
  327. if (!json) {
  328. return req.onerror(e);
  329. }
  330. let inc = 0;
  331. if (div.classList.contains('favorite')) {
  332. div.classList.remove('favorite');
  333. inc--;
  334. } else {
  335. div.classList.add('favorite');
  336. inc++;
  337. }
  338. div.querySelectorAll('a.bookmark').forEach(function (a) { a.href = json.url; });
  339. div.querySelectorAll('a.bookmark > .icon').forEach(function (img) { img.outerHTML = json.icon; });
  340. const favourites = document.querySelector('#aside_feed .favorites .title');
  341. if (favourites) {
  342. favourites.textContent = favourites.textContent.replace(/((?: \([\s0-9]+\))?\s*)$/, function (m, p1) {
  343. return incLabel(p1, inc, false);
  344. });
  345. }
  346. if (div.classList.contains('not_read')) {
  347. const elem = document.querySelector('#aside_feed .favorites .title');
  348. const feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  349. if (elem) {
  350. elem.setAttribute('data-unread', numberFormat(feed_unreads + inc));
  351. }
  352. }
  353. delete pending_entries[div.id];
  354. };
  355. req.setRequestHeader('Content-Type', 'application/json');
  356. req.send(JSON.stringify({
  357. ajax: true,
  358. _csrf: context.csrf,
  359. }));
  360. }
  361. const freshrssOpenArticleEvent = document.createEvent('Event');
  362. freshrssOpenArticleEvent.initEvent('freshrss:openArticle', true, true);
  363. function toggleContent(new_active, old_active, skipping) {
  364. // If skipping, move current without activating or marking as read
  365. if (!new_active) {
  366. return;
  367. }
  368. if (context.does_lazyload && !skipping) {
  369. new_active.querySelectorAll('img[data-original], iframe[data-original]').forEach(function (el) {
  370. el.src = el.getAttribute('data-original');
  371. el.removeAttribute('data-original');
  372. });
  373. }
  374. if (old_active !== new_active) {
  375. if (!skipping) {
  376. new_active.classList.add('active');
  377. }
  378. new_active.classList.add('current');
  379. if (old_active) {
  380. old_active.classList.remove('active');
  381. old_active.classList.remove('current'); // Split for IE11
  382. if (context.auto_remove_article) {
  383. removeArticle(old_active);
  384. }
  385. }
  386. } else {
  387. new_active.classList.toggle('active');
  388. }
  389. const relative_move = context.current_view === 'global';
  390. const box_to_move = relative_move ? document.getElementById('panel') : document.scrollingElement;
  391. if (context.sticky_post) { // Stick the article to the top when opened
  392. const prev_article = new_active.previousElementSibling;
  393. const nav_menu = document.querySelector('.nav_menu');
  394. let nav_menu_height = 0;
  395. if (getComputedStyle(nav_menu).position === 'fixed' || getComputedStyle(nav_menu).position === 'sticky') {
  396. nav_menu_height = nav_menu.offsetHeight;
  397. }
  398. let new_pos = new_active.offsetParent.offsetTop + new_active.offsetTop - nav_menu_height;
  399. if (prev_article && prev_article.offsetParent && new_active.offsetTop - prev_article.offsetTop <= 150) {
  400. new_pos = prev_article.offsetParent.offsetTop + prev_article.offsetTop - nav_menu_height;
  401. if (relative_move) {
  402. new_pos -= box_to_move.offsetTop;
  403. }
  404. }
  405. if (skipping) {
  406. // when skipping, this feels more natural if it’s not so near the top
  407. new_pos -= document.body.clientHeight / 4;
  408. }
  409. box_to_move.scrollTop = new_pos;
  410. }
  411. if (new_active.classList.contains('active') && !skipping) {
  412. if (context.auto_mark_article) {
  413. mark_read(new_active, true, true);
  414. }
  415. new_active.dispatchEvent(freshrssOpenArticleEvent);
  416. }
  417. onScroll();
  418. }
  419. function prev_entry(skipping) {
  420. const old_active = document.querySelector('.flux.current');
  421. let new_active = old_active;
  422. if (new_active) {
  423. do new_active = new_active.previousElementSibling;
  424. while (new_active && !new_active.classList.contains('flux'));
  425. if (!new_active) {
  426. prev_feed();
  427. }
  428. } else {
  429. new_active = document.querySelector('.flux');
  430. }
  431. toggleContent(new_active, old_active, skipping);
  432. }
  433. function next_entry(skipping) {
  434. const old_active = document.querySelector('.flux.current');
  435. let new_active = old_active;
  436. if (new_active) {
  437. do new_active = new_active.nextElementSibling;
  438. while (new_active && !new_active.classList.contains('flux'));
  439. if (!new_active) {
  440. next_feed();
  441. }
  442. } else {
  443. new_active = document.querySelector('.flux');
  444. }
  445. toggleContent(new_active, old_active, skipping);
  446. }
  447. function next_unread_entry(skipping) {
  448. const old_active = document.querySelector('.flux.current');
  449. let new_active = old_active;
  450. if (new_active) {
  451. do new_active = new_active.nextElementSibling;
  452. while (new_active && !new_active.classList.contains('not_read'));
  453. if (!new_active) {
  454. next_feed();
  455. }
  456. } else {
  457. new_active = document.querySelector('.not_read');
  458. }
  459. toggleContent(new_active, old_active, skipping);
  460. }
  461. function prev_feed() {
  462. let found = false;
  463. let adjacent = null;
  464. const feeds = document.querySelectorAll('#aside_feed .feed');
  465. for (let i = feeds.length - 1; i >= 0; i--) {
  466. const feed = feeds[i];
  467. if (feed.classList.contains('active')) {
  468. found = true;
  469. continue;
  470. }
  471. if (!found) {
  472. continue;
  473. }
  474. if (getComputedStyle(feed).display === 'none') {
  475. continue;
  476. }
  477. if (feed.dataset.unread != 0) {
  478. return delayedClick(feed.querySelector('a.item-title'));
  479. } else if (adjacent === null) {
  480. adjacent = feed;
  481. }
  482. }
  483. if (found) {
  484. delayedClick(adjacent.querySelector('a.item-title'));
  485. } else {
  486. last_feed();
  487. }
  488. }
  489. function next_feed() {
  490. let found = false;
  491. let adjacent = null;
  492. const feeds = document.querySelectorAll('#aside_feed .feed');
  493. for (let i = 0; i < feeds.length; i++) {
  494. const feed = feeds[i];
  495. if (feed.classList.contains('active')) {
  496. found = true;
  497. continue;
  498. }
  499. if (!found) {
  500. continue;
  501. }
  502. if (getComputedStyle(feed).display === 'none') {
  503. continue;
  504. }
  505. if (feed.dataset.unread != 0) {
  506. return delayedClick(feed.querySelector('a.item-title'));
  507. } else if (adjacent === null) {
  508. adjacent = feed;
  509. }
  510. }
  511. if (found) {
  512. delayedClick(adjacent.querySelector('a.item-title'));
  513. } else {
  514. first_feed();
  515. }
  516. }
  517. function first_feed() {
  518. const a = document.querySelector('#aside_feed .category.active .feed:not([data-unread="0"]) a.item-title');
  519. delayedClick(a);
  520. }
  521. function last_feed() {
  522. const links = document.querySelectorAll('#aside_feed .category.active .feed:not([data-unread="0"]) a.item-title');
  523. if (links && links.length > 0) {
  524. delayedClick(links[links.length - 1]);
  525. }
  526. }
  527. function prev_category() {
  528. const active_cat = document.querySelector('#aside_feed .category.active');
  529. if (active_cat) {
  530. let cat = active_cat;
  531. do cat = cat.previousElementSibling;
  532. while (cat && getComputedStyle(cat).display === 'none');
  533. if (cat) {
  534. delayedClick(cat.querySelector('a.title'));
  535. }
  536. } else {
  537. last_category();
  538. }
  539. }
  540. function next_category() {
  541. const active_cat = document.querySelector('#aside_feed .category.active');
  542. if (active_cat) {
  543. let cat = active_cat;
  544. do cat = cat.nextElementSibling;
  545. while (cat && getComputedStyle(cat).display === 'none');
  546. if (cat) {
  547. delayedClick(cat.querySelector('a.title'));
  548. }
  549. } else {
  550. first_category();
  551. }
  552. }
  553. function next_unread_category() {
  554. const active_cat = document.querySelector('#aside_feed .category.active');
  555. if (active_cat) {
  556. let cat = active_cat;
  557. do cat = cat.nextElementSibling;
  558. while (cat && cat.getAttribute('data-unread') <= 0);
  559. if (cat) {
  560. delayedClick(cat.querySelector('a.title'));
  561. }
  562. } else {
  563. first_category();
  564. }
  565. }
  566. function first_category() {
  567. const a = document.querySelector('#aside_feed .category:not([data-unread="0"]) a.title');
  568. delayedClick(a);
  569. }
  570. function last_category() {
  571. const links = document.querySelectorAll('#aside_feed .category:not([data-unread="0"]) a.title');
  572. if (links && links.length > 0) {
  573. delayedClick(links[links.length - 1]);
  574. }
  575. }
  576. function collapse_entry() {
  577. const flux_current = document.querySelector('.flux.current');
  578. toggleContent(flux_current, flux_current, false);
  579. }
  580. function toggle_media() {
  581. const media = document.querySelector('.flux.current video,.flux.current audio');
  582. if (media === null) {
  583. return;
  584. }
  585. if (media.paused) {
  586. media.play();
  587. } else {
  588. media.pause();
  589. }
  590. }
  591. function user_filter(key) {
  592. const filter = document.getElementById('dropdown-query');
  593. const filters = filter.parentElement.querySelectorAll('.dropdown-menu > .query > a');
  594. if (typeof key === 'undefined') {
  595. if (!filters.length) {
  596. return;
  597. }
  598. // Display the filter div
  599. location.hash = filter.id;
  600. // Force scrolling to the filter div
  601. const scroll = needsScroll(document.querySelector('.header'));
  602. if (scroll !== 0) {
  603. document.scrollingElement.scrollTop = scroll;
  604. }
  605. // Force the key value if there is only one action, so we can trigger it automatically
  606. if (filters.length === 1) {
  607. key = 1;
  608. } else {
  609. return;
  610. }
  611. }
  612. // Trigger selected share action
  613. key = parseInt(key);
  614. if (key <= filters.length) {
  615. filters[key - 1].click();
  616. }
  617. }
  618. function auto_share(key) {
  619. const share = document.querySelector('.flux.current.active .dropdown-target[id^="dropdown-share"]');
  620. if (!share) {
  621. return;
  622. }
  623. const shares = share.parentElement.querySelectorAll('.dropdown-menu .item a');
  624. if (typeof key === 'undefined') {
  625. // Display the share div
  626. location.hash = share.id;
  627. // Force scrolling to the share div
  628. const scrollTop = needsScroll(share.closest('.bottom'));
  629. if (scrollTop !== 0) {
  630. document.scrollingElement.scrollTop = scrollTop;
  631. }
  632. // Force the key value if there is only one action, so we can trigger it automatically
  633. if (shares.length === 1) {
  634. key = 1;
  635. } else {
  636. return;
  637. }
  638. }
  639. // Trigger selected share action and hide the share div
  640. key = parseInt(key);
  641. if (key <= shares.length) {
  642. shares[key - 1].click();
  643. share.parentElement.querySelector('.dropdown-menu + a.dropdown-close').click();
  644. }
  645. }
  646. let box_to_follow;
  647. function onScroll() {
  648. if (!box_to_follow) {
  649. return;
  650. }
  651. if (context.auto_mark_scroll) {
  652. const hidden_px = -5; // negative = pixels over the edge
  653. const minTop = hidden_px + box_to_follow.scrollTop;
  654. document.querySelectorAll('.not_read:not(.keep_unread)').forEach(function (div) {
  655. if (div.offsetHeight > 0 &&
  656. div.offsetParent.offsetTop + div.offsetTop + div.offsetHeight < minTop) {
  657. mark_read(div, true, true);
  658. }
  659. });
  660. }
  661. let streamFooter;
  662. if (context.auto_load_more && (streamFooter = document.getElementById('stream-footer'))) {
  663. if (box_to_follow.offsetHeight > 0 &&
  664. box_to_follow.scrollTop + box_to_follow.offsetHeight + (window.innerHeight / 2) >= streamFooter.offsetTop) {
  665. // Too close to the last pre-loaded article
  666. load_more_posts();
  667. } else {
  668. const after = document.querySelectorAll('.flux.current ~ .flux').length;
  669. if (after > 0 && after <= 5) {
  670. // Too few pre-loaded articles
  671. load_more_posts();
  672. }
  673. }
  674. }
  675. }
  676. function init_posts() {
  677. if (context.auto_load_more || context.auto_mark_scroll || context.auto_remove_article) {
  678. box_to_follow = context.current_view === 'global' ? document.getElementById('panel') : document.scrollingElement;
  679. let lastScroll = 0; // Throttle
  680. let timerId = 0;
  681. (box_to_follow === document.scrollingElement ? window : box_to_follow).onscroll = function () {
  682. clearTimeout(timerId);
  683. if (lastScroll + 500 < Date.now()) {
  684. lastScroll = Date.now();
  685. onScroll();
  686. } else {
  687. timerId = setTimeout(onScroll, 500);
  688. }
  689. };
  690. onScroll();
  691. }
  692. if (!navigator.share && document.styleSheets.length > 0) {
  693. // https://developer.mozilla.org/en-US/docs/Web/API/Navigator/share
  694. // do not show the menu entry if browser does not support navigator.share
  695. document.styleSheets[0].insertRule(
  696. 'button.as-link[data-type="web-sharing-api"] {display: none !important;}',
  697. document.styleSheets[0].cssRules.length
  698. );
  699. }
  700. }
  701. function rememberOpenCategory(category_id, isOpen) {
  702. if (context.display_categories === 'remember') {
  703. const open_categories = JSON.parse(localStorage.getItem('FreshRSS_open_categories') || '{}');
  704. if (isOpen) {
  705. open_categories[category_id] = true;
  706. } else {
  707. delete open_categories[category_id];
  708. }
  709. localStorage.setItem('FreshRSS_open_categories', JSON.stringify(open_categories));
  710. }
  711. }
  712. function openCategory(category_id) {
  713. const category_element = document.getElementById(category_id);
  714. if (!category_element) return;
  715. category_element.querySelector('.tree-folder-items').classList.add('active');
  716. const img = category_element.querySelector('a.dropdown-toggle img');
  717. if (!img) return;
  718. img.src = img.src.replace('/icons/down.', '/icons/up.');
  719. img.alt = '🔼';
  720. }
  721. function loadJs(name) {
  722. if (!document.getElementById(name)) {
  723. const script = document.createElement('script');
  724. script.setAttribute('id', name);
  725. script.setAttribute('src', '../scripts/' + name + '?' + context.mtime[name]);
  726. script.setAttribute('defer', 'defer');
  727. script.setAttribute('async', 'async');
  728. document.head.appendChild(script);
  729. }
  730. }
  731. function init_column_categories() {
  732. if (context.current_view !== 'normal' && context.current_view !== 'reader') {
  733. return;
  734. }
  735. // Restore sidebar scroll position
  736. document.getElementById('sidebar').scrollTop = +sessionStorage.getItem('FreshRSS_sidebar_scrollTop');
  737. // Restore open categories
  738. if (context.display_categories === 'remember') {
  739. const open_categories = JSON.parse(localStorage.getItem('FreshRSS_open_categories') || '{}');
  740. Object.keys(open_categories).forEach(function (category_id) {
  741. openCategory(category_id);
  742. });
  743. }
  744. document.getElementById('aside_feed').onclick = function (ev) {
  745. let a = ev.target.closest('.tree-folder > .tree-folder-title > a.dropdown-toggle');
  746. if (a) {
  747. const icon = a.querySelector('.icon');
  748. const category_id = a.closest('.category').id;
  749. if (icon.alt === '🔽' || icon.innerHTML === '🔽') {
  750. if (icon.src) {
  751. icon.src = icon.src.replace('/icons/down.', '/icons/up.');
  752. icon.alt = '🔼';
  753. } else {
  754. icon.innerHTML = '🔼';
  755. }
  756. rememberOpenCategory(category_id, true);
  757. } else {
  758. if (icon.src) {
  759. icon.src = icon.src.replace('/icons/up.', '/icons/down.');
  760. icon.alt = '🔽';
  761. } else {
  762. icon.innerHTML = '🔽';
  763. }
  764. rememberOpenCategory(category_id, false);
  765. }
  766. const ul = a.closest('li').querySelector('.tree-folder-items');
  767. let nbVisibleItems = 0;
  768. for (let i = ul.children.length - 1; i >= 0; i--) {
  769. if (ul.children[i].offsetHeight) {
  770. nbVisibleItems++;
  771. }
  772. }
  773. ul.classList.toggle('active');
  774. // CSS transition does not work on max-height:auto
  775. ul.style.maxHeight = ul.classList.contains('active') ? (nbVisibleItems * 4) + 'em' : 0;
  776. return false;
  777. }
  778. a = ev.target.closest('.tree-folder-items > .feed .dropdown-toggle');
  779. if (a) {
  780. loadJs('extra.js');
  781. loadJs('feed.js');
  782. const itemId = a.closest('.item').id;
  783. const templateId = itemId.substring(0, 2) === 't_' ? 'tag_config_template' : 'feed_config_template';
  784. const id = itemId.substr(2);
  785. const feed_web = a.getAttribute('data-fweb') || '';
  786. const div = a.parentElement;
  787. const dropdownMenu = div.querySelector('.dropdown-menu');
  788. const template = document.getElementById(templateId)
  789. .innerHTML.replace(/------/g, id).replace('http://example.net/', feed_web);
  790. if (!dropdownMenu) {
  791. a.href = '#dropdown-' + id;
  792. div.querySelector('.dropdown-target').id = 'dropdown-' + id;
  793. div.insertAdjacentHTML('beforeend', template);
  794. if (feed_web == '') {
  795. const website = div.querySelector('.item.link.website');
  796. if (website) {
  797. website.remove();
  798. }
  799. }
  800. const b = div.querySelector('button.confirm');
  801. if (b) {
  802. b.disabled = false;
  803. }
  804. } else if (getComputedStyle(dropdownMenu).display === 'none') {
  805. const id2 = div.closest('.item').id.substr(2);
  806. a.href = '#dropdown-' + id2;
  807. } else {
  808. a.href = '#close';
  809. }
  810. return true;
  811. }
  812. return true;
  813. };
  814. }
  815. function init_shortcuts() {
  816. Object.keys(context.shortcuts).forEach(function (k) {
  817. context.shortcuts[k] = (context.shortcuts[k] || '').toUpperCase();
  818. });
  819. document.addEventListener('keydown', ev => {
  820. if (ev.target.closest('input, textarea') ||
  821. ev.ctrlKey || ev.metaKey || (ev.altKey && ev.shiftKey)) {
  822. return;
  823. }
  824. const s = context.shortcuts;
  825. let k = (ev.key.trim() || ev.code || 'Space').toUpperCase();
  826. // IE11
  827. if (k === 'SPACEBAR') k = 'SPACE';
  828. else if (k === 'DEL') k = 'DELETE';
  829. else if (k === 'ESC') k = 'ESCAPE';
  830. if (location.hash.match(/^#dropdown-/)) {
  831. const n = parseInt(k);
  832. if (n) {
  833. if (location.hash === '#dropdown-query') {
  834. user_filter(n);
  835. } else {
  836. auto_share(n);
  837. }
  838. ev.preventDefault();
  839. return;
  840. }
  841. }
  842. if (k === s.actualize) {
  843. const btn = document.getElementById('actualize');
  844. if (btn) {
  845. btn.click();
  846. }
  847. ev.preventDefault();
  848. return;
  849. }
  850. if (k === s.next_entry) {
  851. if (ev.altKey) {
  852. next_category();
  853. } else if (ev.shiftKey) {
  854. next_feed();
  855. } else {
  856. next_entry(false);
  857. }
  858. ev.preventDefault();
  859. return;
  860. }
  861. if (k === s.next_unread_entry) {
  862. if (ev.altKey) {
  863. next_unread_category();
  864. } else if (ev.shiftKey) {
  865. next_feed();
  866. } else {
  867. next_unread_entry(false);
  868. }
  869. ev.preventDefault();
  870. return;
  871. }
  872. if (k === s.prev_entry) {
  873. if (ev.altKey) {
  874. prev_category();
  875. } else if (ev.shiftKey) {
  876. prev_feed();
  877. } else {
  878. prev_entry(false);
  879. }
  880. ev.preventDefault();
  881. return;
  882. }
  883. if (k === s.mark_read) {
  884. if (ev.altKey) {
  885. mark_previous_read(document.querySelector('.flux.current'));
  886. } else if (ev.shiftKey) {
  887. document.querySelector('.nav_menu .read_all').click();
  888. } else { // Toggle the read state
  889. mark_read(document.querySelector('.flux.current'), false, false);
  890. }
  891. ev.preventDefault();
  892. return;
  893. }
  894. if (k === s.first_entry) {
  895. if (ev.altKey) {
  896. first_category();
  897. } else if (ev.shiftKey) {
  898. first_feed();
  899. } else {
  900. const old_active = document.querySelector('.flux.current');
  901. const first = document.querySelector('.flux');
  902. if (first.classList.contains('flux')) {
  903. toggleContent(first, old_active, false);
  904. }
  905. }
  906. ev.preventDefault();
  907. return;
  908. }
  909. if (k === s.last_entry) {
  910. if (ev.altKey) {
  911. last_category();
  912. } else if (ev.shiftKey) {
  913. last_feed();
  914. } else {
  915. const old_active = document.querySelector('.flux.current');
  916. const last = document.querySelector('.flux:last-of-type');
  917. if (last.classList.contains('flux')) {
  918. toggleContent(last, old_active, false);
  919. }
  920. }
  921. ev.preventDefault();
  922. return;
  923. }
  924. if (ev.altKey || ev.shiftKey) {
  925. return;
  926. }
  927. if (k === s.mark_favorite) { // Toggle the favorite state
  928. mark_favorite(document.querySelector('.flux.current'));
  929. ev.preventDefault();
  930. return;
  931. }
  932. if (k === s.go_website) {
  933. if (context.auto_mark_site) {
  934. mark_read(document.querySelector('.flux.current'), true, false);
  935. }
  936. const link_go_website = document.querySelector('.flux.current a.go_website');
  937. if (link_go_website) {
  938. const newWindow = window.open();
  939. if (newWindow) {
  940. newWindow.opener = null;
  941. newWindow.location = link_go_website.href;
  942. ev.preventDefault();
  943. }
  944. }
  945. return;
  946. }
  947. if (k === s.skip_next_entry) { next_entry(true); ev.preventDefault(); return; }
  948. if (k === s.skip_prev_entry) { prev_entry(true); ev.preventDefault(); return; }
  949. if (k === s.collapse_entry) { collapse_entry(); ev.preventDefault(); return; }
  950. if (k === s.auto_share) { auto_share(); ev.preventDefault(); return; }
  951. if (k === s.user_filter) { user_filter(); ev.preventDefault(); return; }
  952. if (k === s.load_more) { load_more_posts(); ev.preventDefault(); return; }
  953. if (k === s.close_dropdown) { location.hash = null; ev.preventDefault(); return; }
  954. if (k === s.help) { window.open(context.urls.help); ev.preventDefault(); return; }
  955. if (k === s.focus_search) { document.getElementById('search').focus(); ev.preventDefault(); return; }
  956. if (k === s.normal_view) { delayedClick(document.querySelector('#nav_menu_views .view-normal')); ev.preventDefault(); return; }
  957. if (k === s.reading_view) { delayedClick(document.querySelector('#nav_menu_views .view-reader')); ev.preventDefault(); return; }
  958. if (k === s.global_view) { delayedClick(document.querySelector('#nav_menu_views .view-global')); ev.preventDefault(); return; }
  959. if (k === s.rss_view) { delayedClick(document.querySelector('#nav_menu_views .view-rss')); ev.preventDefault(); return; }
  960. if (k === s.toggle_media) { toggle_media(); ev.preventDefault(); }
  961. });
  962. }
  963. function init_stream(stream) {
  964. stream.onclick = function (ev) {
  965. let el = ev.target.closest('.flux a.read');
  966. if (el) {
  967. mark_read(el.closest('.flux'), false, false);
  968. return false;
  969. }
  970. el = ev.target.closest('.flux a.bookmark');
  971. if (el) {
  972. mark_favorite(el.closest('.flux'));
  973. return false;
  974. }
  975. el = ev.target.closest('.dynamictags');
  976. if (el) {
  977. loadDynamicTags(el);
  978. return true;
  979. }
  980. el = ev.target.closest('.item.title > a');
  981. if (el) { // Allow default control/command-click behaviour such as open in background-tab
  982. return ev.ctrlKey || ev.metaKey;
  983. }
  984. el = ev.target.closest('.flux .content .text a');
  985. if (el) {
  986. if (!el.closest('div').classList.contains('author')) {
  987. el.target = '_blank';
  988. el.rel = 'noreferrer';
  989. }
  990. return true;
  991. }
  992. el = ev.target.closest('.item.share > button[data-type="print"]');
  993. if (el) { // Print
  994. const tmp_window = window.open();
  995. for (let i = 0; i < document.styleSheets.length; i++) {
  996. tmp_window.document.writeln('<link href="' + document.styleSheets[i].href + '" rel="stylesheet" type="text/css" />');
  997. }
  998. tmp_window.document.writeln(el.closest('.flux_content').querySelector('.content').innerHTML);
  999. tmp_window.document.close();
  1000. tmp_window.focus();
  1001. tmp_window.print();
  1002. tmp_window.close();
  1003. return false;
  1004. }
  1005. el = ev.target.closest('.item.share > button[data-type="clipboard"]');
  1006. if (el) { // Clipboard
  1007. if (navigator.clipboard) {
  1008. navigator.clipboard.writeText(el.dataset.url)
  1009. .then(() => {
  1010. toggleClass(el, 'error');
  1011. })
  1012. .catch(e => {
  1013. console.log(e);
  1014. toggleClass(el, 'error');
  1015. });
  1016. } else {
  1017. // fallback, if navigator.clipboard is not available f.e. if access is not via https or localhost
  1018. const inputElement = document.createElement('input');
  1019. inputElement.value = el.dataset.url;
  1020. document.body.appendChild(inputElement);
  1021. inputElement.select();
  1022. if (document.execCommand && document.execCommand('copy')) {
  1023. toggleClass(el, 'ok');
  1024. } else {
  1025. console.log('document.execCommand("copy") failed');
  1026. toggleClass(el, 'error');
  1027. }
  1028. inputElement.remove();
  1029. }
  1030. return false;
  1031. }
  1032. el = ev.target.closest('.item.share > button[data-type="web-sharing-api"]');
  1033. if (el && navigator.share) { // https://developer.mozilla.org/en-US/docs/Web/API/Navigator/share
  1034. const shareData = {
  1035. url: el.dataset.url,
  1036. title: decodeURI(el.dataset.title),
  1037. };
  1038. navigator.share(shareData);
  1039. return false;
  1040. }
  1041. el = ev.target.closest('.item.share > a[data-type="email-webmail-firefox-fix"]');
  1042. if (el) {
  1043. window.open(el.href);
  1044. return false;
  1045. }
  1046. el = ev.target.closest('.item.share > a[href="POST"]');
  1047. if (el) { // Share by POST
  1048. const f = el.parentElement.querySelector('form');
  1049. f.disabled = false;
  1050. f.submit();
  1051. return false;
  1052. }
  1053. el = ev.target.closest('.flux_header, .flux_content');
  1054. if (el) { // flux_toggle
  1055. if (ev.target.closest('.reader, .content, .item.website, .item.link, .dropdown')) {
  1056. return true;
  1057. }
  1058. if (!context.sides_close_article && ev.target.matches('.flux_content')) {
  1059. // setting for not-closing after clicking outside article area
  1060. return false;
  1061. }
  1062. const old_active = document.querySelector('.flux.current');
  1063. const new_active = el.parentNode;
  1064. if (ev.target.tagName.toUpperCase() === 'A') { // Leave real links alone (but does not catch img in a link)
  1065. if (context.auto_mark_article) {
  1066. mark_read(new_active, true, false);
  1067. }
  1068. return true;
  1069. }
  1070. toggleContent(new_active, old_active, false);
  1071. return false;
  1072. }
  1073. };
  1074. stream.onmouseup = function (ev) { // Mouseup enables us to catch middle click, and control+click in IE/Edge
  1075. if (ev.altKey || ev.metaKey || ev.shiftKey) {
  1076. return;
  1077. }
  1078. let el = ev.target.closest('.item.title > a');
  1079. if (el) {
  1080. if (ev.which == 1) {
  1081. if (ev.ctrlKey) { // Control+click
  1082. if (context.auto_mark_site) {
  1083. mark_read(el.closest('.flux'), true, false);
  1084. }
  1085. } else {
  1086. el.parentElement.click(); // Normal click, just toggle article.
  1087. }
  1088. } else if (ev.which == 2 && !ev.ctrlKey) { // Simple middle click: same behaviour as CTRL+click
  1089. if (context.auto_mark_article) {
  1090. const new_active = el.closest('.flux');
  1091. mark_read(new_active, true, false);
  1092. }
  1093. }
  1094. return;
  1095. }
  1096. if (context.auto_mark_site) {
  1097. // catch mouseup instead of click so we can have the correct behaviour
  1098. // with middle button click (scroll button).
  1099. el = ev.target.closest('.flux .link > a');
  1100. if (el) {
  1101. if (ev.which == 3) {
  1102. return;
  1103. }
  1104. mark_read(el.closest('.flux'), true, false);
  1105. }
  1106. }
  1107. };
  1108. stream.onchange = function (ev) {
  1109. const checkboxTag = ev.target.closest('.checkboxTag');
  1110. if (checkboxTag) { // Dynamic tags
  1111. ev.stopPropagation();
  1112. const tagId = checkboxTag.name.replace(/^t_/, '');
  1113. const tagName = checkboxTag.nextElementSibling ? checkboxTag.nextElementSibling.childNodes[0].value : '';
  1114. if ((tagId == 0 && tagName.length > 0) || tagId != 0) {
  1115. const isChecked = checkboxTag.checked;
  1116. const entry = checkboxTag.closest('div.flux');
  1117. const entryId = entry.id.replace(/^flux_/, '');
  1118. checkboxTag.disabled = true;
  1119. const req = new XMLHttpRequest();
  1120. req.open('POST', './?c=tag&a=tagEntry&ajax=1', true);
  1121. req.responseType = 'json';
  1122. req.onerror = function (e) {
  1123. checkboxTag.checked = !isChecked;
  1124. badAjax(this.status == 403);
  1125. };
  1126. req.onload = function (e) {
  1127. if (this.status != 200) {
  1128. return req.onerror(e);
  1129. }
  1130. if (entry.classList.contains('not_read')) {
  1131. incUnreadsTag('t_' + tagId, isChecked ? 1 : -1);
  1132. }
  1133. };
  1134. req.onloadend = function (e) {
  1135. checkboxTag.disabled = false;
  1136. if (tagId == 0) {
  1137. loadDynamicTags(checkboxTag.closest('div.dropdown'));
  1138. }
  1139. };
  1140. req.setRequestHeader('Content-Type', 'application/json');
  1141. req.send(JSON.stringify({
  1142. _csrf: context.csrf,
  1143. id_tag: tagId,
  1144. name_tag: tagId == 0 ? tagName : '',
  1145. id_entry: entryId,
  1146. checked: isChecked,
  1147. ajax: 1,
  1148. }));
  1149. }
  1150. }
  1151. };
  1152. }
  1153. function toggleClass(el, cssclass) {
  1154. el.classList.remove(cssclass);
  1155. el.dataset.foo = el.offsetWidth; // it does nothing, but it is needed. See https://github.com/FreshRSS/FreshRSS/pull/5295
  1156. el.classList.add(cssclass);
  1157. }
  1158. function init_nav_entries() {
  1159. const nav_entries = document.getElementById('nav_entries');
  1160. if (nav_entries) {
  1161. nav_entries.querySelector('.previous_entry').onclick = function (e) {
  1162. prev_entry(false);
  1163. return false;
  1164. };
  1165. nav_entries.querySelector('.next_entry').onclick = function (e) {
  1166. next_entry(false);
  1167. return false;
  1168. };
  1169. nav_entries.querySelector('.up').onclick = function (e) {
  1170. const active_item = (document.querySelector('.flux.current') || document.querySelector('.flux'));
  1171. const windowTop = document.scrollingElement.scrollTop;
  1172. const item_top = active_item.offsetParent.offsetTop + active_item.offsetTop;
  1173. const nav_menu = document.querySelector('.nav_menu');
  1174. let nav_menu_height = 0;
  1175. if (getComputedStyle(nav_menu).position === 'fixed' || getComputedStyle(nav_menu).position === 'sticky') {
  1176. nav_menu_height = nav_menu.offsetHeight;
  1177. }
  1178. document.scrollingElement.scrollTop = windowTop > item_top ? item_top - nav_menu_height : 0 - nav_menu_height;
  1179. return false;
  1180. };
  1181. }
  1182. }
  1183. function loadDynamicTags(div) {
  1184. div.classList.remove('dynamictags');
  1185. div.querySelectorAll('li.item').forEach(function (li) { li.remove(); });
  1186. const entryId = div.closest('div.flux').id.replace(/^flux_/, '');
  1187. const req = new XMLHttpRequest();
  1188. req.open('GET', './?c=tag&a=getTagsForEntry&id_entry=' + entryId, true);
  1189. req.responseType = 'json';
  1190. req.onerror = function (e) {
  1191. div.querySelectorAll('li.item').forEach(function (li) { li.remove(); });
  1192. div.classList.add('dynamictags');
  1193. };
  1194. req.onload = function (e) {
  1195. if (this.status != 200) {
  1196. return req.onerror(e);
  1197. }
  1198. const json = xmlHttpRequestJson(this);
  1199. if (!json) {
  1200. return req.onerror(e);
  1201. }
  1202. if (!context.anonymous) {
  1203. const li_item0 = document.createElement('li');
  1204. li_item0.setAttribute('class', 'item addItem');
  1205. const label = document.createElement('label');
  1206. label.setAttribute('class', 'noHover');
  1207. const input_checkboxTag = document.createElement('input');
  1208. input_checkboxTag.setAttribute('class', 'checkboxTag checkboxNewTag');
  1209. input_checkboxTag.setAttribute('name', 't_0');
  1210. input_checkboxTag.setAttribute('type', 'checkbox');
  1211. const input_newTag = document.createElement('input');
  1212. input_newTag.setAttribute('type', 'text');
  1213. input_newTag.setAttribute('name', 'newTag');
  1214. input_newTag.addEventListener('keydown', function (ev) { if (ev.key.toUpperCase() == 'ENTER') { this.parentNode.previousSibling.click(); } });
  1215. const button_btn = document.createElement('button');
  1216. button_btn.setAttribute('type', 'button');
  1217. button_btn.setAttribute('class', 'btn');
  1218. button_btn.addEventListener('click', function () { this.parentNode.parentNode.click(); });
  1219. const text_plus = document.createTextNode('+');
  1220. const div_stick = document.createElement('div');
  1221. div_stick.setAttribute('class', 'stick');
  1222. button_btn.appendChild(text_plus);
  1223. div_stick.appendChild(input_newTag);
  1224. div_stick.appendChild(button_btn);
  1225. label.appendChild(input_checkboxTag);
  1226. label.appendChild(div_stick);
  1227. li_item0.appendChild(label);
  1228. div.querySelector('.dropdown-menu').appendChild(li_item0);
  1229. }
  1230. let html = '';
  1231. if (json && json.length) {
  1232. let nbLabelsChecked = 0;
  1233. for (let i = 0; i < json.length; i++) {
  1234. const tag = json[i];
  1235. if (context.anonymous && !tag.checked) {
  1236. // In anomymous mode, show only the used tags
  1237. continue;
  1238. }
  1239. if (tag.checked) {
  1240. nbLabelsChecked++;
  1241. }
  1242. html += '<li class="item"><label><input ' +
  1243. (context.anonymous ? '' : 'class="checkboxTag" ') +
  1244. 'name="t_' + tag.id + '"type="checkbox" ' +
  1245. (context.anonymous ? 'disabled="disabled" ' : '') +
  1246. (tag.checked ? 'checked="checked" ' : '') + '/> ' + tag.name + '</label></li>';
  1247. }
  1248. if (context.anonymous && nbLabelsChecked === 0) {
  1249. html += '<li class="item"><span class="emptyLabels">' + context.i18n.labels_empty + '</span></li>';
  1250. }
  1251. }
  1252. div.querySelector('.dropdown-menu').insertAdjacentHTML('beforeend', html);
  1253. };
  1254. req.send();
  1255. }
  1256. // <actualize>
  1257. let feeds_processed = 0;
  1258. let categories_processed = 0;
  1259. let to_process = 0;
  1260. function refreshFeed(feeds, feeds_count) {
  1261. const feed = feeds.pop();
  1262. if (!feed) {
  1263. return;
  1264. }
  1265. const req = new XMLHttpRequest();
  1266. req.open('POST', feed.url, true);
  1267. req.onloadend = function (e) {
  1268. feeds_processed++;
  1269. if (this.status != 200) {
  1270. badAjax(false);
  1271. } else {
  1272. const div = document.getElementById('actualizeProgress');
  1273. div.querySelector('.progress').innerHTML = (categories_processed + feeds_processed) + ' / ' + to_process;
  1274. div.querySelector('.title').innerHTML = feed.title;
  1275. }
  1276. if (feeds_processed === feeds_count) {
  1277. // Empty request to commit new articles
  1278. const req2 = new XMLHttpRequest();
  1279. req2.open('POST', './?c=feed&a=actualize&id=-1&ajax=1', true);
  1280. req2.onloadend = function (e) {
  1281. delayedFunction(function () { location.reload(); });
  1282. };
  1283. req2.setRequestHeader('Content-Type', 'application/json');
  1284. req2.send(JSON.stringify({
  1285. _csrf: context.csrf,
  1286. noCommit: 0,
  1287. }));
  1288. } else {
  1289. refreshFeed(feeds, feeds_count);
  1290. }
  1291. };
  1292. req.setRequestHeader('Content-Type', 'application/json');
  1293. req.send(JSON.stringify({
  1294. _csrf: context.csrf,
  1295. noCommit: 1,
  1296. }));
  1297. }
  1298. function refreshFeeds(json) {
  1299. feeds_processed = 0;
  1300. if (!json.feeds || json.feeds.length === 0) {
  1301. // Empty request to commit new articles
  1302. const req2 = new XMLHttpRequest();
  1303. req2.open('POST', './?c=feed&a=actualize&id=-1&ajax=1', true);
  1304. req2.onloadend = function (e) {
  1305. context.ajax_loading = false;
  1306. };
  1307. req2.setRequestHeader('Content-Type', 'application/json');
  1308. req2.send(JSON.stringify({
  1309. _csrf: context.csrf,
  1310. noCommit: 0,
  1311. }));
  1312. } else {
  1313. const feeds_count = json.feeds.length;
  1314. for (let i = 10; i > 0; i--) {
  1315. refreshFeed(json.feeds, feeds_count);
  1316. }
  1317. }
  1318. }
  1319. function refreshDynamicOpml(categories, categories_count, next) {
  1320. const category = categories.pop();
  1321. if (!category) {
  1322. return;
  1323. }
  1324. const req = new XMLHttpRequest();
  1325. req.open('POST', category.url, true);
  1326. req.onloadend = function (e) {
  1327. categories_processed++;
  1328. if (this.status != 200) {
  1329. badAjax(false);
  1330. } else {
  1331. const div = document.getElementById('actualizeProgress');
  1332. div.querySelector('.progress').innerHTML = (categories_processed + feeds_processed) + ' / ' + to_process;
  1333. div.querySelector('.title').innerHTML = category.title;
  1334. }
  1335. if (categories_processed === categories_count) {
  1336. if (next) { next(); }
  1337. } else {
  1338. refreshDynamicOpml(categories, categories_count, next);
  1339. }
  1340. };
  1341. req.setRequestHeader('Content-Type', 'application/json');
  1342. req.send(JSON.stringify({
  1343. _csrf: context.csrf,
  1344. noCommit: 1,
  1345. }));
  1346. }
  1347. function refreshDynamicOpmls(json, next) {
  1348. categories_processed = 0;
  1349. if (json.categories && json.categories.length > 0) {
  1350. const categories_count = json.categories.length;
  1351. for (let i = 10; i > 0; i--) {
  1352. refreshDynamicOpml(json.categories, categories_count, next);
  1353. }
  1354. } else {
  1355. if (next) { next(); }
  1356. }
  1357. }
  1358. function init_actualize() {
  1359. let auto = false;
  1360. let nbCategoriesFirstRound = 0;
  1361. let skipCategories = false;
  1362. const actualize = document.getElementById('actualize');
  1363. if (!actualize) {
  1364. return;
  1365. }
  1366. actualize.onclick = function () {
  1367. if (context.ajax_loading) {
  1368. return false;
  1369. }
  1370. context.ajax_loading = true;
  1371. const req = new XMLHttpRequest();
  1372. req.open('POST', './?c=javascript&a=actualize', true);
  1373. req.responseType = 'json';
  1374. req.onload = function (e) {
  1375. if (this.status != 200) {
  1376. return badAjax(false);
  1377. }
  1378. const json = xmlHttpRequestJson(this);
  1379. if (!json) {
  1380. return badAjax(false);
  1381. }
  1382. if (auto && json.categories.length < 1 && json.feeds.length < 1) {
  1383. auto = false;
  1384. context.ajax_loading = false;
  1385. return false;
  1386. }
  1387. to_process = json.categories.length + json.feeds.length + nbCategoriesFirstRound;
  1388. if (json.categories.length + json.feeds.length > 0 && !document.getElementById('actualizeProgress')) {
  1389. document.body.insertAdjacentHTML('beforeend', '<div id="actualizeProgress" class="notification good">' +
  1390. json.feedback_actualize + '<br /><span class="title">/</span><br /><span class="progress">0 / ' +
  1391. to_process + '</span></div>');
  1392. } else {
  1393. openNotification(json.feedback_no_refresh, 'good');
  1394. }
  1395. if (json.categories.length > 0 && !skipCategories) {
  1396. skipCategories = true; // To avoid risk of infinite loop
  1397. nbCategoriesFirstRound = json.categories.length;
  1398. // If some dynamic OPML categories are refreshed, need to reload the list of feeds before updating them
  1399. refreshDynamicOpmls(json, () => {
  1400. context.ajax_loading = false;
  1401. actualize.click();
  1402. });
  1403. } else {
  1404. refreshFeeds(json);
  1405. }
  1406. };
  1407. req.setRequestHeader('Content-Type', 'application/json');
  1408. req.send(JSON.stringify({
  1409. _csrf: context.csrf,
  1410. }));
  1411. return false;
  1412. };
  1413. if (context.auto_actualize_feeds) {
  1414. auto = true;
  1415. actualize.click();
  1416. }
  1417. }
  1418. // </actualize>
  1419. // <notification>
  1420. let notification = null;
  1421. let notification_interval = null;
  1422. let notification_working = false;
  1423. function openNotification(msg, status) {
  1424. if (notification_working === true) {
  1425. return false;
  1426. }
  1427. notification_working = true;
  1428. notification.querySelector('.msg').innerHTML = msg;
  1429. notification.className = 'notification';
  1430. notification.classList.add(status);
  1431. if (status == 'good') {
  1432. notification_interval = setTimeout(closeNotification, 4000);
  1433. } else {
  1434. // no status or f.e. status = 'bad', give some more time to read
  1435. notification_interval = setTimeout(closeNotification, 8000);
  1436. }
  1437. }
  1438. function closeNotification() {
  1439. notification.classList.add('closed');
  1440. clearInterval(notification_interval);
  1441. notification_working = false;
  1442. }
  1443. function init_notifications() {
  1444. notification = document.getElementById('notification');
  1445. notification.querySelector('a.close').addEventListener('click', function (ev) {
  1446. closeNotification();
  1447. ev.preventDefault();
  1448. return false;
  1449. });
  1450. notification.addEventListener('mouseenter', function () {
  1451. clearInterval(notification_interval);
  1452. });
  1453. notification.addEventListener('mouseleave', function () {
  1454. notification_interval = setTimeout(closeNotification, 3000);
  1455. });
  1456. if (notification.querySelector('.msg').innerHTML.length > 0) {
  1457. notification_working = true;
  1458. if (notification.classList.contains('good')) {
  1459. notification_interval = setTimeout(closeNotification, 4000);
  1460. } else {
  1461. // no status or f.e. status = 'bad', give some more time to read
  1462. notification_interval = setTimeout(closeNotification, 8000);
  1463. }
  1464. }
  1465. }
  1466. // </notification>
  1467. // <notifs html5>
  1468. let notifs_html5_permission = 'denied';
  1469. function notifs_html5_is_supported() {
  1470. return window.Notification !== undefined;
  1471. }
  1472. function notifs_html5_ask_permission() {
  1473. try {
  1474. window.Notification.requestPermission(function () {
  1475. notifs_html5_permission = window.Notification.permission;
  1476. });
  1477. } catch (e) {
  1478. }
  1479. }
  1480. function notifs_html5_show(nb, nb_new) {
  1481. if (notifs_html5_permission !== 'granted') {
  1482. return;
  1483. }
  1484. try {
  1485. const notification = new window.Notification(context.i18n.notif_title_articles, {
  1486. icon: '../themes/icons/favicon-256-padding.png',
  1487. body: context.i18n.notif_body_new_articles.replace('%%d', nb_new) + ' ' + context.i18n.notif_body_unread_articles.replace('%%d', nb),
  1488. tag: 'freshRssNewArticles',
  1489. });
  1490. notification.onclick = function () {
  1491. delayedFunction(function () {
  1492. location.reload();
  1493. window.focus();
  1494. notification.close();
  1495. });
  1496. };
  1497. if (context.html5_notif_timeout !== 0) {
  1498. setTimeout(function () {
  1499. notification.close();
  1500. }, context.html5_notif_timeout * 1000);
  1501. }
  1502. } catch (e) {
  1503. }
  1504. }
  1505. function init_notifs_html5() {
  1506. if (!notifs_html5_is_supported()) {
  1507. return;
  1508. }
  1509. notifs_html5_permission = notifs_html5_ask_permission();
  1510. }
  1511. // </notifs html5>
  1512. function refreshUnreads() {
  1513. const req = new XMLHttpRequest();
  1514. req.open('GET', './?c=javascript&a=nbUnreadsPerFeed', true);
  1515. req.responseType = 'json';
  1516. req.onload = function (e) {
  1517. const json = xmlHttpRequestJson(this);
  1518. if (!json) {
  1519. return badAjax(false);
  1520. }
  1521. const isAll = document.querySelector('.category.all.active');
  1522. let new_articles = false;
  1523. let nbUnreadFeeds = 0;
  1524. const title = document.querySelector('.category.all .title');
  1525. const nb_unreads_before = title ? str2int(title.getAttribute('data-unread')) : 0;
  1526. Object.keys(json.feeds).forEach(function (feed_id) {
  1527. const nbUnreads = json.feeds[feed_id];
  1528. nbUnreadFeeds += nbUnreads;
  1529. feed_id = 'f_' + feed_id;
  1530. const elem = document.getElementById(feed_id);
  1531. const feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  1532. if ((incUnreadsFeed(null, feed_id, nbUnreads - feed_unreads) || isAll) && // Update of current view?
  1533. (nbUnreads - feed_unreads > 0)) {
  1534. const newArticle = document.getElementById('new-article');
  1535. newArticle.setAttribute('aria-hidden', 'false');
  1536. newArticle.style.display = 'block';
  1537. new_articles = true;
  1538. }
  1539. });
  1540. const noArticlesToShow_div = document.getElementById('noArticlesToShow');
  1541. if (nbUnreadFeeds > 0 && noArticlesToShow_div) {
  1542. noArticlesToShow_div.classList.add('hide');
  1543. }
  1544. let nbUnreadTags = 0;
  1545. Object.keys(json.tags).forEach(function (tag_id) {
  1546. const nbUnreads = json.tags[tag_id];
  1547. nbUnreadTags += nbUnreads;
  1548. const tag = document.getElementById('t_' + tag_id);
  1549. if (tag) {
  1550. tag.setAttribute('data-unread', nbUnreads);
  1551. tag.querySelector('.item-title').setAttribute('data-unread', numberFormat(nbUnreads));
  1552. }
  1553. });
  1554. const tags = document.querySelector('.category.tags');
  1555. if (tags) {
  1556. tags.setAttribute('data-unread', nbUnreadTags);
  1557. tags.querySelector('.title').setAttribute('data-unread', numberFormat(nbUnreadTags));
  1558. }
  1559. const nb_unreads = title ? str2int(title.getAttribute('data-unread')) : 0;
  1560. if (nb_unreads > 0 && new_articles) {
  1561. faviconNbUnread(nb_unreads);
  1562. const nb_new = nb_unreads - nb_unreads_before;
  1563. notifs_html5_show(nb_unreads, nb_new);
  1564. }
  1565. };
  1566. req.send();
  1567. }
  1568. function toggle_bigMarkAsRead_button() {
  1569. const bigMarkAsRead_button = document.getElementById('bigMarkAsRead');
  1570. if (bigMarkAsRead_button) {
  1571. if (document.querySelector('.flux.not_read') != null) {
  1572. bigMarkAsRead_button.style = '';
  1573. bigMarkAsRead_button.querySelector('.markAllRead').style.visibility = '';
  1574. } else {
  1575. if (bigMarkAsRead_button.querySelector('.jumpNext')) {
  1576. bigMarkAsRead_button.querySelector('.markAllRead').style.visibility = 'hidden';
  1577. } else {
  1578. bigMarkAsRead_button.querySelector('.markAllRead').style.visibility = '';
  1579. bigMarkAsRead_button.style.visibility = 'hidden';
  1580. }
  1581. }
  1582. }
  1583. }
  1584. // <endless_mode>
  1585. let url_load_more = '';
  1586. let load_more = false;
  1587. let box_load_more = null;
  1588. function load_more_posts() {
  1589. if (load_more || !url_load_more || !box_load_more) {
  1590. return;
  1591. }
  1592. load_more = true;
  1593. document.getElementById('load_more').classList.add('loading');
  1594. const req = new XMLHttpRequest();
  1595. req.open('GET', url_load_more, true);
  1596. req.responseType = 'document';
  1597. req.onload = function (e) {
  1598. const html = this.response;
  1599. const streamFooter = document.getElementById('stream-footer');
  1600. const streamAdopted = document.adoptNode(html.getElementById('stream'));
  1601. streamAdopted.querySelectorAll('.flux, .day').forEach(function (div) {
  1602. box_load_more.insertBefore(div, streamFooter);
  1603. });
  1604. const streamFooterOld = streamFooter.querySelector('.stream-footer-inner');
  1605. const streamFooterNew = streamAdopted.querySelector('.stream-footer-inner');
  1606. streamFooter.replaceChild(streamFooterNew, streamFooterOld);
  1607. const bigMarkAsRead = document.getElementById('bigMarkAsRead');
  1608. const readAll = document.querySelector('#nav_menu_read_all .read_all');
  1609. if (readAll && bigMarkAsRead && bigMarkAsRead.formAction) {
  1610. if (context.display_order === 'ASC') {
  1611. readAll.formAction = bigMarkAsRead.formAction;
  1612. } else {
  1613. bigMarkAsRead.formAction = readAll.formAction;
  1614. }
  1615. toggle_bigMarkAsRead_button();
  1616. }
  1617. document.querySelectorAll('[id^=day_]').forEach(function (div) {
  1618. const ids = document.querySelectorAll('[id="' + div.id + '"]');
  1619. for (let i = ids.length - 1; i > 0; i--) { // Keep only the first
  1620. ids[i].remove();
  1621. }
  1622. });
  1623. init_load_more(box_load_more);
  1624. const div_load_more = document.getElementById('load_more');
  1625. if (bigMarkAsRead) {
  1626. bigMarkAsRead.removeAttribute('disabled');
  1627. }
  1628. if (div_load_more) {
  1629. div_load_more.classList.remove('loading');
  1630. }
  1631. load_more = false;
  1632. };
  1633. req.send();
  1634. }
  1635. const freshrssLoadMoreEvent = document.createEvent('Event');
  1636. freshrssLoadMoreEvent.initEvent('freshrss:load-more', true, true);
  1637. function init_load_more(box) {
  1638. box_load_more = box;
  1639. document.body.dispatchEvent(freshrssLoadMoreEvent);
  1640. const next_button = document.getElementById('load_more');
  1641. if (!next_button) {
  1642. // no more article to load
  1643. url_load_more = '';
  1644. return;
  1645. }
  1646. url_load_more = next_button.getAttribute('formaction');
  1647. next_button.onclick = function (e) {
  1648. load_more_posts();
  1649. return false;
  1650. };
  1651. }
  1652. // </endless_mode>
  1653. function init_confirm_action() {
  1654. document.body.onclick = function (ev) {
  1655. const b = ev.target.closest('.confirm');
  1656. if (b) {
  1657. let str_confirmation = this.getAttribute('data-str-confirm');
  1658. if (!str_confirmation) {
  1659. str_confirmation = context.i18n.confirmation_default;
  1660. }
  1661. return confirm(str_confirmation);
  1662. }
  1663. };
  1664. document.querySelectorAll('button.confirm').forEach(function (b) { b.disabled = false; });
  1665. }
  1666. function faviconNbUnread(n) {
  1667. if (typeof n === 'undefined') {
  1668. const t = document.querySelector('.category.all .title');
  1669. n = t ? str2int(t.getAttribute('data-unread')) : 0;
  1670. }
  1671. // http://remysharp.com/2010/08/24/dynamic-favicons/
  1672. const canvas = document.createElement('canvas');
  1673. const link = document.getElementById('favicon').cloneNode(true);
  1674. const ratio = window.devicePixelRatio;
  1675. if (canvas.getContext && link) {
  1676. canvas.height = canvas.width = 16 * ratio;
  1677. const img = document.createElement('img');
  1678. img.onload = function () {
  1679. const ctx = canvas.getContext('2d');
  1680. ctx.drawImage(this, 0, 0, canvas.width, canvas.height);
  1681. if (n > 0) {
  1682. let text = '';
  1683. if (n < 1000) {
  1684. text = n;
  1685. } else if (n < 100000) {
  1686. text = Math.floor(n / 1000) + 'k';
  1687. } else {
  1688. text = 'E' + Math.floor(Math.log10(n));
  1689. }
  1690. ctx.font = 'bold ' + 9 * ratio + 'px "Arial", sans-serif';
  1691. ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
  1692. ctx.fillRect(0, 7 * ratio, ctx.measureText(text).width, 9 * ratio);
  1693. ctx.fillStyle = '#F00';
  1694. ctx.fillText(text, 0, canvas.height - 1);
  1695. }
  1696. link.href = canvas.toDataURL('image/png');
  1697. // Check if data URI has generated a real favicon and if not, fallback to standard icon
  1698. if (link.href.length > 180) {
  1699. document.querySelector('link[rel~=icon]').remove();
  1700. document.head.appendChild(link);
  1701. }
  1702. };
  1703. img.src = '../favicon.ico';
  1704. }
  1705. }
  1706. function removeFirstLoadSpinner() {
  1707. const first_load = document.getElementById('first_load');
  1708. if (first_load) {
  1709. first_load.remove();
  1710. }
  1711. }
  1712. function init_normal() {
  1713. const stream = document.getElementById('stream');
  1714. if (!stream) {
  1715. if (window.console) {
  1716. console.log('FreshRSS waiting for content…');
  1717. }
  1718. setTimeout(init_normal, 100);
  1719. return;
  1720. }
  1721. init_column_categories();
  1722. init_stream(stream);
  1723. init_actualize();
  1724. faviconNbUnread();
  1725. window.onbeforeunload = function (e) {
  1726. const sidebar = document.getElementById('sidebar');
  1727. if (sidebar) { // Save sidebar scroll position
  1728. sessionStorage.setItem('FreshRSS_sidebar_scrollTop', sidebar.scrollTop);
  1729. }
  1730. if (mark_read_queue && mark_read_queue.length > 0) {
  1731. return false;
  1732. }
  1733. };
  1734. }
  1735. function init_main_beforeDOM() {
  1736. document.scrollingElement.scrollTop = 0;
  1737. init_shortcuts();
  1738. if (['normal', 'reader', 'global'].indexOf(context.current_view) >= 0) {
  1739. init_normal();
  1740. }
  1741. }
  1742. function init_main_afterDOM() {
  1743. removeFirstLoadSpinner();
  1744. init_notifications();
  1745. init_confirm_action();
  1746. const stream = document.getElementById('stream');
  1747. if (stream) {
  1748. init_load_more(stream);
  1749. init_posts();
  1750. init_nav_entries();
  1751. init_notifs_html5();
  1752. toggle_bigMarkAsRead_button();
  1753. setTimeout(faviconNbUnread, 1000);
  1754. setInterval(refreshUnreads, 120000);
  1755. }
  1756. if (window.console) {
  1757. console.log('FreshRSS main init done.');
  1758. }
  1759. }
  1760. init_main_beforeDOM(); // Can be called before DOM is fully loaded
  1761. if (document.readyState && document.readyState !== 'loading') {
  1762. init_main_afterDOM();
  1763. } else {
  1764. if (window.console) {
  1765. console.log('FreshRSS waiting for DOMContentLoaded…');
  1766. }
  1767. document.addEventListener('DOMContentLoaded', init_main_afterDOM, false);
  1768. }
  1769. // @license-end