tweets-ui.js 4.2 KB

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