main.js 62 KB

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