tt-rss.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. var xmlhttp = false;
  2. var total_unread = 0;
  3. var first_run = true;
  4. var display_tags = false;
  5. var global_unread = -1;
  6. var active_title_text = "";
  7. var current_subtitle = "";
  8. var daemon_enabled = false;
  9. var _qfd_deleted_feed = 0;
  10. var firsttime_update = true;
  11. var last_refetch = 0;
  12. var cookie_lifetime = 0;
  13. /*@cc_on @*/
  14. /*@if (@_jscript_version >= 5)
  15. // JScript gives us Conditional compilation, we can cope with old IE versions.
  16. // and security blocked creation of the objects.
  17. try {
  18. xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  19. } catch (e) {
  20. try {
  21. xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  22. } catch (E) {
  23. xmlhttp = false;
  24. }
  25. }
  26. @end @*/
  27. if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
  28. xmlhttp = new XMLHttpRequest();
  29. }
  30. function toggleTags() {
  31. display_tags = !display_tags;
  32. var p = document.getElementById("dispSwitchPrompt");
  33. if (display_tags) {
  34. p.innerHTML = "display feeds";
  35. } else {
  36. p.innerHTML = "display tags";
  37. }
  38. updateFeedList();
  39. }
  40. function dlg_frefresh_callback() {
  41. if (xmlhttp.readyState == 4) {
  42. notify(xmlhttp.responseText);
  43. updateFeedList(false, false);
  44. if (_qfd_deleted_feed) {
  45. var hframe = document.getElementById("headlines-frame");
  46. if (hframe) {
  47. hframe.src = "backend.php?op=error&msg=No%20feed%20selected.";
  48. }
  49. }
  50. closeDlg();
  51. }
  52. }
  53. function dlg_submit_callback() {
  54. if (xmlhttp.readyState == 4) {
  55. notify(xmlhttp.responseText);
  56. closeDlg();
  57. }
  58. }
  59. function dlg_display_callback() {
  60. if (xmlhttp.readyState == 4) {
  61. var dlg = document.getElementById("userDlg");
  62. var dlg_s = document.getElementById("userDlgShadow");
  63. dlg.innerHTML = xmlhttp.responseText;
  64. dlg_s.style.display = "block";
  65. }
  66. }
  67. function hide_unread_callback() {
  68. if (xmlhttp.readyState == 4) {
  69. try {
  70. var reply = xmlhttp.responseXML.firstChild.firstChild;
  71. var value = reply.getAttribute("value");
  72. var hide_read_feeds = (value != "false")
  73. var feeds_doc = window.frames["feeds-frame"].document;
  74. hideOrShowFeeds(feeds_doc, hide_read_feeds);
  75. if (hide_read_feeds) {
  76. setCookie("ttrss_vf_hreadf", 1);
  77. } else {
  78. setCookie("ttrss_vf_hreadf", 0);
  79. }
  80. } catch (e) {
  81. exception_error("hide_unread_callback", e);
  82. }
  83. }
  84. }
  85. function refetch_callback() {
  86. if (xmlhttp.readyState == 4) {
  87. try {
  88. var date = new Date();
  89. last_refetch = date.getTime() / 1000;
  90. if (!xmlhttp.responseXML) {
  91. notify("refetch_callback: backend did not return valid XML");
  92. return;
  93. }
  94. var reply = xmlhttp.responseXML.firstChild;
  95. if (!reply) {
  96. notify("refetch_callback: backend did not return expected XML object");
  97. updateTitle("");
  98. return;
  99. }
  100. var error_code = reply.getAttribute("error-code");
  101. if (error_code && error_code != 0) {
  102. return fatalError(error_code, reply.getAttribute("error-msg"));
  103. }
  104. var f_document = window.frames["feeds-frame"].document;
  105. parse_counters(reply, f_document, window, true);
  106. debug("refetch_callback: done");
  107. if (!daemon_enabled) {
  108. notify("All feeds updated.");
  109. updateTitle("");
  110. } else {
  111. notify("");
  112. }
  113. } catch (e) {
  114. exception_error("refetch_callback", e);
  115. updateTitle("");
  116. }
  117. }
  118. }
  119. function backend_sanity_check_callback() {
  120. if (xmlhttp.readyState == 4) {
  121. try {
  122. if (!xmlhttp.responseXML) {
  123. fatalError(3, "[D001, Reply is not XML]: " + xmlhttp.responseText);
  124. return;
  125. }
  126. var reply = xmlhttp.responseXML.firstChild;
  127. if (!reply) {
  128. fatalError(3, "[D002, Invalid RPC reply]: " + xmlhttp.responseText);
  129. return;
  130. }
  131. var error_code = reply.getAttribute("error-code");
  132. if (error_code && error_code != 0) {
  133. return fatalError(error_code, reply.getAttribute("error-msg"));
  134. }
  135. debug("sanity check ok");
  136. init_second_stage();
  137. } catch (e) {
  138. exception_error("backend_sanity_check_callback", e);
  139. }
  140. }
  141. }
  142. function scheduleFeedUpdate(force) {
  143. if (!daemon_enabled) {
  144. notify("Updating feeds, please wait.");
  145. updateTitle("Updating");
  146. }
  147. var query_str = "backend.php?op=rpc&subop=";
  148. if (force) {
  149. query_str = query_str + "forceUpdateAllFeeds";
  150. } else {
  151. query_str = query_str + "updateAllFeeds";
  152. }
  153. var omode;
  154. if (firsttime_update && !navigator.userAgent.match("Opera")) {
  155. firsttime_update = false;
  156. omode = "T";
  157. } else {
  158. if (display_tags) {
  159. omode = "t";
  160. } else {
  161. omode = "flc";
  162. }
  163. }
  164. query_str = query_str + "&omode=" + omode;
  165. query_str = query_str + "&uctr=" + global_unread;
  166. debug("in scheduleFeedUpdate");
  167. var date = new Date();
  168. if (!xmlhttp_ready(xmlhttp) && last_refetch < date.getTime() / 1000 - 60) {
  169. debug("xmlhttp seems to be stuck, aborting");
  170. xmlhttp.abort();
  171. }
  172. if (xmlhttp_ready(xmlhttp)) {
  173. xmlhttp.open("GET", query_str, true);
  174. xmlhttp.onreadystatechange=refetch_callback;
  175. xmlhttp.send(null);
  176. } else {
  177. debug("xmlhttp busy");
  178. printLockingError();
  179. }
  180. }
  181. function updateFeedList(silent, fetch) {
  182. // if (silent != true) {
  183. // notify("Loading feed list...");
  184. // }
  185. var query_str = "backend.php?op=feeds";
  186. if (display_tags) {
  187. query_str = query_str + "&tags=1";
  188. }
  189. if (getActiveFeedId()) {
  190. query_str = query_str + "&actid=" + getActiveFeedId();
  191. }
  192. if (navigator.userAgent.match("Opera")) {
  193. var date = new Date();
  194. var timestamp = Math.round(date.getTime() / 1000);
  195. query_str = query_str + "&ts=" + timestamp
  196. }
  197. if (fetch) query_str = query_str + "&fetch=yes";
  198. var feeds_frame = document.getElementById("feeds-frame");
  199. feeds_frame.src = query_str;
  200. }
  201. function catchupAllFeeds() {
  202. var query_str = "backend.php?op=feeds&subop=catchupAll";
  203. notify("Marking all feeds as read...");
  204. var feeds_frame = document.getElementById("feeds-frame");
  205. feeds_frame.src = query_str;
  206. global_unread = 0;
  207. updateTitle("");
  208. }
  209. function viewCurrentFeed(skip, subop) {
  210. if (getActiveFeedId()) {
  211. viewfeed(getActiveFeedId(), skip, subop);
  212. } else {
  213. disableContainerChildren("headlinesToolbar", false, document);
  214. viewfeed(-1, skip, subop); // FIXME
  215. }
  216. }
  217. function viewfeed(feed, skip, subop) {
  218. var f = window.frames["feeds-frame"];
  219. f.viewfeed(feed, skip, subop);
  220. }
  221. function timeout() {
  222. scheduleFeedUpdate(false);
  223. var refresh_time = getCookie('ttrss_vf_refresh');
  224. if (!refresh_time) refresh_time = 600;
  225. setTimeout("timeout()", refresh_time*1000);
  226. }
  227. function resetSearch() {
  228. var searchbox = document.getElementById("searchbox")
  229. if (searchbox.value != "" && getActiveFeedId()) {
  230. searchbox.value = "";
  231. viewfeed(getActiveFeedId(), 0, "");
  232. }
  233. }
  234. function search() {
  235. closeDlg();
  236. viewCurrentFeed(0, "");
  237. }
  238. function localPiggieFunction(enable) {
  239. if (enable) {
  240. var query_str = "backend.php?op=feeds&subop=piggie";
  241. if (xmlhttp_ready(xmlhttp)) {
  242. xmlhttp.open("GET", query_str, true);
  243. xmlhttp.onreadystatechange=feedlist_callback;
  244. xmlhttp.send(null);
  245. }
  246. }
  247. }
  248. function localHotkeyHandler(keycode) {
  249. if (keycode == 82) { // r
  250. return scheduleFeedUpdate(true);
  251. }
  252. if (keycode == 85) { // u
  253. if (getActiveFeedId()) {
  254. return viewfeed(getActiveFeedId(), 0, "ForceUpdate");
  255. }
  256. }
  257. if (keycode == 65) { // a
  258. return toggleDispRead();
  259. }
  260. var f_doc = window.frames["feeds-frame"].document;
  261. var feedlist = f_doc.getElementById('feedList');
  262. if (keycode == 74) { // j
  263. var feed = getActiveFeedId();
  264. var new_feed = getRelativeFeedId(feedlist, feed, 'prev');
  265. if (new_feed) viewfeed(new_feed, 0, '');
  266. }
  267. if (keycode == 75) { // k
  268. var feed = getActiveFeedId();
  269. var new_feed = getRelativeFeedId(feedlist, feed, 'next');
  270. if (new_feed) viewfeed(new_feed, 0, '');
  271. }
  272. // notify("KC: " + keycode);
  273. }
  274. // if argument is undefined, current subtitle is not updated
  275. // use blank string to clear subtitle
  276. function updateTitle(s) {
  277. var tmp = "Tiny Tiny RSS";
  278. if (s != undefined) {
  279. current_subtitle = s;
  280. }
  281. if (global_unread > 0) {
  282. tmp = tmp + " (" + global_unread + ")";
  283. }
  284. if (current_subtitle) {
  285. tmp = tmp + " - " + current_subtitle;
  286. }
  287. if (active_title_text.length > 0) {
  288. tmp = tmp + " > " + active_title_text;
  289. }
  290. document.title = tmp;
  291. }
  292. function genericSanityCheck() {
  293. if (!xmlhttp) fatalError(1);
  294. setCookie("ttrss_vf_test", "TEST");
  295. if (getCookie("ttrss_vf_test") != "TEST") {
  296. fatalError(2);
  297. }
  298. return true;
  299. }
  300. function init() {
  301. try {
  302. // this whole shebang is based on http://www.birnamdesigns.com/misc/busted2.html
  303. if (arguments.callee.done) return;
  304. arguments.callee.done = true;
  305. disableContainerChildren("headlinesToolbar", true);
  306. if (!genericSanityCheck())
  307. return;
  308. if (getURLParam('debug')) {
  309. document.getElementById('debug_output').style.display = 'block';
  310. debug('debug mode activated');
  311. }
  312. xmlhttp.open("GET", "backend.php?op=rpc&subop=sanityCheck", true);
  313. xmlhttp.onreadystatechange=backend_sanity_check_callback;
  314. xmlhttp.send(null);
  315. } catch (e) {
  316. exception_error("init", e);
  317. }
  318. }
  319. function resize_feeds_frame() {
  320. var f = document.getElementById("feeds-frame");
  321. var tf = document.getElementById("mainFooter");
  322. var th = document.getElementById("mainHeader");
  323. f.style.height = document.body.scrollHeight - tf.scrollHeight -
  324. th.scrollHeight - 50 + "px";
  325. }
  326. function init_second_stage() {
  327. try {
  328. cookie_lifetime = getCookie("ttrss_cltime");
  329. delCookie("ttrss_vf_test");
  330. setCookie("ttrss_vf_actfeed", "");
  331. updateFeedList(false, false);
  332. document.onkeydown = hotkey_handler;
  333. var viewbox = document.getElementById("viewbox");
  334. var limitbox = document.getElementById("limitbox");
  335. dropboxSelect(viewbox, getCookie("ttrss_vf_vmode"));
  336. dropboxSelect(limitbox, getCookie("ttrss_vf_limit"));
  337. daemon_enabled = getCookie("ttrss_vf_daemon");
  338. // FIXME should be callled after window resize
  339. if (navigator.userAgent.match("Opera")) {
  340. resize_feeds_frame();
  341. /* // fix headlines frame height for Opera
  342. var h = document.getElementById("headlines");
  343. var c = document.getElementById("content");
  344. var nh = document.body.scrollHeight * 0.25;
  345. h.style.height = nh + "px";
  346. c.style.height = c.scrollHeight - nh + "px"; */
  347. }
  348. debug("second stage ok");
  349. } catch (e) {
  350. exception_error("init_second_stage", e);
  351. }
  352. }
  353. function quickMenuChange() {
  354. var chooser = document.getElementById("quickMenuChooser");
  355. var opid = chooser[chooser.selectedIndex].id;
  356. chooser.selectedIndex = 0;
  357. quickMenuGo(opid);
  358. }
  359. function quickMenuGo(opid) {
  360. try {
  361. if (opid == "qmcPrefs") {
  362. gotoPreferences();
  363. }
  364. if (opid == "qmcSearch") {
  365. displayDlg("search", getActiveFeedId());
  366. return;
  367. }
  368. if (opid == "qmcAddFeed") {
  369. displayDlg("quickAddFeed");
  370. return;
  371. }
  372. if (opid == "qmcRemoveFeed") {
  373. var actid = getActiveFeedId();
  374. if (!actid) {
  375. notify("Please select some feed first.");
  376. return;
  377. }
  378. if (confirm("Remove current feed?")) {
  379. qfdDelete(actid);
  380. }
  381. return;
  382. }
  383. if (opid == "qmcUpdateFeeds") {
  384. scheduleFeedUpdate(true);
  385. return;
  386. }
  387. if (opid == "qmcCatchupAll") {
  388. catchupAllFeeds();
  389. return;
  390. }
  391. if (opid == "qmcShowOnlyUnread") {
  392. toggleDispRead();
  393. return;
  394. }
  395. if (opid == "qmcAddFilter") {
  396. displayDlg("quickAddFilter", getActiveFeedId());
  397. }
  398. } catch (e) {
  399. exception_error("quickMenuGo", e);
  400. }
  401. }
  402. function qafAdd() {
  403. if (!xmlhttp_ready(xmlhttp)) {
  404. printLockingError();
  405. return
  406. }
  407. var link = document.getElementById("qafInput");
  408. if (link.value.length == 0) {
  409. notify("Missing feed URL.");
  410. } else {
  411. notify("Adding feed...");
  412. var cat = document.getElementById("qafCat");
  413. var cat_id = "";
  414. if (cat) {
  415. cat_id = cat[cat.selectedIndex].id;
  416. } else {
  417. cat_id = 0;
  418. }
  419. var feeds_doc = window.frames["feeds-frame"].document;
  420. feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
  421. xmlhttp.open("GET", "backend.php?op=pref-feeds&quiet=1&subop=add&link=" +
  422. param_escape(link.value) + "&cid=" + param_escape(cat_id), true);
  423. xmlhttp.onreadystatechange=dlg_frefresh_callback;
  424. xmlhttp.send(null);
  425. link.value = "";
  426. }
  427. }
  428. function qaddFilter() {
  429. if (!xmlhttp_ready(xmlhttp)) {
  430. printLockingError();
  431. return
  432. }
  433. var regexp = document.getElementById("fadd_regexp");
  434. var match = document.getElementById("fadd_match");
  435. var feed = document.getElementById("fadd_feed");
  436. var action = document.getElementById("fadd_action");
  437. if (regexp.value.length == 0) {
  438. notify("Missing filter expression.");
  439. } else {
  440. notify("Adding filter...");
  441. var v_match = match[match.selectedIndex].text;
  442. var feed_id = feed[feed.selectedIndex].id;
  443. var action_id = action[action.selectedIndex].id;
  444. xmlhttp.open("GET", "backend.php?op=pref-filters&quiet=1&subop=add&regexp=" +
  445. param_escape(regexp.value) + "&match=" + v_match +
  446. "&fid=" + param_escape(feed_id) + "&aid=" + param_escape(action_id), true);
  447. xmlhttp.onreadystatechange=dlg_submit_callback;
  448. xmlhttp.send(null);
  449. regexp.value = "";
  450. }
  451. }
  452. function displayDlg(id, param) {
  453. if (!xmlhttp_ready(xmlhttp)) {
  454. printLockingError();
  455. return
  456. }
  457. notify("");
  458. xmlhttp.open("GET", "backend.php?op=dlg&id=" +
  459. param_escape(id) + "&param=" + param_escape(param), true);
  460. xmlhttp.onreadystatechange=dlg_display_callback;
  461. xmlhttp.send(null);
  462. disableHotkeys();
  463. }
  464. function closeDlg() {
  465. var dlg = document.getElementById("userDlgShadow");
  466. dlg.style.display = "none";
  467. enableHotkeys();
  468. }
  469. function qfdDelete(feed_id) {
  470. notify("Removing feed...");
  471. if (!xmlhttp_ready(xmlhttp)) {
  472. printLockingError();
  473. return
  474. }
  475. // var feeds_doc = window.frames["feeds-frame"].document;
  476. // feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
  477. _qfd_deleted_feed = feed_id;
  478. xmlhttp.open("GET", "backend.php?op=pref-feeds&quiet=1&subop=remove&ids=" + feed_id);
  479. xmlhttp.onreadystatechange=dlg_frefresh_callback;
  480. xmlhttp.send(null);
  481. }
  482. function updateFeedTitle(t) {
  483. active_title_text = t;
  484. updateTitle();
  485. }
  486. function toggleDispRead() {
  487. try {
  488. if (!xmlhttp_ready(xmlhttp)) {
  489. printLockingError();
  490. return
  491. }
  492. var hide_read_feeds = (getCookie("ttrss_vf_hreadf") == 1);
  493. hide_read_feeds = !hide_read_feeds;
  494. var query = "backend.php?op=rpc&subop=setpref" +
  495. "&key=HIDE_READ_FEEDS&value=" + param_escape(hide_read_feeds);
  496. xmlhttp.open("GET", query);
  497. xmlhttp.onreadystatechange=hide_unread_callback;
  498. xmlhttp.send(null);
  499. } catch (e) {
  500. exception_error("toggleDispRead", e);
  501. }
  502. }
  503. function debug(msg) {
  504. var c = document.getElementById('debug_output');
  505. if (c && c.style.display == "block") {
  506. while (c.lastChild != 'undefined' && c.childNodes.length > 20) {
  507. c.removeChild(c.lastChild);
  508. }
  509. var d = new Date();
  510. var ts = leading_zero(d.getHours()) + ":" + leading_zero(d.getMinutes()) +
  511. ":" + leading_zero(d.getSeconds());
  512. c.innerHTML = "<li>[" + ts + "] " + msg + "</li>" + c.innerHTML;
  513. }
  514. }
  515. function fatalError(code, message) {
  516. /* if (!params) {
  517. window.location = "error.php?c=" + param_escape(code);
  518. } else {
  519. window.location = "error.php?c=" + param_escape(code) +
  520. "&p=" + param_escape(params);
  521. } */
  522. try {
  523. var fe = document.getElementById("fatal_error");
  524. var fc = document.getElementById("fatal_error_msg");
  525. fc.innerHTML = "Code " + code + ": " + message;
  526. fe.style.display = "block";
  527. } catch (e) {
  528. exception_error("fatalError", e);
  529. }
  530. }