2020-12-25 01:59:12 +00:00
|
|
|
// I'm not a React developer. Be advised!
|
|
|
|
// If you are, and want to rewrite this the "right way", go ahead and do it :)
|
|
|
|
// This is plain Javascript and only requires basic calls to implement customizations
|
2020-12-25 00:42:30 +00:00
|
|
|
|
|
|
|
|
2020-12-25 01:59:12 +00:00
|
|
|
// Do simple XHR requests to the API on Mastodon
|
|
|
|
// make sure you control errors correctly, as a Mastodon Server might not setup CORS correctly
|
2020-12-25 14:21:04 +00:00
|
|
|
function mastodon_get(path, payload, callbk) { return mastodon_request('GET', path, payload, callbk); }
|
|
|
|
function mastodon_post(path, payload, callbk) { return mastodon_request('POST', path, payload, callbk); }
|
|
|
|
function mastodon_request(method, path, payload, callbk) {
|
2020-12-25 01:59:12 +00:00
|
|
|
payload = payload || null;
|
2020-12-27 18:12:03 +00:00
|
|
|
var url = 'https://'+fediloveApi.getCurrentInstance()+path;
|
2020-12-25 01:59:12 +00:00
|
|
|
|
|
|
|
var oReq = new XMLHttpRequest();
|
|
|
|
oReq.addEventListener("load", function() { callbk(this.responseText); });
|
2020-12-25 14:21:04 +00:00
|
|
|
oReq.open(method, url);
|
2020-12-27 18:12:03 +00:00
|
|
|
oReq.setRequestHeader('Authorization', 'Bearer '+fediloveApi.getAccessToken());
|
2020-12-25 01:59:12 +00:00
|
|
|
oReq.send(payload);
|
|
|
|
}
|
|
|
|
|
2020-12-25 14:21:04 +00:00
|
|
|
function api_status_fav(dom, status_id) {
|
|
|
|
var dislike = false;
|
|
|
|
var path = `/api/v1/statuses/${status_id}/favourite`;
|
|
|
|
if (dom.getAttribute('data-liked') === "1") {
|
|
|
|
path = `/api/v1/statuses/${status_id}/unfavourite`;
|
|
|
|
dislike = true;
|
|
|
|
}
|
|
|
|
mastodon_post(path, {}, function(data) {
|
|
|
|
// to-do: check "data" if the POST succeded (for now we expect it did xD)
|
|
|
|
if (!dislike) {
|
|
|
|
$(dom).addClass('liked-msg');
|
|
|
|
$(dom).attr('data-liked', 1);
|
|
|
|
dom.style.animation = "spin2 .5s linear 1";
|
|
|
|
} else {
|
|
|
|
$(dom).removeClass('liked-msg');
|
|
|
|
$(dom).attr('aria-label', '0 '+$(dom).attr('aria-label'));
|
|
|
|
$(dom).removeAttr('data-liked');
|
|
|
|
dom.style.animation = undefined;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-25 17:08:21 +00:00
|
|
|
// We use the already polished compose system to trick it to send messages using our custom UI
|
|
|
|
// instead of the reply compose box show in every message when you hit "reply"
|
|
|
|
function api_send_message(dom) {
|
|
|
|
|
|
|
|
// the status_id is the last part of our current URL
|
2020-12-27 18:12:03 +00:00
|
|
|
const status_id = fediloveApi.getChatStatusId();
|
2020-12-28 15:05:31 +00:00
|
|
|
const msgText = $('div#chat-compose-global textarea').val();
|
|
|
|
if (msgText.trim() === '') {
|
|
|
|
return false;
|
|
|
|
}
|
2020-12-25 17:08:21 +00:00
|
|
|
|
|
|
|
// search for the reply on status_id, dictated by URL
|
|
|
|
$('button[id*=reply-]').each(function(i) {
|
|
|
|
if ($(this).attr('id').includes('//'+status_id))
|
|
|
|
{
|
|
|
|
// click the reply button on the current status, dictated by URL
|
|
|
|
if ($('#the-compose-box-input-'+status_id).length == 0) {
|
|
|
|
$(this).click();
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for the button event to make the compose layer "visible"
|
|
|
|
var _this = setInterval(function()
|
|
|
|
{
|
|
|
|
if ($('#the-compose-box-input-'+status_id).length > 0) {
|
|
|
|
// search for the user ID in the title attribute
|
|
|
|
$('a[id*=status-author-name-]').each(function(i2) {
|
|
|
|
if ($(this).attr('id').includes('//'+status_id))
|
|
|
|
{
|
|
|
|
// title contains the user ID on this server
|
2020-12-28 15:05:31 +00:00
|
|
|
var text = $(this).attr('title')+' '+msgText;
|
2020-12-25 17:08:21 +00:00
|
|
|
|
|
|
|
// set the text on thie invisible compose box of the status_id
|
|
|
|
$('textarea#the-compose-box-input-'+status_id).val(text);
|
|
|
|
// hit send :)
|
|
|
|
$('div#list-item-'+status_id+' div.status-article-compose-box > div.compose-box-button-wrapper button.compose-box-button').click();
|
|
|
|
// empty our message box
|
|
|
|
$('div#chat-compose-global textarea').val('');
|
2020-12-27 18:12:03 +00:00
|
|
|
fediloveUI.scrollChatToLastItem();
|
2020-12-25 17:08:21 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
clearInterval(_this);
|
|
|
|
}
|
|
|
|
}, 100);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-27 18:12:03 +00:00
|
|
|
var fediloveUI = {
|
|
|
|
scrollChatToLastItem: function() {
|
2020-12-28 11:26:04 +00:00
|
|
|
const startLen = $('div.the-list > div').length;
|
|
|
|
var count = 0;
|
|
|
|
var _this = setInterval(function() {
|
|
|
|
count++;
|
|
|
|
var newLen = $('div.the-list > div').length;
|
|
|
|
if (count >= 100 || newLen != startLen) {
|
|
|
|
document.querySelector('div.the-list > div:last-child').scrollIntoView();
|
|
|
|
clearInterval(_this);
|
|
|
|
}
|
|
|
|
}, 150);
|
2020-12-25 17:53:20 +00:00
|
|
|
}
|
2020-12-27 18:12:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// objects to access from React code
|
|
|
|
var fediloveApi = {
|
|
|
|
getAccessToken: function() {
|
|
|
|
var instance = fediloveApi.getCurrentInstance();
|
|
|
|
return JSON.parse(localStorage.store_loggedInInstances)[instance].access_token;
|
|
|
|
},
|
|
|
|
getCurrentInstance: function() {
|
|
|
|
return JSON.parse(localStorage.store_currentInstance);
|
|
|
|
},
|
|
|
|
getChatStatusId: function() {
|
|
|
|
var parts = window.location.pathname.split('/');
|
|
|
|
return parts[parts.length-1];
|
|
|
|
}
|
|
|
|
};
|
2020-12-28 14:01:15 +00:00
|
|
|
var fediloveReact = {};
|
2020-12-27 18:12:03 +00:00
|
|
|
var fediloveFunctions = {};
|
|
|
|
var fediloveData = {
|
2020-12-28 14:01:15 +00:00
|
|
|
currentChat: null
|
2020-12-27 18:12:03 +00:00
|
|
|
};
|
|
|
|
var fediloveEvents = {
|
2020-12-28 14:01:15 +00:00
|
|
|
onEmojiPicked: function(emoji) {
|
|
|
|
var $txt = $("#chat-compose-global textarea");
|
|
|
|
var caretPos = $txt[0].selectionStart;
|
|
|
|
var textAreaTxt = $txt.val();
|
|
|
|
$txt.val(textAreaTxt.substring(0, caretPos) + emoji + textAreaTxt.substring(caretPos));
|
2020-12-28 11:26:04 +00:00
|
|
|
},
|
2020-12-27 18:12:03 +00:00
|
|
|
onNewNotification: function (dataItems) {
|
2020-12-28 11:26:04 +00:00
|
|
|
if (window.location.pathname.startsWith('/statuses/')) {
|
|
|
|
if (fediloveFunctions.updateStatusAndThread !== undefined) {
|
|
|
|
fediloveFunctions.updateStatusAndThread( fediloveApi.getCurrentInstance(), fediloveApi.getAccessToken(),
|
|
|
|
window.location.pathname, fediloveApi.getChatStatusId() );
|
|
|
|
fediloveUI.scrollChatToLastItem();
|
|
|
|
}
|
2020-12-27 18:12:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-25 14:21:04 +00:00
|
|
|
|
2020-12-25 01:59:12 +00:00
|
|
|
// this is our URL-based customizations made by JavaScript
|
|
|
|
var intervalChatCssChange = null;
|
2020-12-25 00:42:30 +00:00
|
|
|
function fedilove_customization() {
|
2020-12-28 22:07:37 +00:00
|
|
|
document.body.classList = '';
|
2020-12-25 00:42:30 +00:00
|
|
|
document.querySelector('.main-content').classList = "main-content";
|
|
|
|
document.querySelector('#chat-compose-global').style = 'visibility: collapse';
|
2020-12-28 22:07:37 +00:00
|
|
|
document.querySelector('#chat-party-hide').style = 'display: none !important';
|
|
|
|
document.querySelector('nav#main-nav > ul.main-nav-ul').style = '';
|
2020-12-25 00:42:30 +00:00
|
|
|
|
2020-12-25 01:59:12 +00:00
|
|
|
if (window.location.pathname.startsWith('/statuses/'))
|
|
|
|
{
|
2020-12-25 14:21:04 +00:00
|
|
|
$('div.main-content').addClass('chat');
|
2020-12-28 22:07:37 +00:00
|
|
|
$('body').addClass('chat');
|
2020-12-25 00:42:30 +00:00
|
|
|
document.querySelector('#chat-compose-global').style = '';
|
2020-12-28 22:07:37 +00:00
|
|
|
document.querySelector('#chat-party-hide').style = '';
|
|
|
|
document.querySelector('nav#main-nav > ul.main-nav-ul').style = 'display: none !important';
|
2020-12-25 01:59:12 +00:00
|
|
|
|
2020-12-25 14:21:04 +00:00
|
|
|
// add some animations
|
|
|
|
var style = document.createElement('style');
|
|
|
|
style.innerHTML = '@keyframes spin2 { 100% { transform:rotate(-360deg); } }';
|
|
|
|
document.body.appendChild(style);
|
|
|
|
|
2020-12-25 01:59:12 +00:00
|
|
|
// this function changes the css class on articles (messages)
|
|
|
|
// that match the given account_id
|
2020-12-25 14:21:04 +00:00
|
|
|
var makeMessageUIModifications = function(account_id) {
|
|
|
|
if (intervalChatCssChange !== null) return;
|
|
|
|
var theint = 150;
|
|
|
|
setInterval(function()
|
|
|
|
{
|
2020-12-25 01:59:12 +00:00
|
|
|
intervalChatCssChange = this;
|
2020-12-25 14:21:04 +00:00
|
|
|
|
|
|
|
// paint MY messages as mine
|
|
|
|
$('div.main-content.chat article.status-article').each(function(i) {
|
|
|
|
if ($(this).find('a.status-author-name').attr('href').endsWith(`/accounts/${account_id}`)) {
|
|
|
|
$(this).addClass('mymsg');
|
|
|
|
theint = 250;
|
2020-12-28 22:07:37 +00:00
|
|
|
} else {
|
|
|
|
$(this).addClass('partymsg');
|
2020-12-25 01:59:12 +00:00
|
|
|
}
|
2020-12-25 14:21:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// paint LIKES
|
|
|
|
$('a.status-favs-reblogs.status-favs').each(function(i) {
|
|
|
|
// easy, aria-label contains the times this status was fav
|
|
|
|
// so, we use this as the input source to search for "0",
|
|
|
|
// if no "0" found in text, we mark element as liked so we can apply styles and "undo like" function
|
|
|
|
if ($(this).attr('aria-label').search(/0/) == -1) {
|
|
|
|
$(this).addClass('liked-msg');
|
|
|
|
$(this).attr('data-liked', 1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-12-28 22:07:37 +00:00
|
|
|
// remove liking functionality from our own messages
|
|
|
|
$('div.the-list article.status-article.mymsg a.status-favs-reblogs').each(function(i) {
|
|
|
|
$(this).attr('onclick', 'return false;');
|
|
|
|
});
|
|
|
|
|
2020-12-25 14:21:04 +00:00
|
|
|
}, theint);
|
2020-12-25 01:59:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// get the userAccountId to check against the href /account/NUM on the messages
|
|
|
|
// so we can apply different style to my messages
|
|
|
|
if (localStorage.store_userAccountId === undefined) {
|
2020-12-25 14:21:04 +00:00
|
|
|
mastodon_get('/api/v1/accounts/verify_credentials', {}, function(data) {
|
2020-12-25 01:59:12 +00:00
|
|
|
var json = JSON.parse(data);
|
|
|
|
if (json !== undefined) {
|
|
|
|
localStorage.store_userAccountId = json.id;
|
2020-12-25 14:21:04 +00:00
|
|
|
makeMessageUIModifications(json.id);
|
2020-12-25 01:59:12 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2020-12-25 14:21:04 +00:00
|
|
|
makeMessageUIModifications(localStorage.store_userAccountId);
|
2020-12-25 01:59:12 +00:00
|
|
|
}
|
2020-12-25 00:42:30 +00:00
|
|
|
}
|
2020-12-28 14:01:15 +00:00
|
|
|
else if (window.location.pathname == '/direct') {
|
|
|
|
$('div.main-content').addClass('direct');
|
|
|
|
}
|
2020-12-27 18:12:03 +00:00
|
|
|
else if (window.location.pathname.startsWith('/notifications/mentions')) {
|
|
|
|
$('nav.notification-filters li > a.focus-fix').attr('onclick', 'return false;');
|
|
|
|
}
|
2020-12-25 00:42:30 +00:00
|
|
|
}
|
|
|
|
|
2020-12-25 01:59:12 +00:00
|
|
|
// we inject this script.js into the React framework at timelines.js
|
|
|
|
// Watch for URL changes every 1/2 seconds (this is efficient, don't worry about it!)
|
|
|
|
// and dispatch a call to "fedilove_customization" to load page customizations depending on URL context
|
2020-12-25 00:42:30 +00:00
|
|
|
var theurl = null;
|
|
|
|
setInterval(function() {
|
|
|
|
var newurl = window.location.pathname;
|
|
|
|
if (newurl != theurl) {
|
|
|
|
fedilove_customization();
|
|
|
|
theurl = newurl;
|
|
|
|
}
|
2020-12-25 14:21:04 +00:00
|
|
|
}, 250);
|
2020-12-25 00:42:30 +00:00
|
|
|
|