Skip to content

Commit

Permalink
fix unneccessary loads on user timeline end
Browse files Browse the repository at this point in the history
  • Loading branch information
dimdenGD committed Jul 19, 2022
1 parent 2e11fb0 commit e7e5747
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
8 changes: 7 additions & 1 deletion layouts/profile/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ let timeline = {
let settings = {};
let seenThreads = [];
let pinnedTweet, followersYouFollow;
let previousLastTweet, stopLoad = false;
let favoritesCursor, followingCursor, followersCursor, followersYouKnowCursor;
let vars;
chrome.storage.sync.get(['linkColor', 'font', 'heartsNotStars', 'linkColorsInTL', 'enableTwemoji'], data => {
Expand All @@ -20,6 +21,7 @@ let subpage;
let user_handle = location.pathname.slice(1).split("?")[0].split('#')[0];
user_handle = user_handle.split('/')[0];
function updateSubpage() {
previousLastTweet = undefined; stopLoad = false;
user_handle = location.pathname.slice(1).split("?")[0].split('#')[0];
if(user_handle.split('/').length === 1) {
subpage = 'profile';
Expand Down Expand Up @@ -248,6 +250,7 @@ async function updateTimeline() {
// first update
timeline.data = tl;
renderTimeline();
previousLastTweet = timeline.data[timeline.data.length - 1];
}

async function renderFollowing(clear = true, cursor) {
Expand Down Expand Up @@ -1633,7 +1636,7 @@ window.addEventListener('scroll', async () => {
banner.style.top = `${5+Math.min(window.scrollY/4, 470/4)}px`;
// load more tweets
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight - 1000) {
if (loadingNewTweets || timeline.data.length === 0) return;
if (loadingNewTweets || timeline.data.length === 0 || stopLoad) return;
loadingNewTweets = true;
let tl;
try {
Expand All @@ -1643,6 +1646,7 @@ window.addEventListener('scroll', async () => {
favoritesCursor = data.cursor;
} else {
tl = await API.getUserTweets(pageUser.id_str, timeline.data[timeline.data.length - 1].id_str, subpage !== 'profile');
tl = tl.slice(1);
if(subpage === 'media') {
tl = tl.filter(t => t.extended_entities && t.extended_entities.media && t.extended_entities.media.length > 0 && !t.retweeted_status);
}
Expand All @@ -1653,6 +1657,8 @@ window.addEventListener('scroll', async () => {
return;
}
timeline.data = timeline.data.concat(tl);
if(previousLastTweet && previousLastTweet.id_str === timeline.data[timeline.data.length - 1].id_str) return stopLoad = true;
previousLastTweet = timeline.data[timeline.data.length - 1];
let lastTweet = document.getElementById('timeline').lastChild;
await renderTimeline();
setTimeout(() => {
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Old Twitter Layout (2022)",
"description": "A new extension that returns old Twitter's look.",
"version": "1.0.3",
"version": "1.0.4",
"manifest_version": 3,
"homepage_url": "https://github.com/dimdenGD/OldTwitter",
"background": {
Expand Down

0 comments on commit e7e5747

Please sign in to comment.