main.js 53 KB

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