main.js 57 KB

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