tweets-ui.js 4.7 KB

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