pocket.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. .pragma library
  2. Qt.include("keys.js")
  3. var waiting;
  4. var doneIndicator;
  5. var errorIndicator;
  6. var waitTextLabel;
  7. function setComponents(wait, waitText, done, error) {
  8. waiting = wait;
  9. waitTextLabel = waitText;
  10. doneIndicator = done;
  11. errorIndicator = error;
  12. }
  13. function addLinkToPocket(articleUrl, pocketUsername, pocketPassword, tweetID) {
  14. showWaiting(qsTr("Adding to your Pocket..."))
  15. articleUrl = articleUrl.replace("https://readitlaterlist.com/", "");
  16. var url = "https://readitlaterlist.com/v2/add?" +
  17. "apikey=" + POCKET_API_KEY +
  18. "&username=" + encodeURIComponent(pocketUsername) +
  19. "&password=" + encodeURIComponent(pocketPassword) +
  20. "&url=" + encodeURIComponent(articleUrl) +
  21. "&ref_id=" + encodeURIComponent(tweetID);
  22. console.log("Pocket API URL: " + url);
  23. var doc = new XMLHttpRequest();
  24. doc.onreadystatechange = function() {
  25. var shortURL = url;
  26. if (doc.readyState == XMLHttpRequest.HEADERS_RECEIVED) {
  27. var status = doc.status;
  28. if(status!=200) {
  29. if(shortURL.length>32) {
  30. shortURL = shortURL.substring(0,40) + "..";
  31. }
  32. showError("Pocket API returned " + status + " " + doc.statusText + " on URL " + shortURL);
  33. return;
  34. }
  35. } else if (doc.readyState == XMLHttpRequest.DONE) {
  36. var status = doc.status;
  37. if(status!=200) {
  38. console.log("HTTP status " + status);
  39. if(shortURL.length>40) {
  40. shortURL = shortURL.substring(0,40) + "..";
  41. }
  42. showError("Pocket API returned " + status + " " + doc.statusText + " on URL " + shortURL);
  43. return;
  44. }
  45. var data = doc.responseText;
  46. if(typeof(data)=="undefined") {
  47. data = "";
  48. }
  49. //console.log("data=" + data);
  50. //console.log("Callback=" + callback);
  51. showOk(data);
  52. }
  53. }
  54. doc.open("GET", url);
  55. doc.send();
  56. }
  57. function showWaiting(reason) {
  58. waitTextLabel.text = reason;
  59. waiting.state = "shown";
  60. }
  61. function showOk(data) {
  62. waiting.state = "hidden";
  63. doneIndicator.state = "shown";
  64. }
  65. function showError(data) {
  66. waiting.state = "hidden";
  67. if(typeof(data)!="undefined" && data!=null) {
  68. errorIndicator.reason = data;
  69. } else {
  70. errorIndicator.reason = "";
  71. }
  72. errorIndicator.state = "shown";
  73. console.log("ERROR: " + data);
  74. }