Skip to content

Commit

Permalink
tweet pinning
Browse files Browse the repository at this point in the history
  • Loading branch information
dimdenGD committed Jul 19, 2022
1 parent d0643b2 commit b5bcce6
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
4 changes: 2 additions & 2 deletions layouts/home/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,7 @@ async function renderTrends() {
// On scroll to end of timeline, load more tweets
let loadingNewTweets = false;
document.addEventListener('scroll', async () => {
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight - 1000) {
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight - 500) {
if (loadingNewTweets || timeline.data.length === 0) return;
loadingNewTweets = true;
let tl;
Expand Down Expand Up @@ -1029,7 +1029,7 @@ document.addEventListener('scroll', async () => {
setTimeout(() => {
loadingNewTweets = false;
});
}, 200);
}, 250);
}
});

Expand Down
38 changes: 38 additions & 0 deletions layouts/profile/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,7 @@ async function appendTweet(t, timelineContainer, options = {}) {
<hr>
<span class="tweet-interact-more-menu-analytics">Tweet analytics</span><br>
<span class="tweet-interact-more-menu-delete">Delete tweet</span><br>
<span class="tweet-interact-more-menu-pin">${pinnedTweet && pinnedTweet.id_str === t.id_str ? 'Unpin tweet' : 'Pin tweet'}</span>
` : ``}
<hr>
<span class="tweet-interact-more-menu-refresh">Refresh tweet data</span><br>
Expand Down Expand Up @@ -1003,6 +1004,7 @@ async function appendTweet(t, timelineContainer, options = {}) {
const tweetInteractMoreMenuDownload = tweet.getElementsByClassName('tweet-interact-more-menu-download')[0];
const tweetInteractMoreMenuDownloadGif = tweet.getElementsByClassName('tweet-interact-more-menu-download-gif')[0];
const tweetInteractMoreMenuDelete = tweet.getElementsByClassName('tweet-interact-more-menu-delete')[0];
const tweetInteractMoreMenuPin = tweet.getElementsByClassName('tweet-interact-more-menu-pin')[0];

// Translate
if(tweetTranslate) tweetTranslate.addEventListener('click', async () => {
Expand Down Expand Up @@ -1398,6 +1400,42 @@ async function appendTweet(t, timelineContainer, options = {}) {
tweet.remove();
});
});
tweetInteractMoreMenuPin.addEventListener('click', async () => {
if(pinnedTweet && pinnedTweet.id_str === t.id_str) {
await API.unpinTweet(t.id_str);
pinnedTweet = null;
tweet.remove();
let tweetTime = new Date(t.created_at).getTime();
let beforeTweet = Array.from(document.getElementsByClassName('tweet')).find(i => {
let timestamp = +i.getElementsByClassName('tweet-time')[0].dataset.timestamp;
return timestamp < tweetTime;
});
if(beforeTweet) {
appendTweet(t, timelineContainer, { after: beforeTweet });
}
return;
} else {
await API.pinTweet(t.id_str);
pinnedTweet = t;
let pinnedTweetElement = Array.from(document.getElementsByClassName('tweet')).find(i => {
let topText = i.getElementsByClassName('tweet-top-text')[0];
return (topText && topText.innerText === 'Pinned Tweet');
});
if(pinnedTweetElement) {
pinnedTweetElement.remove();
}
tweet.remove();
appendTweet(t, timelineContainer, {
prepend: true,
top: {
text: 'Pinned Tweet',
icon: "\uf003",
color: "var(--link-color)"
}
});
return;
}
});
}
tweetInteractMoreMenuRefresh.addEventListener('click', async () => {
let tweetData;
Expand Down
3 changes: 3 additions & 0 deletions layouts/profile/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,9 @@ body {
.tweet-interact-more-menu-follow:before {
content: "\f056"
}
.tweet-interact-more-menu-pin:before {
content: "\f003"
}

.tweet-media-element-censor {
filter: blur(20px);
Expand Down
38 changes: 38 additions & 0 deletions scripts/apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -2079,4 +2079,42 @@ API.deleteConversation = id => {
reject(e);
});
});
}
API.unpinTweet = id => {
return new Promise((resolve, reject) => {
fetch(`https://twitter.com/i/api/1.1/account/unpin_tweet.json`, {
headers: {
"authorization": OLDTWITTER_CONFIG.public_token,
"x-csrf-token": OLDTWITTER_CONFIG.csrf,
"x-twitter-auth-type": "OAuth2Session",
"content-type": "application/x-www-form-urlencoded; charset=UTF-8"
},
credentials: "include",
method: 'post',
body: `id=${id}`
}).then(i => i.text()).then(data => {
resolve(true);
}).catch(e => {
reject(e);
});
});
}
API.pinTweet = id => {
return new Promise((resolve, reject) => {
fetch(`https://twitter.com/i/api/1.1/account/pin_tweet.json`, {
headers: {
"authorization": OLDTWITTER_CONFIG.public_token,
"x-csrf-token": OLDTWITTER_CONFIG.csrf,
"x-twitter-auth-type": "OAuth2Session",
"content-type": "application/x-www-form-urlencoded; charset=UTF-8"
},
credentials: "include",
method: 'post',
body: `id=${id}`
}).then(i => i.text()).then(data => {
resolve(true);
}).catch(e => {
reject(e);
});
});
}

0 comments on commit b5bcce6

Please sign in to comment.