main.js 60 KB

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