Skip to content

Commit

Permalink
fixed weird bug with tl never loading
Browse files Browse the repository at this point in the history
  • Loading branch information
dimdenGD committed Sep 23, 2022
1 parent b8b61d1 commit 693f888
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 100 deletions.
1 change: 1 addition & 0 deletions layouts/home/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ <h1>Trends</h1>
</div>
<div id="new-tweets" class="center-text" hidden></div>
<div id="timeline"></div>
<div id="load-more" class="center-text">Load more</div>
</div>
<div class="cell" id="right-cell">
<div id="wtf" class="box">
Expand Down
228 changes: 133 additions & 95 deletions layouts/home/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,101 +237,6 @@ function renderNewTweetsButton() {
}
}

// On scroll to end of timeline, load more tweets
let loadingNewTweets = false;
let lastTweetDate = 0;
let activeTweet;
let tweetsToLoad = {};
let lastScroll = Date.now();
document.addEventListener('scroll', async () => {
lastScroll = Date.now();
// find active tweet by scroll amount
if(Date.now() - lastTweetDate > 50) {
lastTweetDate = Date.now();
let tweets = Array.from(document.getElementsByClassName('tweet'));

if(activeTweet) {
activeTweet.classList.remove('tweet-active');
}
let scrollPoint = scrollY + innerHeight/2;
activeTweet = tweets.find(t => scrollPoint > t.offsetTop && scrollPoint < t.offsetTop + t.offsetHeight);
if(activeTweet) {
activeTweet.classList.add('tweet-active');
}
}

// loading new tweets
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight - 1000) {
if (loadingNewTweets || timeline.data.length === 0) return;
loadingNewTweets = true;
let tl;
try {
tl = vars.timelineType === 'algo' ? await API.getAlgoTimeline(algoCursor, 50) : await API.getTimeline(timeline.data[timeline.data.length - 1].id_str);
if(vars.timelineType === 'algo') {
algoCursor = tl.cursor;
tl = tl.list.filter(t => !seenTweets.includes(t.id_str));
for(let t of tl) {
seenTweets.push(t.id_str);
}
} else {
tl = tl.slice(1);
}
} catch (e) {
console.error(e);
loadingNewTweets = false;
return;
}
let originalLength = timeline.data.length;
timeline.data = timeline.data.concat(tl);
try {
await renderTimeline(true, originalLength);
} catch(e) {
loadingNewTweets = false;
}
setTimeout(() => {
loadingNewTweets = false;
}, 250);
}
}, { passive: true });
document.addEventListener('mousemove', e => {
if(Date.now() - lastScroll > 10) {
let t = e.target;
if(t.className.includes('tweet ') || t.className === 'tweet-interact' || t.className === 'tweet-body' || t.className === 'tweet-media') {
if(t.className.includes('tweet-view')) return;
if(t.className === 'tweet-interact' || t.className === 'tweet-media') t = t.parentElement.parentElement;
else if(t.className === 'tweet-body') t = t.parentElement;
let id;
try { id = t.className.split('id-')[1].split(' ')[0] } catch(e) { return };
if(!tweetsToLoad[id]) tweetsToLoad[id] = 1;
else tweetsToLoad[id]++;
if(tweetsToLoad[id] === 15) {
API.getReplies(id);
API.getTweetLikers(id);
t.classList.add('tweet-preload');
console.log(`Preloading ${id}`);
}
}
}
});

document.addEventListener('clearActiveTweet', () => {
if(activeTweet) {
activeTweet.classList.remove('tweet-active');
}
activeTweet = undefined;
});
document.addEventListener('findActiveTweet', () => {
let tweets = Array.from(document.getElementsByClassName('tweet'));
if(activeTweet) {
activeTweet.classList.remove('tweet-active');
}
let scrollPoint = scrollY + innerHeight/2;
activeTweet = tweets.find(t => scrollPoint > t.offsetTop && scrollPoint < t.offsetTop + t.offsetHeight);
if(activeTweet) {
activeTweet.classList.add('tweet-active');
}
});

setTimeout(async () => {
vars = await new Promise(resolve => {
chrome.storage.sync.get(['linkColor', 'font', 'heartsNotStars', 'linkColorsInTL', 'enableTwemoji',
Expand Down Expand Up @@ -497,13 +402,146 @@ setTimeout(async () => {
// weird bug
location.reload();
}
// On scroll to end of timeline, load more tweets
let loadingNewTweets = false;
let lastTweetDate = 0;
let activeTweet;
let tweetsToLoad = {};
let lastScroll = Date.now();
document.addEventListener('scroll', async () => {
lastScroll = Date.now();
// find active tweet by scroll amount
if(Date.now() - lastTweetDate > 50) {
lastTweetDate = Date.now();
let tweets = Array.from(document.getElementsByClassName('tweet'));

if(activeTweet) {
activeTweet.classList.remove('tweet-active');
}
let scrollPoint = scrollY + innerHeight/2;
activeTweet = tweets.find(t => scrollPoint > t.offsetTop && scrollPoint < t.offsetTop + t.offsetHeight);
if(activeTweet) {
activeTweet.classList.add('tweet-active');
}
}

// loading new tweets
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight - 1000) {
if (loadingNewTweets || timeline.data.length === 0) return;
loadingNewTweets = true;
document.getElementById('load-more').innerText = "Loading...";
let tl;
try {
tl = vars.timelineType === 'algo' ? await API.getAlgoTimeline(algoCursor, 50) : await API.getTimeline(timeline.data[timeline.data.length - 1].id_str);
if(vars.timelineType === 'algo') {
algoCursor = tl.cursor;
tl = tl.list.filter(t => !seenTweets.includes(t.id_str));
for(let t of tl) {
seenTweets.push(t.id_str);
}
} else {
tl = tl.slice(1);
}
} catch (e) {
console.error(e);
document.getElementById('load-more').innerText = "Load more";
loadingNewTweets = false;
return;
}
let originalLength = timeline.data.length;
timeline.data = timeline.data.concat(tl);
try {
await renderTimeline(true, originalLength);
} catch(e) {
document.getElementById('load-more').innerText = "Load more";
loadingNewTweets = false;
}
setTimeout(() => {
document.getElementById('load-more').innerText = "Load more";
loadingNewTweets = false;
}, 250);
}
}, { passive: true });
document.addEventListener('mousemove', e => {
if(Date.now() - lastScroll > 10) {
let t = e.target;
if(t.className.includes('tweet ') || t.className === 'tweet-interact' || t.className === 'tweet-body' || t.className === 'tweet-media') {
if(t.className.includes('tweet-view')) return;
if(t.className === 'tweet-interact' || t.className === 'tweet-media') t = t.parentElement.parentElement;
else if(t.className === 'tweet-body') t = t.parentElement;
let id;
try { id = t.className.split('id-')[1].split(' ')[0] } catch(e) { return };
if(!tweetsToLoad[id]) tweetsToLoad[id] = 1;
else tweetsToLoad[id]++;
if(tweetsToLoad[id] === 15) {
API.getReplies(id);
API.getTweetLikers(id);
t.classList.add('tweet-preload');
console.log(`Preloading ${id}`);
}
}
}
});

document.addEventListener('clearActiveTweet', () => {
if(activeTweet) {
activeTweet.classList.remove('tweet-active');
}
activeTweet = undefined;
});
document.addEventListener('findActiveTweet', () => {
let tweets = Array.from(document.getElementsByClassName('tweet'));
if(activeTweet) {
activeTweet.classList.remove('tweet-active');
}
let scrollPoint = scrollY + innerHeight/2;
activeTweet = tweets.find(t => scrollPoint > t.offsetTop && scrollPoint < t.offsetTop + t.offsetHeight);
if(activeTweet) {
activeTweet.classList.add('tweet-active');
}
});
document.getElementById('new-tweets').addEventListener('click', () => {
timeline.toBeUpdated = 0;
timeline.data = timeline.dataToUpdate;
timeline.dataToUpdate = [];
renderNewTweetsButton();
renderTimeline();
});
document.getElementById('load-more').addEventListener('click', async () => {
if (loadingNewTweets || timeline.data.length === 0) return;
loadingNewTweets = true;
document.getElementById('load-more').innerText = "Loading...";
let tl;
try {
tl = vars.timelineType === 'algo' ? await API.getAlgoTimeline(algoCursor, 50) : await API.getTimeline(timeline.data[timeline.data.length - 1].id_str);
if(vars.timelineType === 'algo') {
algoCursor = tl.cursor;
tl = tl.list.filter(t => !seenTweets.includes(t.id_str));
for(let t of tl) {
seenTweets.push(t.id_str);
}
} else {
tl = tl.slice(1);
}
} catch (e) {
console.error(e);
document.getElementById('load-more').innerText = "Load more";
loadingNewTweets = false;
return;
}
let originalLength = timeline.data.length;
timeline.data = timeline.data.concat(tl);
try {
await renderTimeline(true, originalLength);
} catch(e) {
document.getElementById('load-more').innerText = "Load more";
loadingNewTweets = false;
}
setTimeout(() => {
document.getElementById('load-more').innerText = "Load more";
loadingNewTweets = false;
}, 250);
});
document.getElementById('wtf-refresh').addEventListener('click', async () => {
renderDiscovery(false);
});
Expand Down
10 changes: 7 additions & 3 deletions layouts/settings/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,14 @@ setTimeout(async () => {
colorPreviewDark.style.color = `${previewColor}`;
});
sync.addEventListener('click', async () => {
sync.disabled = true;
let color = profileLinkColor.value;
if(color.startsWith('#')) color = color.slice(1);
let tweet = await API.postTweet({
status: `link_color=${color}`
})
let tweet;
try {
tweet = await API.postTweet({
status: `link_color=${color}`
})
let res = await fetch(`https://dimden.dev/services/twitter_link_colors/set/${tweet.id_str}`, {
method: 'POST',
headers: {
Expand All @@ -220,8 +222,10 @@ setTimeout(async () => {
alert('Link color set!');
}
} catch(e) {
console.error(e);
alert('Error setting link color');
} finally {
sync.disabled = false;
API.deleteTweet(tweet.id_str);
}
});
Expand Down
6 changes: 5 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,11 @@
"https://twitter.com/search?*",
"https://twitter.com/search",
"https://twitter.com/search/",
"https://twitter.com/search/?*"
"https://twitter.com/search/?*",
"https://twitter.com/tos",
"https://twitter.com/privacy",
"https://twitter.com/*/tos",
"https://twitter.com/*/privacy"
],
"js": ["libraries/twemoji.min.js", "layouts/profile/script.js", "libraries/gif.js", "libraries/viewer.min.js"],
"css": ["libraries/viewer.min.css"],
Expand Down
4 changes: 3 additions & 1 deletion scripts/newtwitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ setInterval(() => {
realPath === '/register' ||
realPath === '/logout' ||
realPath === '/messages' ||
realPath === '/account/switch' ||
realPath.endsWith('/tos') ||
realPath.endsWith('/privacy') ||
realPath.startsWith('/account/') ||
realPath.endsWith('/lists') ||
realPath.endsWith('/topics') ||
realPath.startsWith('/settings/')
Expand Down

0 comments on commit 693f888

Please sign in to comment.