main.js 56 KB

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