1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-05-03 14:34:08 +02:00

Automatically update relative time shown in notifications

This commit is contained in:
chylex 2021-12-24 14:23:57 +01:00
parent adb304b6a2
commit 427975e5ce
Signed by: chylex
GPG Key ID: 4DE42C8F19A80548
4 changed files with 57 additions and 0 deletions

View File

@ -12,6 +12,7 @@ import disable_clipboard_formatting_notification from "./notification/disable_cl
import expand_links_or_show_tooltip_notification from "./notification/expand_links_or_show_tooltip.js";
import handle_links from "./notification/handle_links.js";
import handle_show_this_thread_link from "./notification/handle_show_this_thread_link.js";
import recalculate_tweet_sent_time from "./notification/recalculate_tweet_sent_time.js";
import reset_scroll_position_on_load from "./notification/reset_scroll_position_on_load.js";
import scroll_smoothly from "./notification/scroll_smoothly.js";
import setup_body_hover_class from "./notification/setup_body_hover_class.js";

View File

@ -0,0 +1,54 @@
/**
* @param {number} seconds
* @returns {string}
*/
function formatRelativeTime(seconds) {
if (seconds < 0) {
return "";
}
else if (seconds < 60) {
return seconds + "s";
}
const minutes = Math.floor(seconds / 60);
if (minutes < 60) {
return minutes + "m";
}
const hours = Math.floor(minutes / 60);
if (hours < 24) {
return hours + "h";
}
const days = Math.floor(hours / 24);
return days + "d";
}
/**
* @param {HTMLElement} textElement
* @param {number} sentTime
*/
function refreshTime(textElement, sentTime) {
const seconds = Math.floor((Date.now() - sentTime) / 1000);
const newText = formatRelativeTime(seconds);
if (textElement.innerText !== newText) {
textElement.innerText = newText;
}
const refreshInMillis = seconds < 60 ? 100 : 60000;
setTimeout(() => refreshTime(textElement, sentTime), refreshInMillis);
}
/**
* Recalculates how much time has passed since a tweet was sent, so that the relative time shown in the notification stays up to date.
*/
export default function() {
const timeElement = document.querySelector("time[data-time]");
const textElement = timeElement?.querySelector("a");
if (textElement) {
const sentTime = parseInt(timeElement.getAttribute("data-time"), 10);
refreshTime(textElement, sentTime);
}
};

View File

@ -99,6 +99,7 @@ const onNewTweet = (function() {
mediaPreviewSize: "medium"
}));
html.find("time[data-time] a").text(""); // remove time since tweet was sent, since it will be recalculated anyway
html.find("footer").last().remove(); // apparently withTweetActions breaks for certain tweets, nice
html.find(".js-quote-detail").removeClass("is-actionable margin-b--8"); // prevent quoted tweets from changing the cursor and reduce bottom margin

View File

@ -399,6 +399,7 @@
<Content Include="Resources\Content\notification\expand_links_or_show_tooltip.js" />
<Content Include="Resources\Content\notification\handle_links.js" />
<Content Include="Resources\Content\notification\handle_show_this_thread_link.js" />
<Content Include="Resources\Content\notification\recalculate_tweet_sent_time.js" />
<Content Include="Resources\Content\notification\reset_scroll_position_on_load.js" />
<Content Include="Resources\Content\notification\scroll_smoothly.js" />
<Content Include="Resources\Content\notification\setup_body_hover_class.js" />