Skip to content

Commit

Permalink
fixed never loading tweets
Browse files Browse the repository at this point in the history
  • Loading branch information
dimdenGD committed Aug 22, 2022
1 parent e6c0734 commit b322991
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 15 deletions.
22 changes: 21 additions & 1 deletion layouts/tweet/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,7 @@ async function appendTweet(t, timelineContainer, options = {}) {
</div>
</div>
`;
let footerFavorites = tweet.getElementsByClassName('tweet-footer-favorites')[0];
if(t.card) {
generateCard(t, tweet, user);
}
Expand All @@ -852,7 +853,6 @@ async function appendTweet(t, timelineContainer, options = {}) {
tweet.querySelector('.tweet-top').append(icon, span);
}
if(options.mainTweet) {
let footerFavorites = tweet.getElementsByClassName('tweet-footer-favorites')[0];
let likers = mainTweetLikers.slice(0, 8);
for(let i in likers) {
let liker = likers[i];
Expand All @@ -864,6 +864,7 @@ async function appendTweet(t, timelineContainer, options = {}) {
likerImg.title = liker.name + ' (@' + liker.screen_name + ')';
likerImg.width = 24;
likerImg.height = 24;
a.dataset.id = liker.id_str;
a.appendChild(likerImg);
footerFavorites.appendChild(a);
}
Expand Down Expand Up @@ -1392,6 +1393,11 @@ async function appendTweet(t, timelineContainer, options = {}) {
tweetInteractFavorite.dataset.val = parseInt(tweetInteractFavorite.innerText) - 1;
tweetInteractFavorite.innerText = parseInt(tweetInteractFavorite.innerText) - 1;
} else {
if(mainTweetLikers.find(liker => liker.id_str === user.id_str)) {
mainTweetLikers.splice(mainTweetLikers.findIndex(liker => liker.id_str === user.id_str), 1);
let likerImg = footerFavorites.querySelector(`a[data-id="${user.id_str}"]`);
if(likerImg) likerImg.remove()
}
tweetFooterFavorites.innerText = Number(parseInt(tweetFooterFavorites.innerText.replace(/\s/g, '').replace(/,/g, '').replace(/\./g, '')) - 1).toLocaleString().replace(/\s/g, ',');
}
tweetInteractFavorite.classList.remove('tweet-interact-favorited');
Expand All @@ -1405,6 +1411,20 @@ async function appendTweet(t, timelineContainer, options = {}) {
tweetInteractFavorite.dataset.val = parseInt(tweetInteractFavorite.innerText) + 1;
tweetInteractFavorite.innerText = parseInt(tweetInteractFavorite.innerText) + 1;
} else {
if(footerFavorites.children.length < 8 && !mainTweetLikers.find(liker => liker.id_str === user.id_str)) {
let a = document.createElement('a');
a.href = `https://twitter.com/${user.screen_name}`;
let likerImg = document.createElement('img');
likerImg.src = user.profile_image_url_https;
likerImg.classList.add('tweet-footer-favorites-img');
likerImg.title = user.name + ' (@' + user.screen_name + ')';
likerImg.width = 24;
likerImg.height = 24;
a.dataset.id = user.id_str;
a.appendChild(likerImg);
footerFavorites.appendChild(a);
mainTweetLikers.push(user);
}
tweetFooterFavorites.innerText = Number(parseInt(tweetFooterFavorites.innerText.replace(/\s/g, '').replace(/,/g, '').replace(/\./g, '')) + 1).toLocaleString().replace(/\s/g, ',');
}
tweetInteractFavorite.classList.add('tweet-interact-favorited');
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.4.9",
"version": "1.5.0",
"manifest_version": 3,
"homepage_url": "https://github.com/dimdenGD/OldTwitter",
"background": {
Expand Down
30 changes: 18 additions & 12 deletions scripts/apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -1861,8 +1861,6 @@ API.getReplies = (id, cursor) => {
if(d.tweetReplies[id] && Date.now() - d.tweetReplies[id].date < 60000) {
return resolve(d.tweetReplies[id].data);
}
}
if(!cursor) {
if(loadingReplies[id]) {
return loadingReplies[id].listeners.push([resolve, reject]);
} else {
Expand Down Expand Up @@ -2024,10 +2022,13 @@ API.getTweetLikers = (id, cursor) => {
chrome.storage.local.get(['tweetLikers'], d => {
if(!cursor) cursor = '';
if(!d.tweetLikers) d.tweetLikers = {};
if(d.tweetLikers[id+cursor] && Date.now() - d.tweetLikers[id+cursor].date < 60000) {
return resolve(d.tweetLikers[id+cursor].data);
}
if(!cursor) {
if(d.tweetLikers[id]) {
console.log(d.tweetLikers[id], Date.now() - d.tweetLikers[id].date < 60000);
}
if(d.tweetLikers[id] && Date.now() - d.tweetLikers[id].date < 60000) {
return resolve(d.tweetLikers[id].data);
}
if(loadingLikers[id]) {
return loadingLikers[id].listeners.push([resolve, reject]);
} else {
Expand Down Expand Up @@ -2068,7 +2069,13 @@ API.getTweetLikers = (id, cursor) => {
return reject(data.errors[0].message);
}
let list = data.data.favoriters_timeline.timeline.instructions.find(i => i.type === 'TimelineAddEntries');
if(!list) return resolve({ list: [], cursor: undefined });
if(!list) {
if(!cursor) {
loadingLikers[id].listeners.forEach(l => l[0]({ list: [], cursor: undefined }));
delete loadingLikers[id];
}
return resolve({ list: [], cursor: undefined });
}
list = list.entries;
let rdata = {
list: list.filter(e => e.entryId.startsWith('user-')).map(e => {
Expand All @@ -2083,13 +2090,12 @@ API.getTweetLikers = (id, cursor) => {
if(!cursor) {
loadingLikers[id].listeners.forEach(l => l[0](rdata));
delete loadingLikers[id];
d.tweetLikers[id] = {
date: Date.now(),
data: rdata
};
chrome.storage.local.set({tweetLikers: d.tweetLikers}, () => {});
}
if(!rdata.cursor) rdata.cursor = '';
d.tweetLikers[id+cursor] = {
date: Date.now(),
data: rdata
};
chrome.storage.local.set({tweetLikers: d.tweetLikers}, () => {});
}).catch(e => {
if(!cursor) {
loadingLikers[id].listeners.forEach(l => l[1](e));
Expand Down
22 changes: 21 additions & 1 deletion scripts/tweetviewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,7 @@ class TweetViewer {
</div>
</div>
`;
let footerFavorites = tweet.getElementsByClassName('tweet-footer-favorites')[0];
if(t.card) {
generateCard(t, tweet, user);
}
Expand All @@ -836,7 +837,6 @@ class TweetViewer {
tweet.querySelector('.tweet-top').append(icon, span);
}
if(options.mainTweet) {
let footerFavorites = tweet.getElementsByClassName('tweet-footer-favorites')[0];
let likers = this.mainTweetLikers.slice(0, 8);
for(let i in likers) {
let liker = likers[i];
Expand All @@ -849,6 +849,7 @@ class TweetViewer {
likerImg.width = 24;
likerImg.height = 24;
a.appendChild(likerImg);
a.dataset.id = liker.id_str;
footerFavorites.appendChild(a);
}
let likesLink = tweet.getElementsByClassName('tweet-footer-stat-f')[0];
Expand Down Expand Up @@ -1361,6 +1362,11 @@ class TweetViewer {
tweetInteractFavorite.dataset.val = parseInt(tweetInteractFavorite.innerText) - 1;
tweetInteractFavorite.innerText = parseInt(tweetInteractFavorite.innerText) - 1;
} else {
if(this.mainTweetLikers.find(liker => liker.id_str === user.id_str)) {
this.mainTweetLikers.splice(this.mainTweetLikers.findIndex(liker => liker.id_str === user.id_str), 1);
let likerImg = footerFavorites.querySelector(`a[data-id="${user.id_str}"]`);
if(likerImg) likerImg.remove()
}
tweetFooterFavorites.innerText = Number(parseInt(tweetFooterFavorites.innerText.replace(/\s/g, '').replace(/,/g, '').replace(/\./g, '')) - 1).toLocaleString().replace(/\s/g, ',');
}
tweetInteractFavorite.classList.remove('tweet-interact-favorited');
Expand All @@ -1374,6 +1380,20 @@ class TweetViewer {
tweetInteractFavorite.dataset.val = parseInt(tweetInteractFavorite.innerText) + 1;
tweetInteractFavorite.innerText = parseInt(tweetInteractFavorite.innerText) + 1;
} else {
if(footerFavorites.children.length < 8 && !this.mainTweetLikers.find(liker => liker.id_str === user.id_str)) {
let a = document.createElement('a');
a.href = `https://twitter.com/${user.screen_name}`;
let likerImg = document.createElement('img');
likerImg.src = user.profile_image_url_https;
likerImg.classList.add('tweet-footer-favorites-img');
likerImg.title = user.name + ' (@' + user.screen_name + ')';
likerImg.width = 24;
likerImg.height = 24;
a.dataset.id = user.id_str;
a.appendChild(likerImg);
footerFavorites.appendChild(a);
this.mainTweetLikers.push(user);
}
tweetFooterFavorites.innerText = Number(parseInt(tweetFooterFavorites.innerText.replace(/\s/g, '').replace(/,/g, '').replace(/\./g, '')) + 1).toLocaleString().replace(/\s/g, ',');
}
tweetInteractFavorite.classList.add('tweet-interact-favorited');
Expand Down

0 comments on commit b322991

Please sign in to comment.