main.js 53 KB

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