main.js 47 KB

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