main.js 73 KB

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