main.js 63 KB

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