main.js 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979
  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 loadLazyImages(rootElement) {
  372. rootElement.querySelectorAll('img[data-original], iframe[data-original]').forEach(function (el) {
  373. el.src = el.getAttribute('data-original');
  374. el.removeAttribute('data-original');
  375. });
  376. }
  377. function toggleContent(new_active, old_active, skipping) {
  378. // If skipping, move current without activating or marking as read
  379. if (!new_active) {
  380. return;
  381. }
  382. if (context.does_lazyload && !skipping) {
  383. loadLazyImages(new_active);
  384. }
  385. if (old_active !== new_active) {
  386. if (!skipping) {
  387. new_active.classList.add('active');
  388. }
  389. new_active.classList.add('current');
  390. if (old_active) {
  391. old_active.classList.remove('active');
  392. old_active.classList.remove('current'); // Split for IE11
  393. if (context.auto_remove_article) {
  394. removeArticle(old_active);
  395. }
  396. }
  397. } else {
  398. new_active.classList.toggle('active');
  399. }
  400. const relative_move = context.current_view === 'global';
  401. const box_to_move = relative_move ? document.getElementById('panel') : document.scrollingElement;
  402. if (context.sticky_post) { // Stick the article to the top when opened
  403. const prev_article = new_active.previousElementSibling;
  404. const nav_menu = document.querySelector('.nav_menu');
  405. let nav_menu_height = 0;
  406. if (nav_menu && (getComputedStyle(nav_menu).position === 'fixed' || getComputedStyle(nav_menu).position === 'sticky')) {
  407. nav_menu_height = nav_menu.offsetHeight;
  408. }
  409. let new_pos = new_active.offsetParent.offsetTop + new_active.offsetTop - nav_menu_height;
  410. if (prev_article && prev_article.offsetParent && new_active.offsetTop - prev_article.offsetTop <= 150) {
  411. new_pos = prev_article.offsetParent.offsetTop + prev_article.offsetTop - nav_menu_height;
  412. if (relative_move) {
  413. new_pos -= box_to_move.offsetTop;
  414. }
  415. }
  416. if (skipping) {
  417. // when skipping, this feels more natural if it’s not so near the top
  418. new_pos -= document.body.clientHeight / 4;
  419. }
  420. box_to_move.scrollTop = new_pos;
  421. }
  422. if (new_active.classList.contains('active') && !skipping) {
  423. if (context.auto_mark_article) {
  424. mark_read(new_active, true, true);
  425. }
  426. new_active.dispatchEvent(freshrssOpenArticleEvent);
  427. }
  428. onScroll();
  429. }
  430. function prev_entry(skipping) {
  431. const old_active = document.querySelector('.flux.current');
  432. let new_active = old_active;
  433. if (new_active) {
  434. do new_active = new_active.previousElementSibling;
  435. while (new_active && !new_active.classList.contains('flux'));
  436. if (!new_active) {
  437. prev_feed();
  438. }
  439. } else {
  440. new_active = document.querySelector('.flux');
  441. }
  442. if (context.auto_mark_focus && !new_active.classList.contains('keep_unread')) {
  443. mark_read(new_active, true, true);
  444. }
  445. toggleContent(new_active, old_active, skipping);
  446. }
  447. function next_entry(skipping) {
  448. const old_active = document.querySelector('.flux.current');
  449. let new_active = old_active;
  450. if (new_active) {
  451. do new_active = new_active.nextElementSibling;
  452. while (new_active && !new_active.classList.contains('flux'));
  453. if (!new_active) {
  454. next_feed();
  455. }
  456. } else {
  457. new_active = document.querySelector('.flux');
  458. }
  459. if (context.auto_mark_focus && !new_active.classList.contains('keep_unread')) {
  460. mark_read(new_active, true, true);
  461. }
  462. toggleContent(new_active, old_active, skipping);
  463. }
  464. function next_unread_entry(skipping) {
  465. const old_active = document.querySelector('.flux.current');
  466. let new_active = old_active;
  467. if (new_active) {
  468. do new_active = new_active.nextElementSibling;
  469. while (new_active && !new_active.classList.contains('not_read'));
  470. if (!new_active) {
  471. next_feed();
  472. }
  473. } else {
  474. new_active = document.querySelector('.not_read');
  475. }
  476. if (context.auto_mark_focus && !new_active.classList.contains('keep_unread')) {
  477. mark_read(new_active, true, true);
  478. }
  479. toggleContent(new_active, old_active, skipping);
  480. }
  481. function prev_feed() {
  482. let found = false;
  483. let adjacent = null;
  484. const feeds = document.querySelectorAll('#aside_feed .feed');
  485. for (let i = feeds.length - 1; i >= 0; i--) {
  486. const feed = feeds[i];
  487. if (feed.classList.contains('active')) {
  488. found = true;
  489. continue;
  490. }
  491. if (!found) {
  492. continue;
  493. }
  494. if (getComputedStyle(feed).display === 'none') {
  495. continue;
  496. }
  497. if (feed.dataset.unread != 0) {
  498. return delayedClick(feed.querySelector('a.item-title'));
  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. return delayedClick(feed.querySelector('a.item-title'));
  527. } else if (adjacent === null) {
  528. adjacent = feed;
  529. }
  530. }
  531. if (found) {
  532. delayedClick(adjacent.querySelector('a.item-title'));
  533. } else {
  534. first_feed();
  535. }
  536. }
  537. function first_feed() {
  538. const a = document.querySelector('#aside_feed .category.active .feed:not([data-unread="0"]) a.item-title');
  539. delayedClick(a);
  540. }
  541. function last_feed() {
  542. const links = document.querySelectorAll('#aside_feed .category.active .feed:not([data-unread="0"]) a.item-title');
  543. if (links && links.length > 0) {
  544. delayedClick(links[links.length - 1]);
  545. }
  546. }
  547. function prev_category() {
  548. const active_cat = document.querySelector('#aside_feed .category.active');
  549. if (active_cat) {
  550. let cat = active_cat;
  551. do cat = cat.previousElementSibling;
  552. while (cat && getComputedStyle(cat).display === 'none');
  553. if (cat) {
  554. delayedClick(cat.querySelector('a.title'));
  555. }
  556. } else {
  557. last_category();
  558. }
  559. }
  560. function next_category() {
  561. const active_cat = document.querySelector('#aside_feed .category.active');
  562. if (active_cat) {
  563. let cat = active_cat;
  564. do cat = cat.nextElementSibling;
  565. while (cat && getComputedStyle(cat).display === 'none');
  566. if (cat) {
  567. delayedClick(cat.querySelector('a.title'));
  568. }
  569. } else {
  570. first_category();
  571. }
  572. }
  573. function next_unread_category() {
  574. const active_cat = document.querySelector('#aside_feed .category.active');
  575. if (active_cat) {
  576. let cat = active_cat;
  577. do cat = cat.nextElementSibling;
  578. while (cat && cat.getAttribute('data-unread') <= 0);
  579. if (cat) {
  580. delayedClick(cat.querySelector('a.title'));
  581. }
  582. } else {
  583. first_category();
  584. }
  585. }
  586. function first_category() {
  587. const a = document.querySelector('#aside_feed .category:not([data-unread="0"]) a.title');
  588. delayedClick(a);
  589. }
  590. function last_category() {
  591. const links = document.querySelectorAll('#aside_feed .category:not([data-unread="0"]) a.title');
  592. if (links && links.length > 0) {
  593. delayedClick(links[links.length - 1]);
  594. }
  595. }
  596. function collapse_entry() {
  597. const flux_current = document.querySelector('.flux.current');
  598. toggleContent(flux_current, flux_current, false);
  599. }
  600. function toggle_media() {
  601. const media = document.querySelector('.flux.current video,.flux.current audio');
  602. if (media === null) {
  603. return;
  604. }
  605. if (media.paused) {
  606. media.play();
  607. } else {
  608. media.pause();
  609. }
  610. }
  611. function user_filter(key) {
  612. const filter = document.getElementById('dropdown-query');
  613. const filters = filter.parentElement.querySelectorAll('.dropdown-menu > .query > a');
  614. if (typeof key === 'undefined') {
  615. if (!filters.length) {
  616. return;
  617. }
  618. // Display the filter div
  619. location.hash = filter.id;
  620. // Force scrolling to the filter div
  621. const scroll = needsScroll(document.querySelector('.header'));
  622. if (scroll !== 0) {
  623. document.scrollingElement.scrollTop = scroll;
  624. }
  625. // Force the key value if there is only one action, so we can trigger it automatically
  626. if (filters.length === 1) {
  627. key = 1;
  628. } else {
  629. return;
  630. }
  631. }
  632. // Trigger selected share action
  633. key = parseInt(key);
  634. if (key <= filters.length) {
  635. filters[key - 1].click();
  636. }
  637. }
  638. function auto_share(key) {
  639. const share = document.querySelector('.flux.current.active .dropdown-target[id^="dropdown-share"]');
  640. if (!share) {
  641. return;
  642. }
  643. const shares = share.parentElement.querySelectorAll('.dropdown-menu .item [data-type]');
  644. if (typeof key === 'undefined') {
  645. // Display the share div
  646. location.hash = share.id;
  647. // Force scrolling to the share div
  648. const scrollTop = needsScroll(share.closest('.bottom'));
  649. if (scrollTop !== 0) {
  650. document.scrollingElement.scrollTop = scrollTop;
  651. }
  652. // Force the key value if there is only one action, so we can trigger it automatically
  653. if (shares.length === 1) {
  654. key = 1;
  655. } else {
  656. return;
  657. }
  658. }
  659. // Trigger selected share action and hide the share div
  660. key = parseInt(key);
  661. if (key <= shares.length) {
  662. shares[key - 1].click();
  663. share.parentElement.querySelector('.dropdown-menu + a.dropdown-close').click();
  664. }
  665. }
  666. let box_to_follow;
  667. function onScroll() {
  668. if (!box_to_follow) {
  669. return;
  670. }
  671. if (context.auto_mark_scroll) {
  672. const hidden_px = -5; // negative = pixels over the edge
  673. const minTop = hidden_px + box_to_follow.scrollTop;
  674. document.querySelectorAll('.not_read:not(.keep_unread)').forEach(function (div) {
  675. if (div.offsetHeight > 0 &&
  676. div.offsetParent.offsetTop + div.offsetTop + div.offsetHeight < minTop) {
  677. mark_read(div, true, true);
  678. }
  679. });
  680. }
  681. let streamFooter;
  682. if (context.auto_load_more && (streamFooter = document.getElementById('stream-footer'))) {
  683. if (box_to_follow.offsetHeight > 0 &&
  684. box_to_follow.scrollTop + box_to_follow.offsetHeight + (window.innerHeight / 2) >= streamFooter.offsetTop) {
  685. // Too close to the last pre-loaded article
  686. load_more_posts();
  687. } else {
  688. const after = document.querySelectorAll('.flux.current ~ .flux').length;
  689. if (after > 0 && after <= 5) {
  690. // Too few pre-loaded articles
  691. load_more_posts();
  692. }
  693. }
  694. }
  695. }
  696. let lastScroll = 0; // Throttle
  697. let timerId = 0;
  698. function debouncedOnScroll() {
  699. clearTimeout(timerId);
  700. if (lastScroll + 500 < Date.now()) {
  701. lastScroll = Date.now();
  702. onScroll();
  703. } else {
  704. timerId = setTimeout(onScroll, 500);
  705. }
  706. }
  707. function init_posts() {
  708. if (context.auto_load_more || context.auto_mark_scroll || context.auto_remove_article) {
  709. box_to_follow = context.current_view === 'global' ? document.getElementById('panel') : document.scrollingElement;
  710. (box_to_follow === document.scrollingElement ? window : box_to_follow).onscroll = debouncedOnScroll;
  711. window.addEventListener('resize', debouncedOnScroll);
  712. onScroll();
  713. }
  714. if (!navigator.share && document.styleSheets.length > 0) {
  715. // https://developer.mozilla.org/en-US/docs/Web/API/Navigator/share
  716. // do not show the menu entry if browser does not support navigator.share
  717. document.styleSheets[0].insertRule(
  718. 'button.as-link[data-type="web-sharing-api"] {display: none !important;}',
  719. document.styleSheets[0].cssRules.length
  720. );
  721. }
  722. }
  723. function rememberOpenCategory(category_id, isOpen) {
  724. if (context.display_categories === 'remember') {
  725. const open_categories = JSON.parse(localStorage.getItem('FreshRSS_open_categories') || '{}');
  726. if (isOpen) {
  727. open_categories[category_id] = true;
  728. } else {
  729. delete open_categories[category_id];
  730. }
  731. localStorage.setItem('FreshRSS_open_categories', JSON.stringify(open_categories));
  732. }
  733. }
  734. function openCategory(category_id) {
  735. const category_element = document.getElementById(category_id);
  736. if (!category_element) return;
  737. category_element.querySelector('.tree-folder-items').classList.add('active');
  738. const img = category_element.querySelector('button.dropdown-toggle img');
  739. if (!img) return;
  740. img.src = img.src.replace('/icons/down.', '/icons/up.');
  741. img.alt = '🔼';
  742. }
  743. function loadJs(name) {
  744. if (!document.getElementById(name)) {
  745. const script = document.createElement('script');
  746. script.setAttribute('id', name);
  747. script.setAttribute('src', '../scripts/' + name + '?' + context.mtime[name]);
  748. script.setAttribute('defer', 'defer');
  749. script.setAttribute('async', 'async');
  750. document.head.appendChild(script);
  751. }
  752. }
  753. function init_column_categories() {
  754. if (context.current_view !== 'normal' && context.current_view !== 'reader') {
  755. return;
  756. }
  757. // Restore sidebar scroll position
  758. document.getElementById('sidebar').scrollTop = +sessionStorage.getItem('FreshRSS_sidebar_scrollTop');
  759. // Restore open categories
  760. if (context.display_categories === 'remember') {
  761. const open_categories = JSON.parse(localStorage.getItem('FreshRSS_open_categories') || '{}');
  762. Object.keys(open_categories).forEach(function (category_id) {
  763. openCategory(category_id);
  764. });
  765. }
  766. document.getElementById('aside_feed').onclick = function (ev) {
  767. let a = ev.target.closest('.tree-folder > .tree-folder-title > button.dropdown-toggle');
  768. if (a) {
  769. const icon = a.querySelector('.icon');
  770. const category_id = a.closest('.category').id;
  771. if (icon.alt === '🔽' || icon.innerHTML === '🔽') {
  772. if (icon.src) {
  773. icon.src = icon.src.replace('/icons/down.', '/icons/up.');
  774. icon.alt = '🔼';
  775. } else {
  776. icon.innerHTML = '🔼';
  777. }
  778. rememberOpenCategory(category_id, true);
  779. } else {
  780. if (icon.src) {
  781. icon.src = icon.src.replace('/icons/up.', '/icons/down.');
  782. icon.alt = '🔽';
  783. } else {
  784. icon.innerHTML = '🔽';
  785. }
  786. rememberOpenCategory(category_id, false);
  787. }
  788. const ul = a.closest('li').querySelector('.tree-folder-items');
  789. let nbVisibleItems = 0;
  790. for (let i = ul.children.length - 1; i >= 0; i--) {
  791. if (ul.children[i].offsetHeight) {
  792. nbVisibleItems++;
  793. }
  794. }
  795. ul.classList.toggle('active');
  796. // CSS transition does not work on max-height:auto
  797. ul.style.maxHeight = ul.classList.contains('active') ? (nbVisibleItems * 4) + 'em' : 0;
  798. return false;
  799. }
  800. a = ev.target.closest('.tree-folder-items > .feed .dropdown-toggle');
  801. if (a) {
  802. loadJs('extra.js');
  803. loadJs('feed.js');
  804. const itemId = a.closest('.item').id;
  805. const templateId = itemId.substring(0, 2) === 't_' ? 'tag_config_template' : 'feed_config_template';
  806. const id = itemId.substr(2);
  807. const feed_web = a.getAttribute('data-fweb') || '';
  808. const div = a.parentElement;
  809. const dropdownMenu = div.querySelector('.dropdown-menu');
  810. const template = document.getElementById(templateId)
  811. .innerHTML.replace(/------/g, id).replace('http://example.net/', feed_web);
  812. if (!dropdownMenu) {
  813. div.insertAdjacentHTML('beforeend', template);
  814. if (feed_web == '') {
  815. const website = div.querySelector('.item.link.website');
  816. if (website) {
  817. website.remove();
  818. }
  819. }
  820. const b = div.querySelector('button.confirm');
  821. if (b) {
  822. b.disabled = false;
  823. }
  824. } else if (getComputedStyle(dropdownMenu).display === 'none') {
  825. const id2 = div.closest('.item').id.substr(2);
  826. a.href = '#dropdown-' + id2;
  827. } else {
  828. a.href = '#close';
  829. }
  830. return true;
  831. }
  832. return true;
  833. };
  834. }
  835. function init_shortcuts() {
  836. Object.keys(context.shortcuts).forEach(function (k) {
  837. context.shortcuts[k] = (context.shortcuts[k] || '').toUpperCase();
  838. });
  839. document.addEventListener('keydown', ev => {
  840. if (ev.target.closest('input, textarea') ||
  841. ev.ctrlKey || ev.metaKey || (ev.altKey && ev.shiftKey)) {
  842. return;
  843. }
  844. const s = context.shortcuts;
  845. let k = (ev.key.trim() || ev.code || 'Space').toUpperCase();
  846. // IE11
  847. if (k === 'SPACEBAR') k = 'SPACE';
  848. else if (k === 'DEL') k = 'DELETE';
  849. else if (k === 'ESC') k = 'ESCAPE';
  850. if (location.hash.match(/^#dropdown-/)) {
  851. const n = parseInt(k);
  852. if (n) {
  853. if (location.hash === '#dropdown-query') {
  854. user_filter(n);
  855. } else {
  856. auto_share(n);
  857. }
  858. ev.preventDefault();
  859. return;
  860. }
  861. }
  862. if (k === s.actualize) {
  863. const btn = document.getElementById('actualize');
  864. if (btn) {
  865. btn.click();
  866. }
  867. ev.preventDefault();
  868. return;
  869. }
  870. if (k === s.next_entry) {
  871. if (ev.altKey) {
  872. next_category();
  873. } else if (ev.shiftKey) {
  874. next_feed();
  875. } else {
  876. next_entry(false);
  877. }
  878. ev.preventDefault();
  879. return;
  880. }
  881. if (k === s.next_unread_entry) {
  882. if (ev.altKey) {
  883. next_unread_category();
  884. } else if (ev.shiftKey) {
  885. next_feed();
  886. } else {
  887. next_unread_entry(false);
  888. }
  889. ev.preventDefault();
  890. return;
  891. }
  892. if (k === s.prev_entry) {
  893. if (ev.altKey) {
  894. prev_category();
  895. } else if (ev.shiftKey) {
  896. prev_feed();
  897. } else {
  898. prev_entry(false);
  899. }
  900. ev.preventDefault();
  901. return;
  902. }
  903. if (k === s.mark_read) {
  904. if (ev.altKey) {
  905. mark_previous_read(document.querySelector('.flux.current'));
  906. } else if (ev.shiftKey) {
  907. document.querySelector('.nav_menu .read_all').click();
  908. } else { // Toggle the read state
  909. mark_read(document.querySelector('.flux.current'), false, false);
  910. }
  911. ev.preventDefault();
  912. return;
  913. }
  914. if (k === s.first_entry) {
  915. if (ev.altKey) {
  916. first_category();
  917. } else if (ev.shiftKey) {
  918. first_feed();
  919. } else {
  920. const old_active = document.querySelector('.flux.current');
  921. const first = document.querySelector('.flux');
  922. if (first.classList.contains('flux')) {
  923. toggleContent(first, old_active, false);
  924. }
  925. }
  926. ev.preventDefault();
  927. return;
  928. }
  929. if (k === s.last_entry) {
  930. if (ev.altKey) {
  931. last_category();
  932. } else if (ev.shiftKey) {
  933. last_feed();
  934. } else {
  935. const old_active = document.querySelector('.flux.current');
  936. const last = document.querySelector('.flux:last-of-type');
  937. if (last.classList.contains('flux')) {
  938. toggleContent(last, old_active, false);
  939. }
  940. }
  941. ev.preventDefault();
  942. return;
  943. }
  944. if (ev.altKey || ev.shiftKey) {
  945. return;
  946. }
  947. if (k === s.mark_favorite) { // Toggle the favorite state
  948. mark_favorite(document.querySelector('.flux.current'));
  949. ev.preventDefault();
  950. return;
  951. }
  952. if (k === s.go_website) {
  953. if (context.auto_mark_site) {
  954. mark_read(document.querySelector('.flux.current'), true, false);
  955. }
  956. const link_go_website = document.querySelector('.flux.current a.go_website');
  957. if (link_go_website) {
  958. const newWindow = window.open();
  959. if (newWindow) {
  960. newWindow.opener = null;
  961. newWindow.location = link_go_website.href;
  962. ev.preventDefault();
  963. }
  964. }
  965. return;
  966. }
  967. if (k === s.skip_next_entry) { next_entry(true); ev.preventDefault(); return; }
  968. if (k === s.skip_prev_entry) { prev_entry(true); ev.preventDefault(); return; }
  969. if (k === s.collapse_entry) { collapse_entry(); ev.preventDefault(); return; }
  970. if (k === s.auto_share) { auto_share(); ev.preventDefault(); return; }
  971. if (k === s.user_filter) { user_filter(); ev.preventDefault(); return; }
  972. if (k === s.load_more) { load_more_posts(); ev.preventDefault(); return; }
  973. if (k === s.close_dropdown) { location.hash = null; ev.preventDefault(); return; }
  974. if (k === s.help) { window.open(context.urls.help); ev.preventDefault(); return; }
  975. if (k === s.focus_search) { document.getElementById('search').focus(); ev.preventDefault(); return; }
  976. if (k === s.normal_view) { delayedClick(document.querySelector('#nav_menu_views .view-normal')); ev.preventDefault(); return; }
  977. if (k === s.reading_view) { delayedClick(document.querySelector('#nav_menu_views .view-reader')); ev.preventDefault(); return; }
  978. if (k === s.global_view) { delayedClick(document.querySelector('#nav_menu_views .view-global')); 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 a.title');
  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. const flux_content = el.closest('.flux_content');
  1018. let content_el = null;
  1019. if (flux_content) {
  1020. content_el = el.closest('.flux_content').querySelector('.content');
  1021. }
  1022. if (content_el === null) {
  1023. content_el = el.closest('.flux').querySelector('.flux_content .content');
  1024. }
  1025. loadLazyImages(content_el);
  1026. tmp_window.document.writeln(content_el.innerHTML);
  1027. tmp_window.document.close();
  1028. tmp_window.focus();
  1029. tmp_window.print();
  1030. tmp_window.close();
  1031. return false;
  1032. }
  1033. el = ev.target.closest('.item.share > button[data-type="clipboard"]');
  1034. if (el) { // Clipboard
  1035. if (navigator.clipboard) {
  1036. navigator.clipboard.writeText(el.dataset.url)
  1037. .then(() => {
  1038. toggleClass(el, 'ok');
  1039. })
  1040. .catch(e => {
  1041. console.log(e);
  1042. toggleClass(el, 'error');
  1043. });
  1044. } else {
  1045. // fallback, if navigator.clipboard is not available f.e. if access is not via https or localhost
  1046. const inputElement = document.createElement('input');
  1047. inputElement.value = el.dataset.url;
  1048. document.body.appendChild(inputElement);
  1049. inputElement.select();
  1050. if (document.execCommand && document.execCommand('copy')) {
  1051. toggleClass(el, 'ok');
  1052. } else {
  1053. console.log('document.execCommand("copy") failed');
  1054. toggleClass(el, 'error');
  1055. }
  1056. inputElement.remove();
  1057. }
  1058. return false;
  1059. }
  1060. el = ev.target.closest('.item.share > button[data-type="web-sharing-api"]');
  1061. if (el && navigator.share) { // https://developer.mozilla.org/en-US/docs/Web/API/Navigator/share
  1062. const shareData = {
  1063. url: el.dataset.url,
  1064. title: decodeURI(el.dataset.title),
  1065. };
  1066. navigator.share(shareData);
  1067. return false;
  1068. }
  1069. el = ev.target.closest('.item.share > a[data-type="email-webmail-firefox-fix"]');
  1070. if (el) {
  1071. window.open(el.href);
  1072. return false;
  1073. }
  1074. el = ev.target.closest('.item.share > a[href="POST"]');
  1075. if (el) { // Share by POST
  1076. const f = el.parentElement.querySelector('form');
  1077. f.disabled = false;
  1078. f.submit();
  1079. return false;
  1080. }
  1081. el = ev.target.closest('.flux_header, .flux_content');
  1082. if (el) { // flux_toggle
  1083. if (ev.target.closest('.reader, .content, .item.website, .item.link, .dropdown')) {
  1084. return true;
  1085. }
  1086. if ((!context.sides_close_article && ev.target.matches('.flux_content')) || ev.target.closest('footer')) {
  1087. // setting for not-closing after clicking outside article area
  1088. return false;
  1089. }
  1090. const old_active = document.querySelector('.flux.current');
  1091. const new_active = el.parentNode;
  1092. if (ev.target.tagName.toUpperCase() === 'A') { // Leave real links alone (but does not catch img in a link)
  1093. if (context.auto_mark_article) {
  1094. mark_read(new_active, true, false);
  1095. }
  1096. return true;
  1097. }
  1098. toggleContent(new_active, old_active, false);
  1099. return false;
  1100. }
  1101. };
  1102. stream.onmouseup = function (ev) { // Mouseup enables us to catch middle click, and control+click in IE/Edge
  1103. if (ev.altKey || ev.metaKey || ev.shiftKey) {
  1104. return;
  1105. }
  1106. let el = ev.target.closest('.item a.title');
  1107. if (el) {
  1108. if (ev.which == 1) {
  1109. if (ev.ctrlKey) { // Control+click
  1110. if (context.auto_mark_site) {
  1111. mark_read(el.closest('.flux'), true, false);
  1112. }
  1113. } else {
  1114. el.parentElement.click(); // Normal click, just toggle article.
  1115. }
  1116. } else if (ev.which == 2 && !ev.ctrlKey) { // Simple middle click: same behaviour as CTRL+click
  1117. if (context.auto_mark_article) {
  1118. const new_active = el.closest('.flux');
  1119. mark_read(new_active, true, false);
  1120. }
  1121. }
  1122. return;
  1123. }
  1124. if (context.auto_mark_site) {
  1125. // catch mouseup instead of click so we can have the correct behaviour
  1126. // with middle button click (scroll button).
  1127. el = ev.target.closest('.flux .link > a');
  1128. if (el) {
  1129. if (ev.which == 3) {
  1130. return;
  1131. }
  1132. mark_read(el.closest('.flux'), true, false);
  1133. }
  1134. }
  1135. };
  1136. stream.onchange = function (ev) {
  1137. const checkboxTag = ev.target.closest('.checkboxTag');
  1138. if (checkboxTag) { // Dynamic tags
  1139. ev.stopPropagation();
  1140. const tagId = checkboxTag.name.replace(/^t_/, '');
  1141. const tagName = checkboxTag.nextElementSibling ? checkboxTag.nextElementSibling.childNodes[0].value : '';
  1142. if ((tagId == 0 && tagName.length > 0) || tagId != 0) {
  1143. const isChecked = checkboxTag.checked;
  1144. const entry = checkboxTag.closest('div.flux');
  1145. const entryId = entry.id.replace(/^flux_/, '');
  1146. checkboxTag.disabled = true;
  1147. const req = new XMLHttpRequest();
  1148. req.open('POST', './?c=tag&a=tagEntry&ajax=1', true);
  1149. req.responseType = 'json';
  1150. req.onerror = function (e) {
  1151. checkboxTag.checked = !isChecked;
  1152. badAjax(this.status == 403);
  1153. };
  1154. req.onload = function (e) {
  1155. if (this.status != 200) {
  1156. return req.onerror(e);
  1157. }
  1158. if (entry.classList.contains('not_read')) {
  1159. incUnreadsTag('t_' + tagId, isChecked ? 1 : -1);
  1160. }
  1161. };
  1162. req.onloadend = function (e) {
  1163. checkboxTag.disabled = false;
  1164. if (tagId == 0) {
  1165. loadDynamicTags(checkboxTag.closest('div.dropdown'));
  1166. }
  1167. };
  1168. req.setRequestHeader('Content-Type', 'application/json');
  1169. req.send(JSON.stringify({
  1170. _csrf: context.csrf,
  1171. id_tag: tagId,
  1172. name_tag: tagId == 0 ? tagName : '',
  1173. id_entry: entryId,
  1174. checked: isChecked,
  1175. ajax: 1,
  1176. }));
  1177. }
  1178. }
  1179. };
  1180. }
  1181. function toggleClass(el, cssclass) {
  1182. el.classList.remove(cssclass);
  1183. el.dataset.foo = el.offsetWidth; // it does nothing, but it is needed. See https://github.com/FreshRSS/FreshRSS/pull/5295
  1184. el.classList.add(cssclass);
  1185. }
  1186. function init_nav_entries() {
  1187. const nav_entries = document.getElementById('nav_entries');
  1188. if (nav_entries) {
  1189. nav_entries.querySelector('.previous_entry').onclick = function (e) {
  1190. prev_entry(false);
  1191. return false;
  1192. };
  1193. nav_entries.querySelector('.next_entry').onclick = function (e) {
  1194. next_entry(false);
  1195. return false;
  1196. };
  1197. nav_entries.querySelector('.up').onclick = function (e) {
  1198. const active_item = (document.querySelector('.flux.current') || document.querySelector('.flux'));
  1199. const windowTop = document.scrollingElement.scrollTop;
  1200. const item_top = active_item.offsetParent.offsetTop + active_item.offsetTop;
  1201. const nav_menu = document.querySelector('.nav_menu');
  1202. let nav_menu_height = 0;
  1203. if (getComputedStyle(nav_menu).position === 'fixed' || getComputedStyle(nav_menu).position === 'sticky') {
  1204. nav_menu_height = nav_menu.offsetHeight;
  1205. }
  1206. document.scrollingElement.scrollTop = windowTop > item_top ? item_top - nav_menu_height : 0 - nav_menu_height;
  1207. return false;
  1208. };
  1209. }
  1210. }
  1211. function loadDynamicTags(div) {
  1212. div.classList.remove('dynamictags');
  1213. div.querySelectorAll('li.item').forEach(function (li) { li.remove(); });
  1214. const entryId = div.closest('div.flux').id.replace(/^flux_/, '');
  1215. const req = new XMLHttpRequest();
  1216. req.open('GET', './?c=tag&a=getTagsForEntry&id_entry=' + entryId, true);
  1217. req.responseType = 'json';
  1218. req.onerror = function (e) {
  1219. div.querySelectorAll('li.item').forEach(function (li) { li.remove(); });
  1220. div.classList.add('dynamictags');
  1221. };
  1222. req.onload = function (e) {
  1223. if (this.status != 200) {
  1224. return req.onerror(e);
  1225. }
  1226. const json = xmlHttpRequestJson(this);
  1227. if (!json) {
  1228. return req.onerror(e);
  1229. }
  1230. if (!context.anonymous) {
  1231. const li_item0 = document.createElement('li');
  1232. li_item0.setAttribute('class', 'item addItem');
  1233. const label = document.createElement('label');
  1234. label.setAttribute('class', 'noHover');
  1235. const input_checkboxTag = document.createElement('input');
  1236. input_checkboxTag.setAttribute('class', 'checkboxTag checkboxNewTag');
  1237. input_checkboxTag.setAttribute('name', 't_0');
  1238. input_checkboxTag.setAttribute('type', 'checkbox');
  1239. const input_newTag = document.createElement('input');
  1240. input_newTag.setAttribute('type', 'text');
  1241. input_newTag.setAttribute('name', 'newTag');
  1242. input_newTag.addEventListener('keydown', function (ev) { if (ev.key.toUpperCase() == 'ENTER') { this.parentNode.previousSibling.click(); } });
  1243. const button_btn = document.createElement('button');
  1244. button_btn.setAttribute('type', 'button');
  1245. button_btn.setAttribute('class', 'btn');
  1246. button_btn.addEventListener('click', function () { this.parentNode.parentNode.click(); });
  1247. const text_plus = document.createTextNode('+');
  1248. const div_stick = document.createElement('div');
  1249. div_stick.setAttribute('class', 'stick');
  1250. button_btn.appendChild(text_plus);
  1251. div_stick.appendChild(input_newTag);
  1252. div_stick.appendChild(button_btn);
  1253. label.appendChild(input_checkboxTag);
  1254. label.appendChild(div_stick);
  1255. li_item0.appendChild(label);
  1256. div.querySelector('.dropdown-menu').appendChild(li_item0);
  1257. }
  1258. let html = '';
  1259. if (json && json.length) {
  1260. let nbLabelsChecked = 0;
  1261. for (let i = 0; i < json.length; i++) {
  1262. const tag = json[i];
  1263. if (context.anonymous && !tag.checked) {
  1264. // In anomymous mode, show only the used tags
  1265. continue;
  1266. }
  1267. if (tag.checked) {
  1268. nbLabelsChecked++;
  1269. }
  1270. html += '<li class="item"><label><input ' +
  1271. (context.anonymous ? '' : 'class="checkboxTag" ') +
  1272. 'name="t_' + tag.id + '"type="checkbox" ' +
  1273. (context.anonymous ? 'disabled="disabled" ' : '') +
  1274. (tag.checked ? 'checked="checked" ' : '') + '/> ' + tag.name + '</label></li>';
  1275. }
  1276. if (context.anonymous && nbLabelsChecked === 0) {
  1277. html += '<li class="item"><span class="emptyLabels">' + context.i18n.labels_empty + '</span></li>';
  1278. }
  1279. }
  1280. div.querySelector('.dropdown-menu').insertAdjacentHTML('beforeend', html);
  1281. };
  1282. req.send();
  1283. }
  1284. // <actualize>
  1285. let feeds_processed = 0;
  1286. let categories_processed = 0;
  1287. let to_process = 0;
  1288. function refreshFeed(feeds, feeds_count) {
  1289. const feed = feeds.pop();
  1290. if (!feed) {
  1291. return;
  1292. }
  1293. const req = new XMLHttpRequest();
  1294. req.open('POST', feed.url, true);
  1295. req.onloadend = function (e) {
  1296. feeds_processed++;
  1297. if (this.status != 200) {
  1298. badAjax(false);
  1299. } else {
  1300. const div = document.getElementById('actualizeProgress');
  1301. div.querySelector('.progress').innerHTML = (categories_processed + feeds_processed) + ' / ' + to_process;
  1302. div.querySelector('.title').innerHTML = feed.title;
  1303. }
  1304. if (feeds_processed === feeds_count) {
  1305. // Empty request to commit new articles
  1306. const req2 = new XMLHttpRequest();
  1307. req2.open('POST', './?c=feed&a=actualize&id=-1&ajax=1', true);
  1308. req2.onloadend = function (e) {
  1309. delayedFunction(function () { location.reload(); });
  1310. };
  1311. req2.setRequestHeader('Content-Type', 'application/json');
  1312. req2.send(JSON.stringify({
  1313. _csrf: context.csrf,
  1314. noCommit: 0,
  1315. }));
  1316. } else {
  1317. refreshFeed(feeds, feeds_count);
  1318. }
  1319. };
  1320. req.setRequestHeader('Content-Type', 'application/json');
  1321. req.send(JSON.stringify({
  1322. _csrf: context.csrf,
  1323. noCommit: 1,
  1324. }));
  1325. }
  1326. function refreshFeeds(json) {
  1327. feeds_processed = 0;
  1328. if (!json.feeds || json.feeds.length === 0) {
  1329. // Empty request to commit new articles
  1330. const req2 = new XMLHttpRequest();
  1331. req2.open('POST', './?c=feed&a=actualize&id=-1&ajax=1', true);
  1332. req2.onloadend = function (e) {
  1333. context.ajax_loading = false;
  1334. };
  1335. req2.setRequestHeader('Content-Type', 'application/json');
  1336. req2.send(JSON.stringify({
  1337. _csrf: context.csrf,
  1338. noCommit: 0,
  1339. }));
  1340. } else {
  1341. const feeds_count = json.feeds.length;
  1342. for (let i = context.nb_parallel_refresh; i > 0; i--) {
  1343. refreshFeed(json.feeds, feeds_count);
  1344. }
  1345. }
  1346. }
  1347. function refreshDynamicOpml(categories, categories_count, next) {
  1348. const category = categories.pop();
  1349. if (!category) {
  1350. return;
  1351. }
  1352. const req = new XMLHttpRequest();
  1353. req.open('POST', category.url, true);
  1354. req.onloadend = function (e) {
  1355. categories_processed++;
  1356. if (this.status != 200) {
  1357. badAjax(false);
  1358. } else {
  1359. const div = document.getElementById('actualizeProgress');
  1360. div.querySelector('.progress').innerHTML = (categories_processed + feeds_processed) + ' / ' + to_process;
  1361. div.querySelector('.title').innerHTML = category.title;
  1362. }
  1363. if (categories_processed === categories_count) {
  1364. if (next) { next(); }
  1365. } else {
  1366. refreshDynamicOpml(categories, categories_count, next);
  1367. }
  1368. };
  1369. req.setRequestHeader('Content-Type', 'application/json');
  1370. req.send(JSON.stringify({
  1371. _csrf: context.csrf,
  1372. noCommit: 1,
  1373. }));
  1374. }
  1375. function refreshDynamicOpmls(json, next) {
  1376. categories_processed = 0;
  1377. if (json.categories && json.categories.length > 0) {
  1378. const categories_count = json.categories.length;
  1379. for (let i = context.nb_parallel_refresh; i > 0; i--) {
  1380. refreshDynamicOpml(json.categories, categories_count, next);
  1381. }
  1382. } else {
  1383. if (next) { next(); }
  1384. }
  1385. }
  1386. function init_actualize() {
  1387. let auto = false;
  1388. let nbCategoriesFirstRound = 0;
  1389. let skipCategories = false;
  1390. const actualize = document.getElementById('actualize');
  1391. if (!actualize) {
  1392. return;
  1393. }
  1394. actualize.onclick = function () {
  1395. if (context.ajax_loading) {
  1396. return false;
  1397. }
  1398. context.ajax_loading = true;
  1399. const req = new XMLHttpRequest();
  1400. req.open('POST', './?c=javascript&a=actualize', true);
  1401. req.responseType = 'json';
  1402. req.onload = function (e) {
  1403. if (this.status != 200) {
  1404. return badAjax(false);
  1405. }
  1406. const json = xmlHttpRequestJson(this);
  1407. if (!json) {
  1408. return badAjax(false);
  1409. }
  1410. if (auto && json.categories.length < 1 && json.feeds.length < 1) {
  1411. auto = false;
  1412. context.ajax_loading = false;
  1413. return false;
  1414. }
  1415. to_process = json.categories.length + json.feeds.length + nbCategoriesFirstRound;
  1416. if (json.categories.length + json.feeds.length > 0 && !document.getElementById('actualizeProgress')) {
  1417. document.body.insertAdjacentHTML('beforeend', '<div id="actualizeProgress" class="notification good">' +
  1418. json.feedback_actualize + '<br /><span class="title">/</span><br /><span class="progress">0 / ' +
  1419. to_process + '</span></div>');
  1420. } else {
  1421. openNotification(json.feedback_no_refresh, 'good');
  1422. }
  1423. if (json.categories.length > 0 && !skipCategories) {
  1424. skipCategories = true; // To avoid risk of infinite loop
  1425. nbCategoriesFirstRound = json.categories.length;
  1426. // If some dynamic OPML categories are refreshed, need to reload the list of feeds before updating them
  1427. refreshDynamicOpmls(json, () => {
  1428. context.ajax_loading = false;
  1429. actualize.click();
  1430. });
  1431. } else {
  1432. refreshFeeds(json);
  1433. }
  1434. };
  1435. req.setRequestHeader('Content-Type', 'application/json');
  1436. req.send(JSON.stringify({
  1437. _csrf: context.csrf,
  1438. }));
  1439. return false;
  1440. };
  1441. if (context.auto_actualize_feeds) {
  1442. auto = true;
  1443. actualize.click();
  1444. }
  1445. }
  1446. // </actualize>
  1447. // <notification>
  1448. let notification = null;
  1449. let notification_interval = null;
  1450. let notification_working = false;
  1451. function openNotification(msg, status) {
  1452. if (notification_working === true) {
  1453. return false;
  1454. }
  1455. notification_working = true;
  1456. notification.querySelector('.msg').innerHTML = msg;
  1457. notification.className = 'notification';
  1458. notification.classList.add(status);
  1459. if (status == 'good') {
  1460. notification_interval = setTimeout(closeNotification, 4000);
  1461. } else {
  1462. // no status or f.e. status = 'bad', give some more time to read
  1463. notification_interval = setTimeout(closeNotification, 8000);
  1464. }
  1465. }
  1466. function closeNotification() {
  1467. notification.classList.add('closed');
  1468. clearInterval(notification_interval);
  1469. notification_working = false;
  1470. }
  1471. function init_notifications() {
  1472. notification = document.getElementById('notification');
  1473. notification.querySelector('a.close').addEventListener('click', function (ev) {
  1474. closeNotification();
  1475. ev.preventDefault();
  1476. return false;
  1477. });
  1478. notification.addEventListener('mouseenter', function () {
  1479. clearInterval(notification_interval);
  1480. });
  1481. notification.addEventListener('mouseleave', function () {
  1482. notification_interval = setTimeout(closeNotification, 3000);
  1483. });
  1484. if (notification.querySelector('.msg').innerHTML.length > 0) {
  1485. notification_working = true;
  1486. if (notification.classList.contains('good')) {
  1487. notification_interval = setTimeout(closeNotification, 4000);
  1488. } else {
  1489. // no status or f.e. status = 'bad', give some more time to read
  1490. notification_interval = setTimeout(closeNotification, 8000);
  1491. }
  1492. }
  1493. }
  1494. // </notification>
  1495. // <notifs html5>
  1496. let notifs_html5_permission = 'denied';
  1497. function notifs_html5_is_supported() {
  1498. return window.Notification !== undefined;
  1499. }
  1500. function notifs_html5_ask_permission() {
  1501. try {
  1502. window.Notification.requestPermission(function () {
  1503. notifs_html5_permission = window.Notification.permission;
  1504. });
  1505. } catch (e) {
  1506. }
  1507. }
  1508. function notifs_html5_show(nb, nb_new) {
  1509. if (notifs_html5_permission !== 'granted') {
  1510. return;
  1511. }
  1512. try {
  1513. const notification = new window.Notification(context.i18n.notif_title_articles, {
  1514. icon: '../themes/icons/favicon-256-padding.png',
  1515. body: context.i18n.notif_body_new_articles.replace('%%d', nb_new) + ' ' + context.i18n.notif_body_unread_articles.replace('%%d', nb),
  1516. tag: 'freshRssNewArticles',
  1517. });
  1518. notification.onclick = function () {
  1519. delayedFunction(function () {
  1520. location.reload();
  1521. window.focus();
  1522. notification.close();
  1523. });
  1524. };
  1525. if (context.html5_notif_timeout !== 0) {
  1526. setTimeout(function () {
  1527. notification.close();
  1528. }, context.html5_notif_timeout * 1000);
  1529. }
  1530. } catch (e) {
  1531. }
  1532. }
  1533. function init_notifs_html5() {
  1534. if (!notifs_html5_is_supported()) {
  1535. return;
  1536. }
  1537. notifs_html5_permission = notifs_html5_ask_permission();
  1538. }
  1539. // </notifs html5>
  1540. function refreshUnreads() {
  1541. const req = new XMLHttpRequest();
  1542. req.open('GET', './?c=javascript&a=nbUnreadsPerFeed', true);
  1543. req.responseType = 'json';
  1544. req.onload = function (e) {
  1545. const json = xmlHttpRequestJson(this);
  1546. if (!json) {
  1547. return badAjax(false);
  1548. }
  1549. const isAll = document.querySelector('.category.all.active');
  1550. let new_articles = false;
  1551. let nbUnreadFeeds = 0;
  1552. const title = document.querySelector('.category.all .title');
  1553. const nb_unreads_before = title ? str2int(title.getAttribute('data-unread')) : 0;
  1554. Object.keys(json.feeds).forEach(function (feed_id) {
  1555. const nbUnreads = json.feeds[feed_id];
  1556. nbUnreadFeeds += nbUnreads;
  1557. feed_id = 'f_' + feed_id;
  1558. const elem = document.getElementById(feed_id);
  1559. const feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  1560. if ((incUnreadsFeed(null, feed_id, nbUnreads - feed_unreads) || isAll) && // Update of current view?
  1561. (nbUnreads - feed_unreads > 0)) {
  1562. const newArticle = document.getElementById('new-article');
  1563. newArticle.setAttribute('aria-hidden', 'false');
  1564. newArticle.style.display = 'block';
  1565. new_articles = true;
  1566. }
  1567. });
  1568. const noArticlesToShow_div = document.getElementById('noArticlesToShow');
  1569. if (nbUnreadFeeds > 0 && noArticlesToShow_div) {
  1570. noArticlesToShow_div.classList.add('hide');
  1571. }
  1572. let nbUnreadTags = 0;
  1573. Object.keys(json.tags).forEach(function (tag_id) {
  1574. const nbUnreads = json.tags[tag_id];
  1575. nbUnreadTags += nbUnreads;
  1576. const tag = document.getElementById('t_' + tag_id);
  1577. if (tag) {
  1578. tag.setAttribute('data-unread', nbUnreads);
  1579. tag.querySelector('.item-title').setAttribute('data-unread', numberFormat(nbUnreads));
  1580. }
  1581. });
  1582. const tags = document.querySelector('.category.tags');
  1583. if (tags) {
  1584. tags.setAttribute('data-unread', nbUnreadTags);
  1585. tags.querySelector('.title').setAttribute('data-unread', numberFormat(nbUnreadTags));
  1586. }
  1587. const nb_unreads = title ? str2int(title.getAttribute('data-unread')) : 0;
  1588. if (nb_unreads > 0 && new_articles) {
  1589. faviconNbUnread(nb_unreads);
  1590. const nb_new = nb_unreads - nb_unreads_before;
  1591. notifs_html5_show(nb_unreads, nb_new);
  1592. }
  1593. };
  1594. req.send();
  1595. }
  1596. function toggle_bigMarkAsRead_button() {
  1597. const bigMarkAsRead_button = document.getElementById('bigMarkAsRead');
  1598. if (bigMarkAsRead_button) {
  1599. if (document.querySelector('.flux.not_read') != null) {
  1600. bigMarkAsRead_button.style = '';
  1601. bigMarkAsRead_button.querySelector('.markAllRead').style.visibility = '';
  1602. } else {
  1603. if (bigMarkAsRead_button.querySelector('.jumpNext')) {
  1604. bigMarkAsRead_button.querySelector('.markAllRead').style.visibility = 'hidden';
  1605. } else {
  1606. bigMarkAsRead_button.querySelector('.markAllRead').style.visibility = '';
  1607. bigMarkAsRead_button.style.visibility = 'hidden';
  1608. }
  1609. }
  1610. }
  1611. }
  1612. // <endless_mode>
  1613. let url_load_more = '';
  1614. let load_more = false;
  1615. let box_load_more = null;
  1616. function load_more_posts() {
  1617. if (load_more || !url_load_more || !box_load_more) {
  1618. return;
  1619. }
  1620. load_more = true;
  1621. document.getElementById('load_more').classList.add('loading');
  1622. const req = new XMLHttpRequest();
  1623. req.open('GET', url_load_more, true);
  1624. req.responseType = 'document';
  1625. req.onload = function (e) {
  1626. const html = this.response;
  1627. const streamFooter = document.getElementById('stream-footer');
  1628. const streamAdopted = document.adoptNode(html.getElementById('stream'));
  1629. streamAdopted.querySelectorAll('.flux, .day').forEach(function (div) {
  1630. box_load_more.insertBefore(div, streamFooter);
  1631. });
  1632. const streamFooterOld = streamFooter.querySelector('.stream-footer-inner');
  1633. const streamFooterNew = streamAdopted.querySelector('.stream-footer-inner');
  1634. streamFooter.replaceChild(streamFooterNew, streamFooterOld);
  1635. const bigMarkAsRead = document.getElementById('bigMarkAsRead');
  1636. const readAll = document.querySelector('#nav_menu_read_all .read_all');
  1637. if (readAll && bigMarkAsRead && bigMarkAsRead.formAction) {
  1638. if (context.display_order === 'ASC') {
  1639. readAll.formAction = bigMarkAsRead.formAction;
  1640. } else {
  1641. bigMarkAsRead.formAction = readAll.formAction;
  1642. }
  1643. toggle_bigMarkAsRead_button();
  1644. }
  1645. document.querySelectorAll('[id^=day_]').forEach(function (div) {
  1646. const ids = document.querySelectorAll('[id="' + div.id + '"]');
  1647. for (let i = ids.length - 1; i > 0; i--) { // Keep only the first
  1648. ids[i].remove();
  1649. }
  1650. });
  1651. init_load_more(box_load_more);
  1652. const div_load_more = document.getElementById('load_more');
  1653. if (bigMarkAsRead) {
  1654. bigMarkAsRead.removeAttribute('disabled');
  1655. }
  1656. if (div_load_more) {
  1657. div_load_more.classList.remove('loading');
  1658. }
  1659. load_more = false;
  1660. };
  1661. req.send();
  1662. }
  1663. const freshrssLoadMoreEvent = document.createEvent('Event');
  1664. freshrssLoadMoreEvent.initEvent('freshrss:load-more', true, true);
  1665. function init_load_more(box) {
  1666. box_load_more = box;
  1667. document.body.dispatchEvent(freshrssLoadMoreEvent);
  1668. const next_button = document.getElementById('load_more');
  1669. if (!next_button) {
  1670. // no more article to load
  1671. url_load_more = '';
  1672. return;
  1673. }
  1674. url_load_more = next_button.getAttribute('formaction');
  1675. next_button.onclick = function (e) {
  1676. load_more_posts();
  1677. return false;
  1678. };
  1679. }
  1680. // </endless_mode>
  1681. function init_confirm_action() {
  1682. document.body.onclick = function (ev) {
  1683. const b = ev.target.closest('.confirm');
  1684. if (b) {
  1685. let str_confirmation = this.getAttribute('data-str-confirm');
  1686. if (!str_confirmation) {
  1687. str_confirmation = context.i18n.confirmation_default;
  1688. }
  1689. return confirm(str_confirmation);
  1690. }
  1691. };
  1692. document.querySelectorAll('button.confirm').forEach(function (b) { b.disabled = false; });
  1693. }
  1694. function faviconNbUnread(n) {
  1695. if (typeof n === 'undefined') {
  1696. const t = document.querySelector('.category.all .title');
  1697. n = t ? str2int(t.getAttribute('data-unread')) : 0;
  1698. }
  1699. // http://remysharp.com/2010/08/24/dynamic-favicons/
  1700. const canvas = document.createElement('canvas');
  1701. const link = document.getElementById('favicon').cloneNode(true);
  1702. const ratio = window.devicePixelRatio;
  1703. if (canvas.getContext && link) {
  1704. canvas.height = canvas.width = 16 * ratio;
  1705. const img = document.createElement('img');
  1706. img.onload = function () {
  1707. const ctx = canvas.getContext('2d');
  1708. ctx.drawImage(this, 0, 0, canvas.width, canvas.height);
  1709. if (n > 0) {
  1710. let text = '';
  1711. if (n < 1000) {
  1712. text = n;
  1713. } else if (n < 100000) {
  1714. text = Math.floor(n / 1000) + 'k';
  1715. } else {
  1716. text = 'E' + Math.floor(Math.log10(n));
  1717. }
  1718. ctx.font = 'bold ' + 9 * ratio + 'px "Arial", sans-serif';
  1719. ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
  1720. ctx.fillRect(0, 7 * ratio, ctx.measureText(text).width, 9 * ratio);
  1721. ctx.fillStyle = '#F00';
  1722. ctx.fillText(text, 0, canvas.height - 1);
  1723. }
  1724. link.href = canvas.toDataURL('image/png');
  1725. // Check if data URI has generated a real favicon and if not, fallback to standard icon
  1726. if (link.href.length > 180) {
  1727. document.querySelector('link[rel~=icon]').remove();
  1728. document.head.appendChild(link);
  1729. }
  1730. };
  1731. img.src = '../favicon.ico';
  1732. }
  1733. }
  1734. function removeFirstLoadSpinner() {
  1735. const first_load = document.getElementById('first_load');
  1736. if (first_load) {
  1737. first_load.remove();
  1738. }
  1739. }
  1740. function init_normal() {
  1741. const stream = document.getElementById('stream');
  1742. if (!stream) {
  1743. if (window.console) {
  1744. console.log('FreshRSS waiting for content…');
  1745. }
  1746. setTimeout(init_normal, 100);
  1747. return;
  1748. }
  1749. init_column_categories();
  1750. init_stream(stream);
  1751. init_actualize();
  1752. faviconNbUnread();
  1753. window.onbeforeunload = function (e) {
  1754. const sidebar = document.getElementById('sidebar');
  1755. if (sidebar) { // Save sidebar scroll position
  1756. sessionStorage.setItem('FreshRSS_sidebar_scrollTop', sidebar.scrollTop);
  1757. }
  1758. if (mark_read_queue && mark_read_queue.length > 0) {
  1759. return false;
  1760. }
  1761. };
  1762. }
  1763. function init_main_beforeDOM() {
  1764. document.scrollingElement.scrollTop = 0;
  1765. init_shortcuts();
  1766. if (['normal', 'reader', 'global'].indexOf(context.current_view) >= 0) {
  1767. init_normal();
  1768. }
  1769. }
  1770. function init_main_afterDOM() {
  1771. removeFirstLoadSpinner();
  1772. init_notifications();
  1773. init_confirm_action();
  1774. const stream = document.getElementById('stream');
  1775. if (stream) {
  1776. init_load_more(stream);
  1777. init_posts();
  1778. if (document.getElementById('new-article')) {
  1779. // Only relevant for interactive views
  1780. init_nav_entries();
  1781. init_notifs_html5();
  1782. toggle_bigMarkAsRead_button();
  1783. setTimeout(faviconNbUnread, 1000);
  1784. setInterval(refreshUnreads, 120000);
  1785. }
  1786. }
  1787. if (window.console) {
  1788. console.log('FreshRSS main init done.');
  1789. }
  1790. }
  1791. init_main_beforeDOM(); // Can be called before DOM is fully loaded
  1792. if (document.readyState && document.readyState !== 'loading') {
  1793. init_main_afterDOM();
  1794. } else {
  1795. if (window.console) {
  1796. console.log('FreshRSS waiting for DOMContentLoaded…');
  1797. }
  1798. document.addEventListener('DOMContentLoaded', init_main_afterDOM, false);
  1799. }
  1800. // @license-end