main.js 74 KB

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