Skip to content

Commit

Permalink
added bio translator
Browse files Browse the repository at this point in the history
  • Loading branch information
dimdenGD committed Aug 22, 2022
1 parent b322991 commit bdeb9d2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
26 changes: 25 additions & 1 deletion layouts/profile/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ async function renderFollowersYouFollow(clear = true, cursor) {

let months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
let everAddedAdditional = false;
function renderProfile() {
async function renderProfile() {
document.getElementById('profile-banner').src = pageUser.profile_banner_url ? pageUser.profile_banner_url : 'https://abs.twimg.com/images/themes/theme1/bg.png';
let attempts = 0;
document.getElementById('profile-avatar').addEventListener('error', () => {
Expand Down Expand Up @@ -478,6 +478,30 @@ function renderProfile() {
updateSelection();

document.getElementById('profile-bio').innerHTML = escapeHTML(pageUser.description).replace(/\n/g, '<br>').replace(/((http|https|ftp):\/\/[\w?=&.\/-;#~%-]+(?![\w\s?&.\/;#~%"=-]*>))/g, '<a href="$1">$1</a>').replace(/(?<!\w)@([\w+]{1,15}\b)/g, `<a href="https://twitter.com/$1">@$1</a>`).replace(/(?<!\w)#([\w+]+\b)/g, `<a href="https://twitter.com/hashtag/$1">#$1</a>`);
let textWithoutLinks = pageUser.description.replace(/(?:https?|ftp):\/\/[\n\S]+/g, '').replace(/(?<!\w)@([\w+]{1,15}\b)/g, '');
let isEnglish = textWithoutLinks.length < 1 ? {languages:[{language:'en', percentage:100}]} : await chrome.i18n.detectLanguage(textWithoutLinks);
isEnglish = isEnglish.languages[0] && isEnglish.languages[0].percentage > 60 && isEnglish.languages[0].language.startsWith('en');
let at = false;
if(!isEnglish) {
let translateBtn = document.createElement('span');
translateBtn.className = "translate-bio";
translateBtn.addEventListener('click', async () => {
if(at) return;
let translated = await API.translateProfile(pageUser.id_str);
at = true;
let span = document.createElement('span');
span.innerHTML = `
<br>
<span class='piu-a'>Translated from [${translated.localizedSourceLanguage}]</span>
<span>${escapeHTML(translated.translation)}</span>
`;
document.getElementById('profile-bio').append(span);
if(vars.enableTwemoji) twemoji.parse(span);
});
translateBtn.innerText = "Translate bio";
document.getElementById('profile-bio').append(document.createElement('br'), translateBtn);
}

if(vars.enableTwemoji) twemoji.parse(document.getElementById('profile-info'));

document.getElementById('profile-stat-tweets-value').innerText = Number(pageUser.statuses_count).toLocaleString().replace(/\s/g, ',');
Expand Down
14 changes: 14 additions & 0 deletions layouts/profile/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,20 @@ a:hover,
color: var(--light-gray);
text-decoration: none
}
.translate-bio {
font-size: 12px;
color: var(--light-gray);
}
.translate-bio:hover {
text-decoration: underline;
cursor: pointer;
}
.translate-bio:before {
content: "\f089";
font: 12px RosettaIcons;
margin-right: 5px;
vertical-align: text-bottom;
}

#profile-info-text {
margin-top: 10px;
Expand Down
31 changes: 29 additions & 2 deletions scripts/apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -1214,11 +1214,12 @@ API.getUnreadCount = (cache = true) => {
}
API.translateTweet = id => {
return new Promise((resolve, reject) => {
fetch(`https://api.twitter.com/1.1/translations/show.json?id=${id}&dest=en&use_display_text=true&cards_platform=Web-13&include_entities=1&include_user_entities=1&include_cards=1&send_error_codes=1&tweet_mode=extended&include_ext_alt_text=true&include_reply_count=true`, {
fetch(`https://twitter.com/i/api/1.1/strato/column/None/tweetId=${id},destinationLanguage=None,translationSource=Some(Google),feature=None,timeout=None,onlyCached=None/translation/service/translateTweet`, {
headers: {
"authorization": OLDTWITTER_CONFIG.oauth_key,
"x-csrf-token": OLDTWITTER_CONFIG.csrf,
"x-twitter-auth-type": "OAuth2Session",
"x-twitter-client-language": navigator.language ? navigator.language.split('-')[0] : "en"
},
credentials: "include"
}).then(i => i.json()).then(data => {
Expand All @@ -1228,7 +1229,33 @@ API.translateTweet = id => {
if (data.errors && data.errors[0]) {
return reject(data.errors[0].message);
}
resolve(data);
resolve({
translated_lang: data.localizedSourceLanguage,
text: data.translation
});
}).catch(e => {
reject(e);
});
});
}
API.translateProfile = id => {
return new Promise((resolve, reject) => {
fetch(`https://twitter.com/i/api/1.1/strato/column/None/profileUserId=${id},destinationLanguage=None,translationSource=Some(Google)/translation/service/translateProfile`, {
headers: {
"authorization": OLDTWITTER_CONFIG.oauth_key,
"x-csrf-token": OLDTWITTER_CONFIG.csrf,
"x-twitter-auth-type": "OAuth2Session",
"x-twitter-client-language": navigator.language ? navigator.language.split('-')[0] : "en"
},
credentials: "include"
}).then(i => i.json()).then(data => {
if (data.errors && data.errors[0].code === 32) {
return reject("Not logged in");
}
if (data.errors && data.errors[0]) {
return reject(data.errors[0].message);
}
resolve(data.profileTranslation);
}).catch(e => {
reject(e);
});
Expand Down

0 comments on commit bdeb9d2

Please sign in to comment.