// TODO:
// * handle opening the feed at a specific post id in time
// * custom colors
// * author profiles, bios, and showing only their messages instead of the full site
// * show site/profile info on click of navbar for mobile
// * in-app search of site content
// * homepage with history and sponsored sources
// * don't show redundant day markers
// * navigate markdown Wordpress export pages from Git
// * app info in page without JS
let MbState = {};
//
function CanScrollEl(el, scrollAxis) {
if (0 === el[scrollAxis]) {
el[scrollAxis] = 1;
if (1 === el[scrollAxis]) {
el[scrollAxis] = 0;
return true;
}
} else {
return true;
}
return false;
}
function IsScrollableY(el) {
return (el.scrollHeight > el.clientHeight) && CanScrollEl(el, 'scrollTop') && ('hidden' !== getComputedStyle(el).overflowY);
}
function GetDomainFromUrl (url) {
return url.split('//')[1].split('/')[0];
}
function MakeSiteRestUrl (path='') {
const siteUrl = MbState.siteUrl;
if (GetDomainFromUrl(siteUrl).toLowerCase() === 'octospacc.altervista.org') {
return `${siteUrl}/wp-content/uploads/${siteUrl.split('.').slice(0, 1)[0].split('//')[1]}/scripts/stuff.php?&Thing=SiteWpJsonCors&AccessToken=9ab6e20c&$Query=${path}`;
} else {
// TODO: universal cors bypass
return `${siteUrl}/wp-json/${path}`;
}
}
async function MbViewerInit () {
if (!location.hash) {
location.hash = '/';
}
MbState = {
siteData: {
name: "MBViewer",
description: "",
},
};
$('form.tgme_header_search_form')[0].action = '';
$('form.tgme_header_search_form')[0].onsubmit = function(event){
location.hash = `/siteUrl=${event.target.querySelector('input').value}`;
event.preventDefault();
};
$('a.tgme_header_link')[0].href = '';
$('.tgme_channel_info_header_username').html('');
$('.tgme_page_photo_image').html('');
$('.tgme_page_photo_image').removeClass('bgcolor0');
$('.tgme_page_photo_image').attr('data-content', '');
$('section.tgme_channel_history.js-message_history').html('');
for (const arg of location.hash.split('/').slice(1).join('/').split('|')) {
const argTokens = arg.split('=');
MbState[argTokens[0]] = argTokens.slice(1).join('=');
}
if (MbState.siteUrl) {
const siteRequest = await fetch(MakeSiteRestUrl());
MbState.siteData = await siteRequest.json();
$('form.tgme_header_search_form')[0].action = `${MbState.siteUrl}/?s`;
$('form.tgme_header_search_form')[0].onsubmit = null;
$('a.tgme_header_link')[0].href = MbState.siteUrl;
$('.tgme_channel_info_header_username').html(`${GetDomainFromUrl(MbState.siteUrl).toLowerCase()}`);
$('.tgme_page_photo_image').html(``);
} else {
$('.tgme_page_photo_image').addClass('bgcolor0');
$('.tgme_page_photo_image').attr('data-content', 'MBV');
$('section.tgme_channel_history.js-message_history').append(MakeMbHtml({ content: `
MBViewer WIP Version, 2024-01-13.
Uses code borrowed from t.me, specially modified to handle custom MB-style data visualization.
This webapp is not affiliated with Telegram, and all rights upon the original materials belong to the original owners.
` }));
}
$('.tgme_header_title, .tgme_channel_info_header_title').html(MbState.siteData.name);
$('.tgme_channel_info_description').html(MbState.siteData.description);
//const postsRequest = await fetch(MakeSiteRestUrl('wp/v2/posts/?&offset=0&per_page=1'));
//const postsData = await postsRequest.json();
// // TODO: since this request already gives us the content of the latest post, maybe we can cache it somewhere to avoid wasting too much traffic?
//$('section.tgme_channel_history.js-message_history').append(MakeMoreWrapperHtml(postsData[0].id, 'before'));
$('section.tgme_channel_history.js-message_history').append(MakeMoreWrapperHtml(0, 'before'));
TWeb.loadMore($('.js-messages_more_wrap > a'), true);
// TODO: scroll to the very bottom of the document somehow after showing a first message, to have a less jarring visual effect?
}
//function MakeMoreWrapperHtml (postId, wrapType) {
// return ``
//}
function MakeMoreWrapperHtml (postOffset, wrapType) {
MbState.lastPostOffset = (postOffset + 1);
return ``
}
function MakeMbHtml (postData) {
postData = (typeof(postData) === 'string' ? JSON.parse(postData) : postData);
postData = (postData[0] || postData);
return `
${MbState.siteUrl ? MakeMoreWrapperHtml(MbState.lastPostOffset, 'before') : ''}
`;
}
window.addEventListener('hashchange', MbViewerInit);