main.js 62 KB

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