main.js 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893
  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 icon = a.querySelector('.icon');
  724. const category_id = a.closest('.category').id;
  725. if (icon.alt === '🔽' || icon.innerHTML === '🔽') {
  726. if (icon.src) {
  727. icon.src = icon.src.replace('/icons/down.', '/icons/up.');
  728. icon.alt = '🔼';
  729. } else {
  730. icon.innerHTML = '🔼';
  731. }
  732. rememberOpenCategory(category_id, true);
  733. } else {
  734. if (icon.src) {
  735. icon.src = icon.src.replace('/icons/up.', '/icons/down.');
  736. icon.alt = '🔽';
  737. } else {
  738. icon.innerHTML = '🔽';
  739. }
  740. rememberOpenCategory(category_id, false);
  741. }
  742. const ul = a.closest('li').querySelector('.tree-folder-items');
  743. let nbVisibleItems = 0;
  744. for (let i = ul.children.length - 1; i >= 0; i--) {
  745. if (ul.children[i].offsetHeight) {
  746. nbVisibleItems++;
  747. }
  748. }
  749. ul.classList.toggle('active');
  750. // CSS transition does not work on max-height:auto
  751. ul.style.maxHeight = ul.classList.contains('active') ? (nbVisibleItems * 4) + 'em' : 0;
  752. return false;
  753. }
  754. a = ev.target.closest('.tree-folder-items > .feed .dropdown-toggle');
  755. if (a) {
  756. const itemId = a.closest('.item').id;
  757. const templateId = itemId.substring(0, 2) === 't_' ? 'tag_config_template' : 'feed_config_template';
  758. const id = itemId.substr(2);
  759. const feed_web = a.getAttribute('data-fweb');
  760. const div = a.parentElement;
  761. const dropdownMenu = div.querySelector('.dropdown-menu');
  762. const template = document.getElementById(templateId)
  763. .innerHTML.replace(/------/g, id).replace('http://example.net/', feed_web);
  764. if (!dropdownMenu) {
  765. a.href = '#dropdown-' + id;
  766. div.querySelector('.dropdown-target').id = 'dropdown-' + id;
  767. div.insertAdjacentHTML('beforeend', template);
  768. const b = div.querySelector('button.confirm');
  769. if (b) {
  770. b.disabled = false;
  771. }
  772. } else if (getComputedStyle(dropdownMenu).display === 'none') {
  773. const id2 = div.closest('.item').id.substr(2);
  774. a.href = '#dropdown-' + id2;
  775. } else {
  776. a.href = '#close';
  777. }
  778. return true;
  779. }
  780. return true;
  781. };
  782. }
  783. function init_shortcuts() {
  784. Object.keys(context.shortcuts).forEach(function (k) {
  785. context.shortcuts[k] = (context.shortcuts[k] || '').toUpperCase();
  786. });
  787. document.addEventListener('keydown', ev => {
  788. if (ev.target.closest('input, textarea') ||
  789. ev.ctrlKey || ev.metaKey || (ev.altKey && ev.shiftKey)) {
  790. return;
  791. }
  792. const s = context.shortcuts;
  793. let k = (ev.key.trim() || ev.code || 'Space').toUpperCase();
  794. // IE11
  795. if (k === 'SPACEBAR') k = 'SPACE';
  796. else if (k === 'DEL') k = 'DELETE';
  797. else if (k === 'ESC') k = 'ESCAPE';
  798. if (location.hash.match(/^#dropdown-/)) {
  799. const n = parseInt(k);
  800. if (n) {
  801. if (location.hash === '#dropdown-query') {
  802. user_filter(n);
  803. } else {
  804. auto_share(n);
  805. }
  806. ev.preventDefault();
  807. return;
  808. }
  809. }
  810. if (k === s.actualize) {
  811. const btn = document.getElementById('actualize');
  812. if (btn) {
  813. btn.click();
  814. }
  815. ev.preventDefault();
  816. return;
  817. }
  818. if (k === s.next_entry) {
  819. if (ev.altKey) {
  820. next_category();
  821. } else if (ev.shiftKey) {
  822. next_feed();
  823. } else {
  824. next_entry(false);
  825. }
  826. ev.preventDefault();
  827. return;
  828. }
  829. if (k === s.next_unread_entry) {
  830. if (ev.altKey) {
  831. next_unread_category();
  832. } else if (ev.shiftKey) {
  833. next_feed();
  834. } else {
  835. next_unread_entry(false);
  836. }
  837. ev.preventDefault();
  838. return;
  839. }
  840. if (k === s.prev_entry) {
  841. if (ev.altKey) {
  842. prev_category();
  843. } else if (ev.shiftKey) {
  844. prev_feed();
  845. } else {
  846. prev_entry(false);
  847. }
  848. ev.preventDefault();
  849. return;
  850. }
  851. if (k === s.mark_read) {
  852. if (ev.altKey) {
  853. mark_previous_read(document.querySelector('.flux.current'));
  854. } else if (ev.shiftKey) {
  855. document.querySelector('.nav_menu .read_all').click();
  856. } else { // Toggle the read state
  857. mark_read(document.querySelector('.flux.current'), false, false);
  858. }
  859. ev.preventDefault();
  860. return;
  861. }
  862. if (k === s.first_entry) {
  863. if (ev.altKey) {
  864. first_category();
  865. } else if (ev.shiftKey) {
  866. first_feed();
  867. } else {
  868. const old_active = document.querySelector('.flux.current');
  869. const first = document.querySelector('.flux');
  870. if (first.classList.contains('flux')) {
  871. toggleContent(first, old_active, false);
  872. }
  873. }
  874. ev.preventDefault();
  875. return;
  876. }
  877. if (k === s.last_entry) {
  878. if (ev.altKey) {
  879. last_category();
  880. } else if (ev.shiftKey) {
  881. last_feed();
  882. } else {
  883. const old_active = document.querySelector('.flux.current');
  884. const last = document.querySelector('.flux:last-of-type');
  885. if (last.classList.contains('flux')) {
  886. toggleContent(last, old_active, false);
  887. }
  888. }
  889. ev.preventDefault();
  890. return;
  891. }
  892. if (ev.altKey || ev.shiftKey) {
  893. return;
  894. }
  895. if (k === s.mark_favorite) { // Toggle the favorite state
  896. mark_favorite(document.querySelector('.flux.current'));
  897. ev.preventDefault();
  898. return;
  899. }
  900. if (k === s.go_website) {
  901. if (context.auto_mark_site) {
  902. mark_read(document.querySelector('.flux.current'), true, false);
  903. }
  904. const newWindow = window.open();
  905. if (newWindow) {
  906. newWindow.opener = null;
  907. newWindow.location = document.querySelector('.flux.current a.go_website').href;
  908. }
  909. ev.preventDefault();
  910. return;
  911. }
  912. if (k === s.skip_next_entry) { next_entry(true); ev.preventDefault(); return; }
  913. if (k === s.skip_prev_entry) { prev_entry(true); ev.preventDefault(); return; }
  914. if (k === s.collapse_entry) { collapse_entry(); ev.preventDefault(); return; }
  915. if (k === s.auto_share) { auto_share(); ev.preventDefault(); return; }
  916. if (k === s.user_filter) { user_filter(); ev.preventDefault(); return; }
  917. if (k === s.load_more) { load_more_posts(); ev.preventDefault(); return; }
  918. if (k === s.close_dropdown) { location.hash = null; ev.preventDefault(); return; }
  919. if (k === s.help) { window.open(context.urls.help); ev.preventDefault(); return; }
  920. if (k === s.focus_search) { document.getElementById('search').focus(); ev.preventDefault(); return; }
  921. if (k === s.normal_view) { delayedClick(document.querySelector('#nav_menu_views .view-normal')); ev.preventDefault(); return; }
  922. if (k === s.reading_view) { delayedClick(document.querySelector('#nav_menu_views .view-reader')); ev.preventDefault(); return; }
  923. if (k === s.global_view) { delayedClick(document.querySelector('#nav_menu_views .view-global')); ev.preventDefault(); return; }
  924. if (k === s.rss_view) { delayedClick(document.querySelector('#nav_menu_views .view-rss')); ev.preventDefault(); return; }
  925. if (k === s.toggle_media) { toggle_media(); ev.preventDefault(); }
  926. });
  927. }
  928. function init_stream(stream) {
  929. stream.onclick = function (ev) {
  930. let el = ev.target.closest('.flux a.read');
  931. if (el) {
  932. mark_read(el.closest('.flux'), false, false);
  933. return false;
  934. }
  935. el = ev.target.closest('.flux a.bookmark');
  936. if (el) {
  937. mark_favorite(el.closest('.flux'));
  938. return false;
  939. }
  940. el = ev.target.closest('.dynamictags');
  941. if (el) {
  942. loadDynamicTags(el);
  943. return true;
  944. }
  945. el = ev.target.closest('.item.title > a');
  946. if (el) { // Allow default control-click behaviour such as open in background-tab
  947. return ev.ctrlKey;
  948. }
  949. el = ev.target.closest('.flux .content a');
  950. if (el) {
  951. if (!el.closest('div').classList.contains('author')) {
  952. el.target = '_blank';
  953. el.rel = 'noreferrer';
  954. }
  955. return true;
  956. }
  957. el = ev.target.closest('.item.share > button[data-type="print"]');
  958. if (el) { // Print
  959. const tmp_window = window.open();
  960. for (let i = 0; i < document.styleSheets.length; i++) {
  961. tmp_window.document.writeln('<link href="' + document.styleSheets[i].href + '" rel="stylesheet" type="text/css" />');
  962. }
  963. tmp_window.document.writeln(el.closest('.flux_content').querySelector('.content').innerHTML);
  964. tmp_window.document.close();
  965. tmp_window.focus();
  966. tmp_window.print();
  967. tmp_window.close();
  968. return false;
  969. }
  970. el = ev.target.closest('.item.share > button[data-type="clipboard"]');
  971. if (el && navigator.clipboard) { // Clipboard
  972. navigator.clipboard.writeText(el.dataset.url);
  973. return false;
  974. }
  975. el = ev.target.closest('.item.share > button[data-type="web-sharing-api"]');
  976. if (el && navigator.share) { // https://developer.mozilla.org/en-US/docs/Web/API/Navigator/share
  977. const shareData = {
  978. url: el.dataset.url,
  979. title: decodeURI(el.dataset.title),
  980. };
  981. navigator.share(shareData);
  982. return false;
  983. }
  984. el = ev.target.closest('.item.share > a[href="POST"]');
  985. if (el) { // Share by POST
  986. const f = el.parentElement.querySelector('form');
  987. f.disabled = false;
  988. f.submit();
  989. return false;
  990. }
  991. el = ev.target.closest('.flux_header, .flux_content');
  992. if (el) { // flux_toggle
  993. if (ev.target.closest('.content, .item.website, .item.link, .dropdown')) {
  994. return true;
  995. }
  996. if (!context.sides_close_article && ev.target.matches('div.flux_content')) {
  997. // setting for not-closing after clicking outside article area
  998. return false;
  999. }
  1000. const old_active = document.querySelector('.flux.current');
  1001. const new_active = el.parentNode;
  1002. if (ev.target.tagName.toUpperCase() === 'A') { // Leave real links alone (but does not catch img in a link)
  1003. if (context.auto_mark_article) {
  1004. mark_read(new_active, true, false);
  1005. }
  1006. return true;
  1007. }
  1008. toggleContent(new_active, old_active, false);
  1009. return false;
  1010. }
  1011. };
  1012. stream.onmouseup = function (ev) { // Mouseup enables us to catch middle click, and control+click in IE/Edge
  1013. if (ev.altKey || ev.metaKey || ev.shiftKey) {
  1014. return;
  1015. }
  1016. let el = ev.target.closest('.item.title > a');
  1017. if (el) {
  1018. if (ev.which == 1) {
  1019. if (ev.ctrlKey) { // Control+click
  1020. if (context.auto_mark_site) {
  1021. mark_read(el.closest('.flux'), true, false);
  1022. }
  1023. } else {
  1024. el.parentElement.click(); // Normal click, just toggle article.
  1025. }
  1026. } else if (ev.which == 2 && !ev.ctrlKey) { // Simple middle click: same behaviour as CTRL+click
  1027. if (context.auto_mark_article) {
  1028. const new_active = el.closest('.flux');
  1029. mark_read(new_active, true, false);
  1030. }
  1031. }
  1032. return;
  1033. }
  1034. if (context.auto_mark_site) {
  1035. // catch mouseup instead of click so we can have the correct behaviour
  1036. // with middle button click (scroll button).
  1037. el = ev.target.closest('.flux .link > a');
  1038. if (el) {
  1039. if (ev.which == 3) {
  1040. return;
  1041. }
  1042. mark_read(el.closest('.flux'), true, false);
  1043. }
  1044. }
  1045. };
  1046. stream.onchange = function (ev) {
  1047. const checkboxTag = ev.target.closest('.checkboxTag');
  1048. if (checkboxTag) { // Dynamic tags
  1049. ev.stopPropagation();
  1050. const tagId = checkboxTag.name.replace(/^t_/, '');
  1051. const tagName = checkboxTag.nextElementSibling ? checkboxTag.nextElementSibling.childNodes[0].value : '';
  1052. if ((tagId == 0 && tagName.length > 0) || tagId != 0) {
  1053. const isChecked = checkboxTag.checked;
  1054. const entry = checkboxTag.closest('div.flux');
  1055. const entryId = entry.id.replace(/^flux_/, '');
  1056. checkboxTag.disabled = true;
  1057. const req = new XMLHttpRequest();
  1058. req.open('POST', './?c=tag&a=tagEntry', true);
  1059. req.responseType = 'json';
  1060. req.onerror = function (e) {
  1061. checkboxTag.checked = !isChecked;
  1062. badAjax(this.status == 403);
  1063. };
  1064. req.onload = function (e) {
  1065. if (this.status != 200) {
  1066. return req.onerror(e);
  1067. }
  1068. if (entry.classList.contains('not_read')) {
  1069. incUnreadsTag('t_' + tagId, isChecked ? 1 : -1);
  1070. }
  1071. };
  1072. req.onloadend = function (e) {
  1073. checkboxTag.disabled = false;
  1074. if (tagId == 0) {
  1075. loadDynamicTags(checkboxTag.closest('div.dropdown'));
  1076. }
  1077. };
  1078. req.setRequestHeader('Content-Type', 'application/json');
  1079. req.send(JSON.stringify({
  1080. _csrf: context.csrf,
  1081. id_tag: tagId,
  1082. name_tag: tagId == 0 ? tagName : '',
  1083. id_entry: entryId,
  1084. checked: isChecked,
  1085. }));
  1086. }
  1087. }
  1088. };
  1089. }
  1090. function init_nav_entries() {
  1091. const nav_entries = document.getElementById('nav_entries');
  1092. if (nav_entries) {
  1093. nav_entries.querySelector('.previous_entry').onclick = function (e) {
  1094. prev_entry(false);
  1095. return false;
  1096. };
  1097. nav_entries.querySelector('.next_entry').onclick = function (e) {
  1098. next_entry(false);
  1099. return false;
  1100. };
  1101. nav_entries.querySelector('.up').onclick = function (e) {
  1102. const active_item = (document.querySelector('.flux.current') || document.querySelector('.flux'));
  1103. const windowTop = document.scrollingElement.scrollTop;
  1104. const item_top = active_item.offsetParent.offsetTop + active_item.offsetTop;
  1105. document.scrollingElement.scrollTop = windowTop > item_top ? item_top : 0;
  1106. return false;
  1107. };
  1108. }
  1109. }
  1110. function loadDynamicTags(div) {
  1111. div.classList.remove('dynamictags');
  1112. div.querySelectorAll('li.item').forEach(function (li) { li.remove(); });
  1113. const entryId = div.closest('div.flux').id.replace(/^flux_/, '');
  1114. const req = new XMLHttpRequest();
  1115. req.open('GET', './?c=tag&a=getTagsForEntry&id_entry=' + entryId, true);
  1116. req.responseType = 'json';
  1117. req.onerror = function (e) {
  1118. div.querySelectorAll('li.item').forEach(function (li) { li.remove(); });
  1119. div.classList.add('dynamictags');
  1120. };
  1121. req.onload = function (e) {
  1122. if (this.status != 200) {
  1123. return req.onerror(e);
  1124. }
  1125. const json = xmlHttpRequestJson(this);
  1126. if (!json) {
  1127. return req.onerror(e);
  1128. }
  1129. const li_item0 = document.createElement('li');
  1130. li_item0.setAttribute('class', 'item addItem');
  1131. const label = document.createElement('label');
  1132. label.setAttribute('class', 'noHover');
  1133. const input_checkboxTag = document.createElement('input');
  1134. input_checkboxTag.setAttribute('class', 'checkboxTag checkboxNewTag');
  1135. input_checkboxTag.setAttribute('name', 't_0');
  1136. input_checkboxTag.setAttribute('type', 'checkbox');
  1137. const input_newTag = document.createElement('input');
  1138. input_newTag.setAttribute('type', 'text');
  1139. input_newTag.setAttribute('name', 'newTag');
  1140. input_newTag.addEventListener('keydown', function (ev) { if (ev.key.toUpperCase() == 'ENTER') { this.parentNode.previousSibling.click(); } });
  1141. const button_btn = document.createElement('button');
  1142. button_btn.setAttribute('type', 'button');
  1143. button_btn.setAttribute('class', 'btn');
  1144. button_btn.addEventListener('click', function () { this.parentNode.parentNode.click(); });
  1145. const text_plus = document.createTextNode('+');
  1146. const div_stick = document.createElement('div');
  1147. div_stick.setAttribute('class', 'stick');
  1148. button_btn.appendChild(text_plus);
  1149. div_stick.appendChild(input_newTag);
  1150. div_stick.appendChild(button_btn);
  1151. label.appendChild(input_checkboxTag);
  1152. label.appendChild(div_stick);
  1153. li_item0.appendChild(label);
  1154. div.querySelector('.dropdown-menu').appendChild(li_item0);
  1155. let html = '';
  1156. if (json && json.length) {
  1157. for (let i = 0; i < json.length; i++) {
  1158. const tag = json[i];
  1159. html += '<li class="item"><label><input class="checkboxTag" name="t_' + tag.id + '" type="checkbox"' +
  1160. (tag.checked ? ' checked="checked"' : '') + '> ' + tag.name + '</label></li>';
  1161. }
  1162. }
  1163. div.querySelector('.dropdown-menu').insertAdjacentHTML('beforeend', html);
  1164. };
  1165. req.send();
  1166. }
  1167. // <actualize>
  1168. let feed_processed = 0;
  1169. function updateFeed(feeds, feeds_count) {
  1170. const feed = feeds.pop();
  1171. if (!feed) {
  1172. return;
  1173. }
  1174. const req = new XMLHttpRequest();
  1175. req.open('POST', feed.url, true);
  1176. req.onloadend = function (e) {
  1177. if (this.status != 200) {
  1178. return badAjax(false);
  1179. }
  1180. feed_processed++;
  1181. const div = document.getElementById('actualizeProgress');
  1182. div.querySelector('.progress').innerHTML = feed_processed + ' / ' + feeds_count;
  1183. div.querySelector('.title').innerHTML = feed.title;
  1184. if (feed_processed === feeds_count) {
  1185. // Empty request to commit new articles
  1186. const req2 = new XMLHttpRequest();
  1187. req2.open('POST', './?c=feed&a=actualize&id=-1&ajax=1', true);
  1188. req2.onloadend = function (e) {
  1189. delayedFunction(function () { location.reload(); });
  1190. };
  1191. req2.setRequestHeader('Content-Type', 'application/json');
  1192. req2.send(JSON.stringify({
  1193. _csrf: context.csrf,
  1194. noCommit: 0,
  1195. }));
  1196. } else {
  1197. updateFeed(feeds, feeds_count);
  1198. }
  1199. };
  1200. req.setRequestHeader('Content-Type', 'application/json');
  1201. req.send(JSON.stringify({
  1202. _csrf: context.csrf,
  1203. noCommit: 1,
  1204. }));
  1205. }
  1206. function init_actualize() {
  1207. let auto = false;
  1208. const actualize = document.getElementById('actualize');
  1209. if (!actualize) {
  1210. return;
  1211. }
  1212. actualize.onclick = function () {
  1213. if (context.ajax_loading) {
  1214. return false;
  1215. }
  1216. context.ajax_loading = true;
  1217. const req = new XMLHttpRequest();
  1218. req.open('POST', './?c=javascript&a=actualize', true);
  1219. req.responseType = 'json';
  1220. req.onload = function (e) {
  1221. if (this.status != 200) {
  1222. return badAjax(false);
  1223. }
  1224. const json = xmlHttpRequestJson(this);
  1225. if (!json) {
  1226. return badAjax(false);
  1227. }
  1228. if (auto && json.feeds.length < 1) {
  1229. auto = false;
  1230. context.ajax_loading = false;
  1231. return false;
  1232. }
  1233. if (json.feeds.length === 0) {
  1234. openNotification(json.feedback_no_refresh, 'good');
  1235. // Empty request to commit new articles
  1236. const req2 = new XMLHttpRequest();
  1237. req2.open('POST', './?c=feed&a=actualize&id=-1&ajax=1', true);
  1238. req2.onloadend = function (e) {
  1239. context.ajax_loading = false;
  1240. };
  1241. req2.setRequestHeader('Content-Type', 'application/json');
  1242. req2.send(JSON.stringify({
  1243. _csrf: context.csrf,
  1244. noCommit: 0,
  1245. }));
  1246. return;
  1247. }
  1248. // Progress bar
  1249. const feeds_count = json.feeds.length;
  1250. document.body.insertAdjacentHTML('beforeend', '<div id="actualizeProgress" class="notification good">' +
  1251. json.feedback_actualize + '<br /><span class="title">/</span><br /><span class="progress">0 / ' +
  1252. feeds_count + '</span></div>');
  1253. for (let i = 10; i > 0; i--) {
  1254. updateFeed(json.feeds, feeds_count);
  1255. }
  1256. };
  1257. req.setRequestHeader('Content-Type', 'application/json');
  1258. req.send(JSON.stringify({
  1259. _csrf: context.csrf,
  1260. }));
  1261. return false;
  1262. };
  1263. if (context.auto_actualize_feeds) {
  1264. auto = true;
  1265. actualize.click();
  1266. }
  1267. }
  1268. // </actualize>
  1269. // <notification>
  1270. let notification = null;
  1271. let notification_interval = null;
  1272. let notification_working = false;
  1273. function openNotification(msg, status) {
  1274. if (notification_working === true) {
  1275. return false;
  1276. }
  1277. notification_working = true;
  1278. notification.querySelector('.msg').innerHTML = msg;
  1279. notification.className = 'notification';
  1280. notification.classList.add(status);
  1281. if (status == 'good') {
  1282. notification_interval = setTimeout(closeNotification, 4000);
  1283. } else {
  1284. // no status or f.e. status = 'bad', give some more time to read
  1285. notification_interval = setTimeout(closeNotification, 8000);
  1286. }
  1287. }
  1288. function closeNotification() {
  1289. notification.classList.add('closed');
  1290. clearInterval(notification_interval);
  1291. notification_working = false;
  1292. }
  1293. function init_notifications() {
  1294. notification = document.getElementById('notification');
  1295. notification.querySelector('a.close').addEventListener('click', function (ev) {
  1296. closeNotification();
  1297. ev.preventDefault();
  1298. return false;
  1299. });
  1300. notification.addEventListener('mouseenter', function () {
  1301. clearInterval(notification_interval);
  1302. });
  1303. notification.addEventListener('mouseleave', function () {
  1304. notification_interval = setTimeout(closeNotification, 3000);
  1305. });
  1306. if (notification.querySelector('.msg').innerHTML.length > 0) {
  1307. notification_working = true;
  1308. if (notification.classList.contains('good')) {
  1309. notification_interval = setTimeout(closeNotification, 4000);
  1310. } else {
  1311. // no status or f.e. status = 'bad', give some more time to read
  1312. notification_interval = setTimeout(closeNotification, 8000);
  1313. }
  1314. }
  1315. }
  1316. // </notification>
  1317. // <slider>
  1318. function init_slider_observers() {
  1319. const slider = document.getElementById('slider');
  1320. const closer = document.getElementById('close-slider');
  1321. if (!slider) {
  1322. return;
  1323. }
  1324. window.onclick = open_slider_listener;
  1325. closer.addEventListener('click', function (ev) {
  1326. if (slider_data_leave_validation() || confirm(context.i18n.confirmation_default)) {
  1327. slider.querySelectorAll('form').forEach(function (f) { f.reset(); });
  1328. closer.classList.remove('active');
  1329. slider.classList.remove('active');
  1330. return true;
  1331. } else {
  1332. return false;
  1333. }
  1334. });
  1335. }
  1336. function open_slider_listener(ev) {
  1337. const a = ev.target.closest('.open-slider');
  1338. if (a) {
  1339. if (!context.ajax_loading) {
  1340. location.href = '#'; // close menu/dropdown
  1341. context.ajax_loading = true;
  1342. const req = new XMLHttpRequest();
  1343. req.open('GET', a.href + '&ajax=1', true);
  1344. req.responseType = 'document';
  1345. req.onload = function (e) {
  1346. const slider = document.getElementById('slider');
  1347. const closer = document.getElementById('close-slider');
  1348. slider.innerHTML = this.response.body.innerHTML;
  1349. slider.classList.add('active');
  1350. closer.classList.add('active');
  1351. context.ajax_loading = false;
  1352. };
  1353. req.send();
  1354. return false;
  1355. }
  1356. }
  1357. }
  1358. function slider_data_leave_validation() {
  1359. const ds = document.querySelectorAll('[data-leave-validation]');
  1360. for (let i = ds.length - 1; i >= 0; i--) {
  1361. const input = ds[i];
  1362. if (input.type === 'checkbox' || input.type === 'radio') {
  1363. if (input.checked != input.getAttribute('data-leave-validation')) {
  1364. return false;
  1365. }
  1366. } else if (input.value != input.getAttribute('data-leave-validation')) {
  1367. return false;
  1368. }
  1369. }
  1370. return true;
  1371. }
  1372. // </slider>
  1373. // <popup>
  1374. let popup = null;
  1375. let popup_iframe_container = null;
  1376. let popup_iframe = null;
  1377. let popup_txt = null;
  1378. let popup_working = false;
  1379. /* eslint-disable no-unused-vars */
  1380. // TODO: Re-enable no-unused-vars
  1381. function openPopupWithMessage(msg) {
  1382. if (popup_working === true) {
  1383. return false;
  1384. }
  1385. popup_working = true;
  1386. popup_txt.innerHTML = msg;
  1387. popup_txt.style.display = 'table-row';
  1388. popup.style.display = 'block';
  1389. }
  1390. function openPopupWithSource(source) {
  1391. if (popup_working === true) {
  1392. return false;
  1393. }
  1394. popup_working = true;
  1395. popup_iframe.src = source;
  1396. popup_iframe_container.style.display = 'table-row';
  1397. popup.style.display = 'block';
  1398. }
  1399. /* eslint-enable no-unused-vars */
  1400. function closePopup() {
  1401. popup.style.display = 'none';
  1402. popup_iframe_container.style.display = 'none';
  1403. popup_txt.style.display = 'none';
  1404. popup_iframe.src = 'about:blank';
  1405. popup_working = false;
  1406. }
  1407. function init_popup() {
  1408. // Fetch elements.
  1409. popup = document.getElementById('popup');
  1410. if (popup) {
  1411. popup_iframe_container = document.getElementById('popup-iframe-container');
  1412. popup_iframe = document.getElementById('popup-iframe');
  1413. popup_txt = document.getElementById('popup-txt');
  1414. // Configure close button.
  1415. document.getElementById('popup-close').addEventListener('click', function (ev) {
  1416. closePopup();
  1417. });
  1418. // Configure close-on-click.
  1419. window.addEventListener('click', function (ev) {
  1420. if (ev.target == popup) {
  1421. closePopup();
  1422. }
  1423. });
  1424. }
  1425. }
  1426. // </popup>
  1427. // <notifs html5>
  1428. let notifs_html5_permission = 'denied';
  1429. function notifs_html5_is_supported() {
  1430. return window.Notification !== undefined;
  1431. }
  1432. function notifs_html5_ask_permission() {
  1433. window.Notification.requestPermission(function () {
  1434. notifs_html5_permission = window.Notification.permission;
  1435. });
  1436. }
  1437. function notifs_html5_show(nb, nb_new) {
  1438. if (notifs_html5_permission !== 'granted') {
  1439. return;
  1440. }
  1441. const notification = new window.Notification(context.i18n.notif_title_articles, {
  1442. icon: '../themes/icons/favicon-256-padding.png',
  1443. body: context.i18n.notif_body_new_articles.replace('%d', nb_new) + ' ' + context.i18n.notif_body_unread_articles.replace('%d', nb),
  1444. tag: 'freshRssNewArticles',
  1445. });
  1446. notification.onclick = function () {
  1447. delayedFunction(function () {
  1448. location.reload();
  1449. window.focus();
  1450. notification.close();
  1451. });
  1452. };
  1453. if (context.html5_notif_timeout !== 0) {
  1454. setTimeout(function () {
  1455. notification.close();
  1456. }, context.html5_notif_timeout * 1000);
  1457. }
  1458. }
  1459. function init_notifs_html5() {
  1460. if (!notifs_html5_is_supported()) {
  1461. return;
  1462. }
  1463. notifs_html5_permission = notifs_html5_ask_permission();
  1464. }
  1465. // </notifs html5>
  1466. function refreshUnreads() {
  1467. const req = new XMLHttpRequest();
  1468. req.open('GET', './?c=javascript&a=nbUnreadsPerFeed', true);
  1469. req.responseType = 'json';
  1470. req.onload = function (e) {
  1471. const json = xmlHttpRequestJson(this);
  1472. if (!json) {
  1473. return badAjax(false);
  1474. }
  1475. const isAll = document.querySelector('.category.all.active');
  1476. let new_articles = false;
  1477. let nbUnreadFeeds = 0;
  1478. const title = document.querySelector('.category.all .title');
  1479. const nb_unreads_before = title ? str2int(title.getAttribute('data-unread')) : 0;
  1480. Object.keys(json.feeds).forEach(function (feed_id) {
  1481. const nbUnreads = json.feeds[feed_id];
  1482. nbUnreadFeeds += nbUnreads;
  1483. feed_id = 'f_' + feed_id;
  1484. const elem = document.getElementById(feed_id);
  1485. const feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  1486. if ((incUnreadsFeed(null, feed_id, nbUnreads - feed_unreads) || isAll) && // Update of current view?
  1487. (nbUnreads - feed_unreads > 0)) {
  1488. const newArticle = document.getElementById('new-article');
  1489. newArticle.setAttribute('aria-hidden', 'false');
  1490. newArticle.style.display = 'block';
  1491. new_articles = true;
  1492. }
  1493. });
  1494. const noArticlesToShow_div = document.getElementById('noArticlesToShow');
  1495. if (nbUnreadFeeds > 0 && noArticlesToShow_div) {
  1496. noArticlesToShow_div.classList.add('hide');
  1497. }
  1498. let nbUnreadTags = 0;
  1499. Object.keys(json.tags).forEach(function (tag_id) {
  1500. const nbUnreads = json.tags[tag_id];
  1501. nbUnreadTags += nbUnreads;
  1502. const tag = document.getElementById('t_' + tag_id);
  1503. if (tag) {
  1504. tag.setAttribute('data-unread', nbUnreads);
  1505. tag.querySelector('.item-title').setAttribute('data-unread', numberFormat(nbUnreads));
  1506. }
  1507. });
  1508. const tags = document.querySelector('.category.tags');
  1509. if (tags) {
  1510. tags.setAttribute('data-unread', nbUnreadTags);
  1511. tags.querySelector('.title').setAttribute('data-unread', numberFormat(nbUnreadTags));
  1512. }
  1513. const nb_unreads = title ? str2int(title.getAttribute('data-unread')) : 0;
  1514. if (nb_unreads > 0 && new_articles) {
  1515. faviconNbUnread(nb_unreads);
  1516. const nb_new = nb_unreads - nb_unreads_before;
  1517. notifs_html5_show(nb_unreads, nb_new);
  1518. }
  1519. };
  1520. req.send();
  1521. }
  1522. // <endless_mode>
  1523. let url_load_more = '';
  1524. let load_more = false;
  1525. let box_load_more = null;
  1526. function load_more_posts() {
  1527. if (load_more || !url_load_more || !box_load_more) {
  1528. return;
  1529. }
  1530. load_more = true;
  1531. document.getElementById('load_more').classList.add('loading');
  1532. const req = new XMLHttpRequest();
  1533. req.open('GET', url_load_more, true);
  1534. req.responseType = 'document';
  1535. req.onload = function (e) {
  1536. const html = this.response;
  1537. const streamFooter = document.getElementById('stream-footer');
  1538. const streamAdopted = document.adoptNode(html.getElementById('stream'));
  1539. streamAdopted.querySelectorAll('.flux, .day').forEach(function (div) {
  1540. box_load_more.insertBefore(div, streamFooter);
  1541. });
  1542. const streamFooterOld = streamFooter.querySelector('.stream-footer-inner');
  1543. const streamFooterNew = streamAdopted.querySelector('.stream-footer-inner');
  1544. streamFooter.replaceChild(streamFooterNew, streamFooterOld);
  1545. const bigMarkAsRead = document.getElementById('bigMarkAsRead');
  1546. const readAll = document.querySelector('#nav_menu_read_all .read_all');
  1547. if (readAll && bigMarkAsRead && bigMarkAsRead.formAction) {
  1548. if (context.display_order === 'ASC') {
  1549. readAll.formAction = bigMarkAsRead.formAction;
  1550. } else {
  1551. bigMarkAsRead.formAction = readAll.formAction;
  1552. }
  1553. }
  1554. document.querySelectorAll('[id^=day_]').forEach(function (div) {
  1555. const ids = document.querySelectorAll('[id="' + div.id + '"]');
  1556. for (let i = ids.length - 1; i > 0; i--) { // Keep only the first
  1557. ids[i].remove();
  1558. }
  1559. });
  1560. init_load_more(box_load_more);
  1561. const div_load_more = document.getElementById('load_more');
  1562. if (bigMarkAsRead) {
  1563. bigMarkAsRead.removeAttribute('disabled');
  1564. }
  1565. if (div_load_more) {
  1566. div_load_more.classList.remove('loading');
  1567. }
  1568. load_more = false;
  1569. };
  1570. req.send();
  1571. }
  1572. const freshrssLoadMoreEvent = document.createEvent('Event');
  1573. freshrssLoadMoreEvent.initEvent('freshrss:load-more', true, true);
  1574. function init_load_more(box) {
  1575. box_load_more = box;
  1576. document.body.dispatchEvent(freshrssLoadMoreEvent);
  1577. const next_button = document.getElementById('load_more');
  1578. if (!next_button) {
  1579. // no more article to load
  1580. url_load_more = '';
  1581. return;
  1582. }
  1583. url_load_more = next_button.getAttribute('formaction');
  1584. next_button.onclick = function (e) {
  1585. load_more_posts();
  1586. return false;
  1587. };
  1588. }
  1589. // </endless_mode>
  1590. function init_confirm_action() {
  1591. document.body.onclick = function (ev) {
  1592. const b = ev.target.closest('.confirm');
  1593. if (b) {
  1594. let str_confirmation = this.getAttribute('data-str-confirm');
  1595. if (!str_confirmation) {
  1596. str_confirmation = context.i18n.confirmation_default;
  1597. }
  1598. return confirm(str_confirmation);
  1599. }
  1600. };
  1601. document.querySelectorAll('button.confirm').forEach(function (b) { b.disabled = false; });
  1602. }
  1603. function faviconNbUnread(n) {
  1604. if (typeof n === 'undefined') {
  1605. const t = document.querySelector('.category.all .title');
  1606. n = t ? str2int(t.getAttribute('data-unread')) : 0;
  1607. }
  1608. // http://remysharp.com/2010/08/24/dynamic-favicons/
  1609. const canvas = document.createElement('canvas');
  1610. const link = document.getElementById('favicon').cloneNode(true);
  1611. const ratio = window.devicePixelRatio;
  1612. if (canvas.getContext && link) {
  1613. canvas.height = canvas.width = 16 * ratio;
  1614. const img = document.createElement('img');
  1615. img.onload = function () {
  1616. const ctx = canvas.getContext('2d');
  1617. ctx.drawImage(this, 0, 0, canvas.width, canvas.height);
  1618. if (n > 0) {
  1619. let text = '';
  1620. if (n < 1000) {
  1621. text = n;
  1622. } else if (n < 100000) {
  1623. text = Math.floor(n / 1000) + 'k';
  1624. } else {
  1625. text = 'E' + Math.floor(Math.log10(n));
  1626. }
  1627. ctx.font = 'bold ' + 9 * ratio + 'px "Arial", sans-serif';
  1628. ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
  1629. ctx.fillRect(0, 7 * ratio, ctx.measureText(text).width, 9 * ratio);
  1630. ctx.fillStyle = '#F00';
  1631. ctx.fillText(text, 0, canvas.height - 1);
  1632. }
  1633. link.href = canvas.toDataURL('image/png');
  1634. // Check if data URI has generated a real favicon and if not, fallback to standard icon
  1635. if (link.href.length > 180) {
  1636. document.querySelector('link[rel~=icon]').remove();
  1637. document.head.appendChild(link);
  1638. }
  1639. };
  1640. img.src = '../favicon.ico';
  1641. }
  1642. }
  1643. function removeFirstLoadSpinner() {
  1644. const first_load = document.getElementById('first_load');
  1645. if (first_load) {
  1646. first_load.remove();
  1647. }
  1648. }
  1649. function init_normal() {
  1650. const stream = document.getElementById('stream');
  1651. if (!stream) {
  1652. if (window.console) {
  1653. console.log('FreshRSS waiting for content…');
  1654. }
  1655. setTimeout(init_normal, 100);
  1656. return;
  1657. }
  1658. init_column_categories();
  1659. init_stream(stream);
  1660. init_shortcuts();
  1661. init_actualize();
  1662. faviconNbUnread();
  1663. window.onbeforeunload = function (e) {
  1664. const sidebar = document.getElementById('sidebar');
  1665. if (sidebar) { // Save sidebar scroll position
  1666. sessionStorage.setItem('FreshRSS_sidebar_scrollTop', sidebar.scrollTop);
  1667. }
  1668. if (mark_read_queue && mark_read_queue.length > 0) {
  1669. return false;
  1670. }
  1671. };
  1672. }
  1673. function init_beforeDOM() {
  1674. document.scrollingElement.scrollTop = 0;
  1675. if (['normal', 'reader', 'global'].indexOf(context.current_view) >= 0) {
  1676. init_normal();
  1677. }
  1678. }
  1679. function init_afterDOM() {
  1680. removeFirstLoadSpinner();
  1681. init_notifications();
  1682. init_slider_observers();
  1683. init_popup();
  1684. init_confirm_action();
  1685. const stream = document.getElementById('stream');
  1686. if (stream) {
  1687. init_load_more(stream);
  1688. init_posts();
  1689. init_nav_entries();
  1690. init_notifs_html5();
  1691. setTimeout(faviconNbUnread, 1000);
  1692. setInterval(refreshUnreads, 120000);
  1693. }
  1694. if (window.console) {
  1695. console.log('FreshRSS main init done.');
  1696. }
  1697. }
  1698. init_beforeDOM(); // Can be called before DOM is fully loaded
  1699. if (document.readyState && document.readyState !== 'loading') {
  1700. init_afterDOM();
  1701. } else {
  1702. if (window.console) {
  1703. console.log('FreshRSS waiting for DOMContentLoaded…');
  1704. }
  1705. document.addEventListener('DOMContentLoaded', init_afterDOM, false);
  1706. }
  1707. // @license-end