main.js 60 KB

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