tweets-ui.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. function noteCardFormat (tweet, annotation) {
  2. if (!tweet) { return ""; }
  3. if (!annotation) { annotation = ''; } else { annotation = '\n' + annotation + '\n'; }
  4. var now = new Date();
  5. var s = `---
  6. ${formatTime(now)}
  7. ${annotation}
  8. ${extendedFormat(tweet)}
  9. `;
  10. return s;
  11. }
  12. function simpleFormat (tweet) {
  13. if (!tweet) { return ""; }
  14. var s;
  15. with (tweet) {
  16. if( !tweet['source_url'] ) { source_url = null; }
  17. s =
  18. `${source_url || ''}
  19. ${display_name}
  20. @${handle}
  21. ${created_at}
  22. ${text || '(no text)'}
  23. `;
  24. }
  25. return s.trim();
  26. }
  27. function htmlToText(html) {
  28. var temp = document.createElement('div');
  29. temp.innerHTML = html;
  30. return temp.textContent; // Or return temp.innerText if you need to return only visible text. It's slower.
  31. }
  32. function extendedFormat (tweet) {
  33. if (!tweet) { return ""; }
  34. var s;
  35. if (!tweet.text && tweet.html) {
  36. // FIXME: HTML to text
  37. tweet.text = htmlToText(tweet.html.replaceAll('</p>', '</p>\n\n'));
  38. }
  39. with (tweet) {
  40. var total_rt_count = (public_metrics.quote_count || 0) + (public_metrics.retweet_count || 0);
  41. s =
  42. `${source_url || ''}
  43. ${display_name}
  44. @${handle}
  45. ${created_at}
  46. ${!isNaN(public_metrics.reply_count) ? public_metrics.reply_count : 'unknown'} replies, ${total_rt_count} rts, ${public_metrics.like_count} likes
  47. ${tweet['retweeted_by'] ? 'Retweeted by ' : tweet['retweeted_by']}
  48. ${text || '(no text)'}
  49. ${tweet['card'] ? "Card:\n" + cardFormat(card) : ''}
  50. ${tweet['quoted_tweet'] ? "Quoted Tweet:\n" + simpleFormat(quoted_tweet) : ''}
  51. `;
  52. }
  53. return s.trim();
  54. }
  55. function cardFormat (card) {
  56. if (!card) { return ""; }
  57. var s;
  58. with (card) {
  59. s =
  60. `${source_url || ''}
  61. ${display_url}
  62. ${title}
  63. ${content}
  64. `;
  65. }
  66. return s;
  67. }
  68. function jsonFormat (tweet) {
  69. return JSON.stringify(tweet, null, 2);
  70. }
  71. function formatDate (date) {
  72. return date.getFullYear()
  73. + "-" + (date.getMonth() + 1).toString().padStart(2, '0')
  74. + "-" + date.getDate().toString().padStart(2, '0');
  75. }
  76. function formatTime (date) {
  77. return ("" + date.getHours()).padStart(2, "0")
  78. + ":" + ("" + date.getMinutes()).padStart(2, "0")
  79. + ":" + ("" + date.getSeconds()).padStart(2, "0")
  80. }
  81. function copyToClipboard (text) {
  82. navigator.clipboard.writeText(text);
  83. }
  84. function tweetById (tweetId) {
  85. var tweets = window.dataset.items.filter(t => t.id == tweetId);
  86. if (!tweets.length) {
  87. return null;
  88. }
  89. return tweets[0];
  90. }
  91. function copyTweetToClipboard (tweetId) {
  92. var text = noteCardFormat(tweetById(tweetId));
  93. return copyToClipboard(text)
  94. }
  95. async function swipeTweetToNotesApp (tweetId) {
  96. const { value: annotation } = await Swal.fire({
  97. input: 'textarea',
  98. inputLabel: 'Add annotation to swipe?',
  99. inputPlaceholder: 'Type your annotation here...',
  100. inputAttributes: {
  101. 'aria-label': 'Type your annotation here'
  102. },
  103. showCancelButton: true,
  104. cancelButtonText: 'No annotation'
  105. })
  106. var noteId = formatDate(new Date()) + '-swipe.md'
  107. var url = notesAppUrl + '/intent/prepend-text/' + noteId
  108. var text = noteCardFormat(tweetById(tweetId), annotation);
  109. var params = new URLSearchParams({
  110. text: text,
  111. should_create: 1
  112. });
  113. return fetch(url + '?' + params).then(function (r) {
  114. Toast.fire({
  115. icon: 'success',
  116. title: 'Tweet swiped.'
  117. });
  118. return r;
  119. });
  120. }