main.js 63 KB

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