main.js 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072
  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.ctrlKey || ev.metaKey || (ev.altKey && ev.shiftKey) || ev.target.closest('input, select, textarea')) {
  890. return;
  891. }
  892. const s = context.shortcuts;
  893. let k = (ev.key.trim() || ev.code || 'Space').toUpperCase();
  894. // IE11
  895. if (k === 'SPACEBAR') k = 'SPACE';
  896. else if (k === 'DEL') k = 'DELETE';
  897. else if (k === 'ESC') k = 'ESCAPE';
  898. if (location.hash.match(/^#dropdown-/)) {
  899. const n = parseInt(k);
  900. if (n) {
  901. if (location.hash === '#dropdown-query') {
  902. user_filter(n);
  903. } else {
  904. auto_share(n);
  905. }
  906. ev.preventDefault();
  907. return;
  908. }
  909. }
  910. if (k === s.actualize) {
  911. const btn = document.getElementById('actualize');
  912. if (btn) {
  913. btn.click();
  914. }
  915. ev.preventDefault();
  916. return;
  917. }
  918. if (k === s.next_entry) {
  919. if (ev.altKey) {
  920. next_category();
  921. } else if (ev.shiftKey) {
  922. next_feed();
  923. } else {
  924. next_entry(false);
  925. }
  926. ev.preventDefault();
  927. return;
  928. }
  929. if (k === s.next_unread_entry) {
  930. if (ev.altKey) {
  931. next_unread_category();
  932. } else if (ev.shiftKey) {
  933. next_feed();
  934. } else {
  935. next_unread_entry(false);
  936. }
  937. ev.preventDefault();
  938. return;
  939. }
  940. if (k === s.prev_entry) {
  941. if (ev.altKey) {
  942. prev_category();
  943. } else if (ev.shiftKey) {
  944. prev_feed();
  945. } else {
  946. prev_entry(false);
  947. }
  948. ev.preventDefault();
  949. return;
  950. }
  951. if (k === s.mark_read) {
  952. if (ev.altKey) {
  953. mark_previous_read(document.querySelector('.flux.current'));
  954. } else if (ev.shiftKey) {
  955. document.querySelector('.nav_menu .read_all').click();
  956. } else { // Toggle the read state
  957. mark_read(document.querySelector('.flux.current'), false, false);
  958. }
  959. ev.preventDefault();
  960. return;
  961. }
  962. if (k === s.first_entry) {
  963. if (ev.altKey) {
  964. first_category();
  965. } else if (ev.shiftKey) {
  966. first_feed();
  967. } else {
  968. const old_active = document.querySelector('.flux.current');
  969. const first = document.querySelector('.flux');
  970. if (first.classList.contains('flux')) {
  971. toggleContent(first, old_active, false);
  972. }
  973. }
  974. ev.preventDefault();
  975. return;
  976. }
  977. if (k === s.last_entry) {
  978. if (ev.altKey) {
  979. last_category();
  980. } else if (ev.shiftKey) {
  981. last_feed();
  982. } else {
  983. const old_active = document.querySelector('.flux.current');
  984. const last = document.querySelector('.flux:last-of-type');
  985. if (last.classList.contains('flux')) {
  986. toggleContent(last, old_active, false);
  987. }
  988. }
  989. ev.preventDefault();
  990. return;
  991. }
  992. if (ev.key === '?') {
  993. window.location.href = context.urls.shortcuts.replace(/&amp;/g, '&');
  994. return;
  995. }
  996. if (ev.altKey || ev.shiftKey) {
  997. return;
  998. }
  999. if (k === s.mark_favorite) { // Toggle the favorite state
  1000. mark_favorite(document.querySelector('.flux.current'));
  1001. ev.preventDefault();
  1002. return;
  1003. }
  1004. if (k === s.go_website) {
  1005. if (context.auto_mark_site) {
  1006. mark_read(document.querySelector('.flux.current'), true, false);
  1007. }
  1008. const link_go_website = document.querySelector('.flux.current a.go_website');
  1009. if (link_go_website) {
  1010. const newWindow = window.open();
  1011. if (newWindow) {
  1012. newWindow.opener = null;
  1013. newWindow.location = link_go_website.href;
  1014. ev.preventDefault();
  1015. }
  1016. }
  1017. return;
  1018. }
  1019. if (k === s.skip_next_entry) { next_entry(true); ev.preventDefault(); return; }
  1020. if (k === s.skip_prev_entry) { prev_entry(true); ev.preventDefault(); return; }
  1021. if (k === s.collapse_entry) { collapse_entry(); ev.preventDefault(); return; }
  1022. if (k === s.auto_share) { auto_share(); ev.preventDefault(); return; }
  1023. if (k === s.user_filter) { user_filter(); ev.preventDefault(); return; }
  1024. if (k === s.load_more) { load_more_posts(); ev.preventDefault(); return; }
  1025. if (k === s.close_dropdown) { location.hash = null; ev.preventDefault(); return; }
  1026. if (k === s.help) { window.open(context.urls.help); ev.preventDefault(); return; }
  1027. if (k === s.focus_search) { document.getElementById('search').focus(); ev.preventDefault(); return; }
  1028. if (k === s.normal_view) { delayedClick(document.querySelector('#nav_menu_views .view-normal')); ev.preventDefault(); return; }
  1029. if (k === s.reading_view) { delayedClick(document.querySelector('#nav_menu_views .view-reader')); ev.preventDefault(); return; }
  1030. if (k === s.global_view) { delayedClick(document.querySelector('#nav_menu_views .view-global')); ev.preventDefault(); return; }
  1031. if (k === s.toggle_media) { toggle_media(); ev.preventDefault(); }
  1032. });
  1033. }
  1034. function init_stream(stream) {
  1035. stream.onclick = function (ev) {
  1036. let el = ev.target.closest('.flux a.read');
  1037. if (el) {
  1038. mark_read(el.closest('.flux'), false, false);
  1039. return false;
  1040. }
  1041. el = ev.target.closest('.flux a.bookmark');
  1042. if (el) {
  1043. mark_favorite(el.closest('.flux'));
  1044. return false;
  1045. }
  1046. el = ev.target.closest('.item a.title');
  1047. if (el) { // Allow default control/command-click behaviour such as open in background-tab
  1048. return ev.ctrlKey || ev.metaKey;
  1049. }
  1050. el = ev.target.closest('.flux .content .text a');
  1051. if (el) {
  1052. if (!el.closest('div').classList.contains('author')) {
  1053. el.target = '_blank';
  1054. el.rel = 'noreferrer';
  1055. }
  1056. return true;
  1057. }
  1058. el = ev.target.closest('.item.labels a.dropdown-toggle');
  1059. if (el) {
  1060. return show_labels_menu(el);
  1061. }
  1062. el = ev.target.closest('.item.share a.dropdown-toggle');
  1063. if (el) {
  1064. return show_share_menu(el);
  1065. }
  1066. el = ev.target.closest('.item.share > button[data-type="print"]');
  1067. if (el) { // Print
  1068. const tmp_window = window.open();
  1069. for (let i = 0; i < document.styleSheets.length; i++) {
  1070. tmp_window.document.writeln('<link href="' + document.styleSheets[i].href + '" rel="stylesheet" type="text/css" />');
  1071. }
  1072. const flux_content = el.closest('.flux_content');
  1073. let content_el = null;
  1074. if (flux_content) {
  1075. content_el = el.closest('.flux_content').querySelector('.content');
  1076. }
  1077. if (content_el === null) {
  1078. content_el = el.closest('.flux').querySelector('.flux_content .content');
  1079. }
  1080. loadLazyImages(content_el);
  1081. tmp_window.document.writeln(content_el.innerHTML);
  1082. tmp_window.document.close();
  1083. tmp_window.focus();
  1084. tmp_window.print();
  1085. tmp_window.close();
  1086. return false;
  1087. }
  1088. el = ev.target.closest('.item.share > button[data-type="clipboard"]');
  1089. if (el) { // Clipboard
  1090. if (navigator.clipboard) {
  1091. navigator.clipboard.writeText(el.dataset.url)
  1092. .then(() => {
  1093. toggleClass(el, 'ok');
  1094. })
  1095. .catch(e => {
  1096. console.log(e);
  1097. toggleClass(el, 'error');
  1098. });
  1099. } else {
  1100. // fallback, if navigator.clipboard is not available f.e. if access is not via https or localhost
  1101. const inputElement = document.createElement('input');
  1102. inputElement.value = el.dataset.url;
  1103. document.body.appendChild(inputElement);
  1104. inputElement.select();
  1105. if (document.execCommand && document.execCommand('copy')) {
  1106. toggleClass(el, 'ok');
  1107. } else {
  1108. console.log('document.execCommand("copy") failed');
  1109. toggleClass(el, 'error');
  1110. }
  1111. inputElement.remove();
  1112. }
  1113. return false;
  1114. }
  1115. el = ev.target.closest('.item.share > button[data-type="web-sharing-api"]');
  1116. if (el && navigator.share) { // https://developer.mozilla.org/en-US/docs/Web/API/Navigator/share
  1117. const shareData = {
  1118. url: el.dataset.url,
  1119. title: decodeURI(el.dataset.title),
  1120. };
  1121. navigator.share(shareData);
  1122. return false;
  1123. }
  1124. el = ev.target.closest('.item.share > a[data-type="email-webmail-firefox-fix"]');
  1125. if (el) {
  1126. window.open(el.href);
  1127. return false;
  1128. }
  1129. el = ev.target.closest('.item.share > a[href="POST"]');
  1130. if (el) { // Share by POST
  1131. const f = el.parentElement.querySelector('form');
  1132. f.disabled = false;
  1133. f.submit();
  1134. return false;
  1135. }
  1136. el = ev.target.closest('.flux_header, .flux_content');
  1137. if (el) { // flux_toggle
  1138. if (ev.target.closest('.reader, .content, .item.website, .item.link, .dropdown')) {
  1139. return true;
  1140. }
  1141. if ((!context.sides_close_article && ev.target.matches('.flux_content')) || ev.target.closest('footer')) {
  1142. // setting for not-closing after clicking outside article area
  1143. return false;
  1144. }
  1145. const old_active = document.querySelector('.flux.current');
  1146. const new_active = el.parentNode;
  1147. if (ev.target.tagName.toUpperCase() === 'A') { // Leave real links alone (but does not catch img in a link)
  1148. if (context.auto_mark_article) {
  1149. mark_read(new_active, true, false);
  1150. }
  1151. return true;
  1152. }
  1153. toggleContent(new_active, old_active, false);
  1154. return false;
  1155. }
  1156. };
  1157. stream.onmouseup = function (ev) { // Mouseup enables us to catch middle click, and control+click in IE/Edge
  1158. if (ev.altKey || ev.metaKey || ev.shiftKey) {
  1159. return;
  1160. }
  1161. let el = ev.target.closest('.item a.title');
  1162. if (el) {
  1163. if (ev.which == 1) {
  1164. if (ev.ctrlKey) { // Control+click
  1165. if (context.auto_mark_site) {
  1166. mark_read(el.closest('.flux'), true, false);
  1167. }
  1168. } else {
  1169. el.parentElement.click(); // Normal click, just toggle article.
  1170. }
  1171. } else if (ev.which == 2 && !ev.ctrlKey) { // Simple middle click: same behaviour as CTRL+click
  1172. if (context.auto_mark_article) {
  1173. const new_active = el.closest('.flux');
  1174. mark_read(new_active, true, false);
  1175. }
  1176. }
  1177. return;
  1178. }
  1179. if (context.auto_mark_site) {
  1180. // catch mouseup instead of click so we can have the correct behaviour
  1181. // with middle button click (scroll button).
  1182. el = ev.target.closest('.flux .link > a');
  1183. if (el) {
  1184. if (ev.which == 3) {
  1185. return;
  1186. }
  1187. mark_read(el.closest('.flux'), true, false);
  1188. }
  1189. }
  1190. };
  1191. stream.onchange = function (ev) {
  1192. const checkboxTag = ev.target.closest('.checkboxTag');
  1193. if (checkboxTag) { // Dynamic tags
  1194. ev.stopPropagation();
  1195. const tagId = checkboxTag.name.replace(/^t_/, '');
  1196. const tagName = checkboxTag.nextElementSibling ? checkboxTag.nextElementSibling.childNodes[0].value : '';
  1197. if ((tagId == 0 && tagName.length > 0) || tagId != 0) {
  1198. const isChecked = checkboxTag.checked;
  1199. const entry = checkboxTag.closest('div.flux');
  1200. const entryId = entry.id.replace(/^flux_/, '');
  1201. checkboxTag.disabled = true;
  1202. const req = new XMLHttpRequest();
  1203. req.open('POST', './?c=tag&a=tagEntry&ajax=1', true);
  1204. req.responseType = 'json';
  1205. req.onerror = function (e) {
  1206. checkboxTag.checked = !isChecked;
  1207. badAjax(this.status == 403);
  1208. };
  1209. req.onload = function (e) {
  1210. if (this.status != 200) {
  1211. return req.onerror(e);
  1212. }
  1213. if (entry.classList.contains('not_read')) {
  1214. incUnreadsTag('t_' + tagId, isChecked ? 1 : -1);
  1215. }
  1216. };
  1217. req.onloadend = function (e) {
  1218. checkboxTag.disabled = false;
  1219. if (tagId == 0) {
  1220. // new tag is added
  1221. forceReloadLabelsList = true;
  1222. loadDynamicTags(checkboxTag.closest('div.dropdown'));
  1223. } else {
  1224. // a tag was (un)checked
  1225. const dropdownmenu_current = ev.target.closest('.dropdown-menu');
  1226. const flux = ev.target.closest('.flux');
  1227. const dropdownmenu_all = flux.querySelectorAll('.dynamictags .dropdown-menu');
  1228. if (dropdownmenu_all.length > 1) {
  1229. // delete all other tag dropdown menus except the current one
  1230. dropdownmenu_all.forEach(
  1231. function (currentValue) {
  1232. if (currentValue !== dropdownmenu_current) {
  1233. currentValue.nextElementSibling.remove();
  1234. currentValue.parentNode.removeChild(currentValue);
  1235. }
  1236. }
  1237. );
  1238. }
  1239. }
  1240. };
  1241. req.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
  1242. req.send(JSON.stringify({
  1243. _csrf: context.csrf,
  1244. id_tag: tagId,
  1245. name_tag: tagId == 0 ? tagName : '',
  1246. id_entry: entryId,
  1247. checked: isChecked,
  1248. ajax: 1,
  1249. }));
  1250. }
  1251. }
  1252. };
  1253. }
  1254. function toggleClass(el, cssclass) {
  1255. el.classList.remove(cssclass);
  1256. el.dataset.foo = el.offsetWidth; // it does nothing, but it is needed. See https://github.com/FreshRSS/FreshRSS/pull/5295
  1257. el.classList.add(cssclass);
  1258. }
  1259. function init_nav_entries() {
  1260. const nav_entries = document.getElementById('nav_entries');
  1261. if (nav_entries) {
  1262. nav_entries.querySelector('.previous_entry').onclick = function (e) {
  1263. prev_entry(false);
  1264. return false;
  1265. };
  1266. nav_entries.querySelector('.next_entry').onclick = function (e) {
  1267. next_entry(false);
  1268. return false;
  1269. };
  1270. nav_entries.querySelector('.up').onclick = function (e) {
  1271. const active_item = (document.querySelector('.flux.current') || document.querySelector('.flux'));
  1272. const windowTop = document.scrollingElement.scrollTop;
  1273. const item_top = active_item.offsetParent.offsetTop + active_item.offsetTop;
  1274. const nav_menu = document.querySelector('.nav_menu');
  1275. let nav_menu_height = 0;
  1276. if (getComputedStyle(nav_menu).position === 'fixed' || getComputedStyle(nav_menu).position === 'sticky') {
  1277. nav_menu_height = nav_menu.offsetHeight;
  1278. }
  1279. document.scrollingElement.scrollTop = windowTop > item_top ? item_top - nav_menu_height : 0 - nav_menu_height;
  1280. return false;
  1281. };
  1282. }
  1283. }
  1284. // forceReloadLabelsList default is false, so that the list does need a reload after opening it a second time.
  1285. // will be set to true, if a new tag is added. Then the labels list will be reloaded each opening.
  1286. // purpose of this flag: minimize the network traffic.
  1287. let forceReloadLabelsList = false;
  1288. function loadDynamicTags(div) {
  1289. div.querySelectorAll('li.item').forEach(function (li) { li.remove(); });
  1290. const entryId = div.closest('div.flux').id.replace(/^flux_/, '');
  1291. const req = new XMLHttpRequest();
  1292. req.open('GET', './?c=tag&a=getTagsForEntry&id_entry=' + entryId, true);
  1293. req.responseType = 'json';
  1294. req.onerror = function (e) {
  1295. div.querySelectorAll('li.item').forEach(function (li) { li.remove(); });
  1296. };
  1297. req.onload = function (e) {
  1298. if (this.status != 200) {
  1299. return req.onerror(e);
  1300. }
  1301. const json = xmlHttpRequestJson(this);
  1302. if (!json) {
  1303. return req.onerror(e);
  1304. }
  1305. if (!context.anonymous) {
  1306. const li_item0 = document.createElement('li');
  1307. li_item0.setAttribute('class', 'item addItem');
  1308. const label = document.createElement('label');
  1309. label.setAttribute('class', 'noHover');
  1310. const input_checkboxTag = document.createElement('input');
  1311. input_checkboxTag.setAttribute('class', 'checkboxTag checkboxNewTag');
  1312. input_checkboxTag.setAttribute('name', 't_0');
  1313. input_checkboxTag.setAttribute('type', 'checkbox');
  1314. const input_newTag = document.createElement('input');
  1315. input_newTag.setAttribute('type', 'text');
  1316. input_newTag.setAttribute('name', 'newTag');
  1317. input_newTag.setAttribute('list', 'datalist-labels');
  1318. input_newTag.addEventListener('keydown', function (ev) { if (ev.key.toUpperCase() == 'ENTER') { this.parentNode.previousSibling.click(); } });
  1319. const button_btn = document.createElement('button');
  1320. button_btn.setAttribute('type', 'button');
  1321. button_btn.setAttribute('class', 'btn');
  1322. button_btn.addEventListener('click', function () { this.parentNode.parentNode.click(); });
  1323. const text_plus = document.createTextNode('+');
  1324. const div_stick = document.createElement('div');
  1325. div_stick.setAttribute('class', 'stick');
  1326. button_btn.appendChild(text_plus);
  1327. div_stick.appendChild(input_newTag);
  1328. div_stick.appendChild(button_btn);
  1329. label.appendChild(input_checkboxTag);
  1330. label.appendChild(div_stick);
  1331. li_item0.appendChild(label);
  1332. div.querySelector('.dropdown-menu-scrollable').appendChild(li_item0);
  1333. }
  1334. let html = '';
  1335. let datalist = '';
  1336. if (json && json.length) {
  1337. let nbLabelsChecked = 0;
  1338. for (let i = 0; i < json.length; i++) {
  1339. const tag = json[i];
  1340. if (context.anonymous && !tag.checked) {
  1341. // In anomymous mode, show only the used tags
  1342. continue;
  1343. }
  1344. if (tag.checked) {
  1345. nbLabelsChecked++;
  1346. }
  1347. html += '<li class="item"><label><input ' +
  1348. (context.anonymous ? '' : 'class="checkboxTag" ') +
  1349. 'name="t_' + tag.id + '"type="checkbox" ' +
  1350. (context.anonymous ? 'disabled="disabled" ' : '') +
  1351. (tag.checked ? 'checked="checked" ' : '') + '/>' + tag.name + '</label></li>';
  1352. datalist += '<option value="' + tag.name + '"></option>';
  1353. }
  1354. if (context.anonymous && nbLabelsChecked === 0) {
  1355. html += '<li class="item"><span class="emptyLabels">' + context.i18n.labels_empty + '</span></li>';
  1356. }
  1357. }
  1358. div.querySelector('.dropdown-menu-scrollable').insertAdjacentHTML('beforeend', html);
  1359. const datalistLabels = document.getElementById('datalist-labels');
  1360. datalistLabels.innerHTML = ''; // clear before add the (updated) labels list
  1361. datalistLabels.insertAdjacentHTML('beforeend', datalist);
  1362. };
  1363. req.send();
  1364. }
  1365. // <actualize>
  1366. let feeds_processed = 0;
  1367. let categories_processed = 0;
  1368. let to_process = 0;
  1369. function refreshFeed(feeds, feeds_count) {
  1370. const feed = feeds.pop();
  1371. if (!feed) {
  1372. return;
  1373. }
  1374. const req = new XMLHttpRequest();
  1375. req.open('POST', feed.url, true);
  1376. req.onloadend = function (e) {
  1377. feeds_processed++;
  1378. if (this.status != 200) {
  1379. badAjax(false);
  1380. } else {
  1381. const div = document.getElementById('actualizeProgress');
  1382. div.querySelector('.progress').innerHTML = (categories_processed + feeds_processed) + ' / ' + to_process;
  1383. div.querySelector('.title').innerHTML = feed.title;
  1384. }
  1385. if (feeds_processed === feeds_count) {
  1386. // Empty request to commit new articles
  1387. const req2 = new XMLHttpRequest();
  1388. req2.open('POST', './?c=feed&a=actualize&id=-1&ajax=1', true);
  1389. req2.onloadend = function (e) {
  1390. delayedFunction(function () { location.reload(); });
  1391. };
  1392. req2.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
  1393. req2.send(JSON.stringify({
  1394. _csrf: context.csrf,
  1395. noCommit: 0,
  1396. }));
  1397. } else {
  1398. refreshFeed(feeds, feeds_count);
  1399. }
  1400. };
  1401. req.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
  1402. req.send(JSON.stringify({
  1403. _csrf: context.csrf,
  1404. noCommit: 1,
  1405. }));
  1406. }
  1407. function refreshFeeds(json) {
  1408. feeds_processed = 0;
  1409. if (!json.feeds || json.feeds.length === 0) {
  1410. // Empty request to commit new articles
  1411. const req2 = new XMLHttpRequest();
  1412. req2.open('POST', './?c=feed&a=actualize&id=-1&ajax=1', true);
  1413. req2.onloadend = function (e) {
  1414. context.ajax_loading = false;
  1415. };
  1416. req2.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
  1417. req2.send(JSON.stringify({
  1418. _csrf: context.csrf,
  1419. noCommit: 0,
  1420. }));
  1421. } else {
  1422. const feeds_count = json.feeds.length;
  1423. for (let i = context.nb_parallel_refresh; i > 0; i--) {
  1424. refreshFeed(json.feeds, feeds_count);
  1425. }
  1426. }
  1427. }
  1428. function refreshDynamicOpml(categories, categories_count, next) {
  1429. const category = categories.pop();
  1430. if (!category) {
  1431. return;
  1432. }
  1433. const req = new XMLHttpRequest();
  1434. req.open('POST', category.url, true);
  1435. req.onloadend = function (e) {
  1436. categories_processed++;
  1437. if (this.status != 200) {
  1438. badAjax(false);
  1439. } else {
  1440. const div = document.getElementById('actualizeProgress');
  1441. div.querySelector('.progress').innerHTML = (categories_processed + feeds_processed) + ' / ' + to_process;
  1442. div.querySelector('.title').innerHTML = category.title;
  1443. }
  1444. if (categories_processed === categories_count) {
  1445. if (next) { next(); }
  1446. } else {
  1447. refreshDynamicOpml(categories, categories_count, next);
  1448. }
  1449. };
  1450. req.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
  1451. req.send(JSON.stringify({
  1452. _csrf: context.csrf,
  1453. noCommit: 1,
  1454. }));
  1455. }
  1456. function refreshDynamicOpmls(json, next) {
  1457. categories_processed = 0;
  1458. if (json.categories && json.categories.length > 0) {
  1459. const categories_count = json.categories.length;
  1460. for (let i = context.nb_parallel_refresh; i > 0; i--) {
  1461. refreshDynamicOpml(json.categories, categories_count, next);
  1462. }
  1463. } else {
  1464. if (next) { next(); }
  1465. }
  1466. }
  1467. function init_actualize() {
  1468. let auto = false;
  1469. let nbCategoriesFirstRound = 0;
  1470. let skipCategories = false;
  1471. const actualize = document.getElementById('actualize');
  1472. if (!actualize) {
  1473. return;
  1474. }
  1475. actualize.onclick = function () {
  1476. if (context.ajax_loading) {
  1477. return false;
  1478. }
  1479. context.ajax_loading = true;
  1480. const req = new XMLHttpRequest();
  1481. req.open('POST', './?c=javascript&a=actualize', true);
  1482. req.responseType = 'json';
  1483. req.onload = function (e) {
  1484. if (this.status != 200) {
  1485. return badAjax(false);
  1486. }
  1487. const json = xmlHttpRequestJson(this);
  1488. if (!json) {
  1489. return badAjax(false);
  1490. }
  1491. if (auto && json.categories.length < 1 && json.feeds.length < 1) {
  1492. auto = false;
  1493. context.ajax_loading = false;
  1494. return false;
  1495. }
  1496. to_process = json.categories.length + json.feeds.length + nbCategoriesFirstRound;
  1497. if (json.categories.length + json.feeds.length > 0 && !document.getElementById('actualizeProgress')) {
  1498. document.body.insertAdjacentHTML('beforeend', '<div id="actualizeProgress" class="notification good">' +
  1499. json.feedback_actualize + '<br /><span class="title">/</span><br /><span class="progress">0 / ' +
  1500. to_process + '</span></div>');
  1501. } else {
  1502. openNotification(json.feedback_no_refresh, 'good');
  1503. }
  1504. if (json.categories.length > 0 && !skipCategories) {
  1505. skipCategories = true; // To avoid risk of infinite loop
  1506. nbCategoriesFirstRound = json.categories.length;
  1507. // If some dynamic OPML categories are refreshed, need to reload the list of feeds before updating them
  1508. refreshDynamicOpmls(json, () => {
  1509. context.ajax_loading = false;
  1510. actualize.click();
  1511. });
  1512. } else {
  1513. refreshFeeds(json);
  1514. }
  1515. };
  1516. req.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
  1517. req.send(JSON.stringify({
  1518. _csrf: context.csrf,
  1519. }));
  1520. return false;
  1521. };
  1522. if (context.auto_actualize_feeds) {
  1523. auto = true;
  1524. actualize.click();
  1525. }
  1526. }
  1527. // </actualize>
  1528. // <notification>
  1529. let notification = null;
  1530. let notification_interval = null;
  1531. let notification_working = false;
  1532. function openNotification(msg, status) {
  1533. if (notification_working === true) {
  1534. return false;
  1535. }
  1536. notification_working = true;
  1537. notification.querySelector('.msg').innerHTML = msg;
  1538. notification.className = 'notification';
  1539. notification.classList.add(status);
  1540. if (status == 'good') {
  1541. notification_interval = setTimeout(closeNotification, 4000);
  1542. } else {
  1543. // no status or f.e. status = 'bad', give some more time to read
  1544. notification_interval = setTimeout(closeNotification, 8000);
  1545. }
  1546. }
  1547. function closeNotification() {
  1548. notification.classList.add('closed');
  1549. clearInterval(notification_interval);
  1550. notification_working = false;
  1551. }
  1552. function init_notifications() {
  1553. notification = document.getElementById('notification');
  1554. notification.querySelector('a.close').addEventListener('click', function (ev) {
  1555. closeNotification();
  1556. ev.preventDefault();
  1557. return false;
  1558. });
  1559. notification.addEventListener('mouseenter', function () {
  1560. clearInterval(notification_interval);
  1561. });
  1562. notification.addEventListener('mouseleave', function () {
  1563. notification_interval = setTimeout(closeNotification, 3000);
  1564. });
  1565. if (notification.querySelector('.msg').innerHTML.length > 0) {
  1566. notification_working = true;
  1567. if (notification.classList.contains('good')) {
  1568. notification_interval = setTimeout(closeNotification, 4000);
  1569. } else {
  1570. // no status or f.e. status = 'bad', give some more time to read
  1571. notification_interval = setTimeout(closeNotification, 8000);
  1572. }
  1573. }
  1574. }
  1575. // </notification>
  1576. // <notifs html5>
  1577. let notifs_html5_permission = 'denied';
  1578. function notifs_html5_is_supported() {
  1579. return window.Notification !== undefined;
  1580. }
  1581. function notifs_html5_ask_permission() {
  1582. try {
  1583. window.Notification.requestPermission(function () {
  1584. notifs_html5_permission = window.Notification.permission;
  1585. });
  1586. } catch (e) {
  1587. }
  1588. }
  1589. function notifs_html5_show(nb, nb_new) {
  1590. if (notifs_html5_permission !== 'granted') {
  1591. return;
  1592. }
  1593. try {
  1594. const notification = new window.Notification(context.i18n.notif_title_articles, {
  1595. icon: '../themes/icons/favicon-256-padding.png',
  1596. body: context.i18n.notif_body_new_articles.replace('%%d', nb_new) + ' ' + context.i18n.notif_body_unread_articles.replace('%%d', nb),
  1597. tag: 'freshRssNewArticles',
  1598. });
  1599. notification.onclick = function () {
  1600. delayedFunction(function () {
  1601. location.reload();
  1602. window.focus();
  1603. notification.close();
  1604. });
  1605. };
  1606. if (context.html5_notif_timeout !== 0) {
  1607. setTimeout(function () {
  1608. notification.close();
  1609. }, context.html5_notif_timeout * 1000);
  1610. }
  1611. } catch (e) {
  1612. }
  1613. }
  1614. function init_notifs_html5() {
  1615. if (!notifs_html5_is_supported()) {
  1616. return;
  1617. }
  1618. notifs_html5_permission = notifs_html5_ask_permission();
  1619. }
  1620. // </notifs html5>
  1621. function refreshUnreads() {
  1622. const req = new XMLHttpRequest();
  1623. req.open('GET', './?c=javascript&a=nbUnreadsPerFeed', true);
  1624. req.responseType = 'json';
  1625. req.onload = function (e) {
  1626. const json = xmlHttpRequestJson(this);
  1627. if (!json) {
  1628. return badAjax(false);
  1629. }
  1630. const isAll = document.querySelector('.category.all.active');
  1631. let new_articles = false;
  1632. let nbUnreadFeeds = 0;
  1633. const title = document.querySelector('.category.all .title');
  1634. const nb_unreads_before = title ? str2int(title.getAttribute('data-unread')) : 0;
  1635. Object.keys(json.feeds).forEach(function (feed_id) {
  1636. const nbUnreads = json.feeds[feed_id];
  1637. nbUnreadFeeds += nbUnreads;
  1638. feed_id = 'f_' + feed_id;
  1639. const elem = document.getElementById(feed_id);
  1640. const feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  1641. if ((incUnreadsFeed(null, feed_id, nbUnreads - feed_unreads) || isAll) && // Update of current view?
  1642. (nbUnreads - feed_unreads > 0)) {
  1643. const newArticle = document.getElementById('new-article');
  1644. newArticle.removeAttribute('hidden');
  1645. newArticle.style.display = 'block';
  1646. new_articles = true;
  1647. }
  1648. });
  1649. const noArticlesToShow_div = document.getElementById('noArticlesToShow');
  1650. if (nbUnreadFeeds > 0 && noArticlesToShow_div) {
  1651. noArticlesToShow_div.classList.add('hide');
  1652. }
  1653. let nbUnreadTags = 0;
  1654. Object.keys(json.tags).forEach(function (tag_id) {
  1655. const nbUnreads = json.tags[tag_id];
  1656. nbUnreadTags += nbUnreads;
  1657. const tag = document.getElementById('t_' + tag_id);
  1658. if (tag) {
  1659. tag.setAttribute('data-unread', nbUnreads);
  1660. tag.querySelector('.item-title').setAttribute('data-unread', numberFormat(nbUnreads));
  1661. }
  1662. });
  1663. const tags = document.querySelector('.category.tags');
  1664. if (tags) {
  1665. tags.setAttribute('data-unread', nbUnreadTags);
  1666. tags.querySelector('.title').setAttribute('data-unread', numberFormat(nbUnreadTags));
  1667. }
  1668. const nb_unreads = title ? str2int(title.getAttribute('data-unread')) : 0;
  1669. if (nb_unreads > 0 && new_articles) {
  1670. faviconNbUnread(nb_unreads);
  1671. const nb_new = nb_unreads - nb_unreads_before;
  1672. notifs_html5_show(nb_unreads, nb_new);
  1673. }
  1674. };
  1675. req.send();
  1676. }
  1677. function toggle_bigMarkAsRead_button() {
  1678. const bigMarkAsRead_button = document.getElementById('bigMarkAsRead');
  1679. if (bigMarkAsRead_button) {
  1680. if (document.querySelector('.flux.not_read') != null) {
  1681. bigMarkAsRead_button.style = '';
  1682. bigMarkAsRead_button.querySelector('.markAllRead').style.visibility = '';
  1683. } else {
  1684. if (bigMarkAsRead_button.querySelector('.jumpNext')) {
  1685. bigMarkAsRead_button.querySelector('.markAllRead').style.visibility = 'hidden';
  1686. } else {
  1687. bigMarkAsRead_button.querySelector('.markAllRead').style.visibility = '';
  1688. bigMarkAsRead_button.style.visibility = 'hidden';
  1689. }
  1690. }
  1691. }
  1692. }
  1693. // <endless_mode>
  1694. let url_load_more = '';
  1695. let load_more = false;
  1696. let box_load_more = null;
  1697. function load_more_posts() {
  1698. if (load_more || !url_load_more || !box_load_more) {
  1699. return;
  1700. }
  1701. load_more = true;
  1702. document.getElementById('load_more').classList.add('loading');
  1703. const req = new XMLHttpRequest();
  1704. req.open('GET', url_load_more, true);
  1705. req.responseType = 'document';
  1706. req.onload = function (e) {
  1707. const html = this.response;
  1708. const streamFooter = document.getElementById('stream-footer');
  1709. const streamAdopted = document.adoptNode(html.getElementById('stream'));
  1710. streamAdopted.querySelectorAll('.flux, .day').forEach(function (div) {
  1711. box_load_more.insertBefore(div, streamFooter);
  1712. });
  1713. const streamFooterOld = streamFooter.querySelector('.stream-footer-inner');
  1714. const streamFooterNew = streamAdopted.querySelector('.stream-footer-inner');
  1715. streamFooter.replaceChild(streamFooterNew, streamFooterOld);
  1716. const bigMarkAsRead = document.getElementById('bigMarkAsRead');
  1717. const readAll = document.querySelector('#nav_menu_read_all .read_all');
  1718. if (readAll && bigMarkAsRead && bigMarkAsRead.formAction) {
  1719. if (context.display_order === 'ASC') {
  1720. readAll.formAction = bigMarkAsRead.formAction;
  1721. } else {
  1722. bigMarkAsRead.formAction = readAll.formAction;
  1723. }
  1724. toggle_bigMarkAsRead_button();
  1725. }
  1726. document.querySelectorAll('[id^=day_]').forEach(function (div) {
  1727. const ids = document.querySelectorAll('[id="' + div.id + '"]');
  1728. for (let i = ids.length - 1; i > 0; i--) { // Keep only the first
  1729. ids[i].remove();
  1730. }
  1731. });
  1732. init_load_more(box_load_more);
  1733. const div_load_more = document.getElementById('load_more');
  1734. if (bigMarkAsRead) {
  1735. bigMarkAsRead.removeAttribute('disabled');
  1736. }
  1737. if (div_load_more) {
  1738. div_load_more.classList.remove('loading');
  1739. }
  1740. load_more = false;
  1741. };
  1742. req.send();
  1743. }
  1744. const freshrssLoadMoreEvent = document.createEvent('Event');
  1745. freshrssLoadMoreEvent.initEvent('freshrss:load-more', true, true);
  1746. function init_load_more(box) {
  1747. box_load_more = box;
  1748. document.body.dispatchEvent(freshrssLoadMoreEvent);
  1749. const next_button = document.getElementById('load_more');
  1750. if (!next_button) {
  1751. // no more article to load
  1752. url_load_more = '';
  1753. return;
  1754. }
  1755. url_load_more = next_button.getAttribute('formaction');
  1756. next_button.onclick = function (e) {
  1757. load_more_posts();
  1758. return false;
  1759. };
  1760. }
  1761. // </endless_mode>
  1762. function init_confirm_action() {
  1763. document.body.onclick = function (ev) {
  1764. const b = ev.target.closest('.confirm');
  1765. if (b) {
  1766. let str_confirmation = this.getAttribute('data-str-confirm');
  1767. if (!str_confirmation) {
  1768. str_confirmation = context.i18n.confirmation_default;
  1769. }
  1770. return confirm(str_confirmation);
  1771. }
  1772. };
  1773. document.querySelectorAll('button.confirm').forEach(function (b) { b.disabled = false; });
  1774. }
  1775. function faviconNbUnread(n) {
  1776. if (typeof n === 'undefined') {
  1777. const t = document.querySelector('.category.all .title');
  1778. n = t ? str2int(t.getAttribute('data-unread')) : 0;
  1779. }
  1780. // http://remysharp.com/2010/08/24/dynamic-favicons/
  1781. const canvas = document.createElement('canvas');
  1782. const link = document.getElementById('favicon').cloneNode(true);
  1783. const ratio = window.devicePixelRatio;
  1784. if (canvas.getContext && link) {
  1785. canvas.height = canvas.width = 16 * ratio;
  1786. const img = document.createElement('img');
  1787. img.onload = function () {
  1788. const ctx = canvas.getContext('2d');
  1789. ctx.drawImage(this, 0, 0, canvas.width, canvas.height);
  1790. if (n > 0) {
  1791. let text = '';
  1792. if (n < 1000) {
  1793. text = n;
  1794. } else if (n < 100000) {
  1795. text = Math.floor(n / 1000) + 'k';
  1796. } else {
  1797. text = 'E' + Math.floor(Math.log10(n));
  1798. }
  1799. ctx.font = 'bold ' + 9 * ratio + 'px "Arial", sans-serif';
  1800. ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
  1801. ctx.fillRect(0, 7 * ratio, ctx.measureText(text).width, 9 * ratio);
  1802. ctx.fillStyle = '#F00';
  1803. ctx.fillText(text, 0, canvas.height - 1);
  1804. }
  1805. link.href = canvas.toDataURL('image/png');
  1806. // Check if data URI has generated a real favicon and if not, fallback to standard icon
  1807. if (link.href.length > 180) {
  1808. document.querySelector('link[rel~=icon]').remove();
  1809. document.head.appendChild(link);
  1810. }
  1811. };
  1812. img.src = '../favicon.ico';
  1813. }
  1814. }
  1815. function removeFirstLoadSpinner() {
  1816. const first_load = document.getElementById('first_load');
  1817. if (first_load) {
  1818. first_load.remove();
  1819. }
  1820. }
  1821. function init_normal() {
  1822. const stream = document.getElementById('stream');
  1823. if (!stream) {
  1824. if (window.console) {
  1825. console.log('FreshRSS waiting for content…');
  1826. }
  1827. setTimeout(init_normal, 100);
  1828. return;
  1829. }
  1830. init_column_categories();
  1831. init_stream(stream);
  1832. init_actualize();
  1833. faviconNbUnread();
  1834. window.onbeforeunload = function (e) {
  1835. const sidebar = document.getElementById('sidebar');
  1836. if (sidebar) { // Save sidebar scroll position
  1837. sessionStorage.setItem('FreshRSS_sidebar_scrollTop', sidebar.scrollTop);
  1838. }
  1839. if (mark_read_queue && mark_read_queue.length > 0) {
  1840. return false;
  1841. }
  1842. };
  1843. }
  1844. function init_main_beforeDOM() {
  1845. history.scrollRestoration = 'manual';
  1846. document.scrollingElement.scrollTop = 0;
  1847. init_shortcuts();
  1848. if (['normal', 'reader', 'global'].indexOf(context.current_view) >= 0) {
  1849. init_normal();
  1850. }
  1851. }
  1852. function init_main_afterDOM() {
  1853. removeFirstLoadSpinner();
  1854. init_notifications();
  1855. init_confirm_action();
  1856. const stream = document.getElementById('stream');
  1857. if (stream) {
  1858. init_load_more(stream);
  1859. init_posts();
  1860. if (document.getElementById('new-article')) {
  1861. // Only relevant for interactive views
  1862. init_nav_entries();
  1863. init_notifs_html5();
  1864. toggle_bigMarkAsRead_button();
  1865. setTimeout(faviconNbUnread, 1000);
  1866. setInterval(refreshUnreads, 120000);
  1867. }
  1868. }
  1869. if (window.console) {
  1870. console.log('FreshRSS main init done.');
  1871. }
  1872. }
  1873. init_main_beforeDOM(); // Can be called before DOM is fully loaded
  1874. if (document.readyState && document.readyState !== 'loading') {
  1875. init_main_afterDOM();
  1876. } else {
  1877. if (window.console) {
  1878. console.log('FreshRSS waiting for DOMContentLoaded…');
  1879. }
  1880. document.addEventListener('DOMContentLoaded', init_main_afterDOM, false);
  1881. }
  1882. // @license-end