main.js 50 KB

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