Skip to content

Instantly share code, notes, and snippets.

@samogot
Last active July 11, 2022 16:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samogot/026e8894684cd76839c786910192aabd to your computer and use it in GitHub Desktop.
Save samogot/026e8894684cd76839c786910192aabd to your computer and use it in GitHub Desktop.
Button for returning to previous scroll position after posting a message
How to use:
I asume you have a channel with lot of unread messages. You are reading it all. You want to reply one of the messages.
But if you'd send message all unread messages will be marked as read. Moreover your scrolling position will be lost. Now this problem is gone!
1. Scroll wherever you whant. This scrolling position will be remembered
2. Send a message
3. There will appear a blue line with right and left buttons.
4. If you don't want to renurn srcolling position - click right side ("Cancel")
5. If you want to renurn srcolling position - click left side ("Return to last scrolling position")
If your scrolling position was within 50 messages - you will be scrolled immediately.
Otherwise plugin will start recursively load old messages find you scrolling position
6. If you whant to stop loading - click right side ("Cancel")
Availiable locales: ru, ua, en, nl
Changelog:
1.4
- Add Dutch localization (thanks to HoLLy#2750)
//META{"name":"ReturnScroll"}*//
var ReturnScroll = function () {};
ReturnScroll.prototype.getName = function() {
return "ReturnScroll";
};
ReturnScroll.prototype.getDescription = function() {
return "Button for returning to previous scroll position after posting a message";
};
ReturnScroll.prototype.getVersion = function() {
return "1.4";
};
ReturnScroll.prototype.getAuthor = function() {
return "Samogot";
};
ReturnScroll.prototype.start = function() {
var lastTopMessage;
var doNotFindFuther;
var stopTO;
var localeStr;
switch(navigator.language) {
case 'ru':
case 'ru_RU':
localeStr = {
return: 'Вернутся к предыдущей позиции скролла',
cancel: 'Отмена',
};
break;
case 'uk':
case 'uk_UA':
localeStr = {
return: 'Повернутся до попередньої позиції скролла',
cancel: 'Відміна',
};
break;
case 'nl':
case 'nl_BE':
localeStr = {
return: 'Ga terug naar laatste positie',
cancel: 'Annuleer',
};
break;
default:
localeStr = {
return: 'Return to last scrolling position',
cancel: 'Cancel',
};
break;
}
function recursiveFindMessageForReturn() {
var messageForReturn;
$('.comment h2').each(function() {
if(lastTopMessage == $(this).text()) {
messageForReturn = this;
return false;
}
else if (doNotFindFuther && doNotFindFuther == $(this).text())
return false;
});
if(messageForReturn) {
$('.messages.scroller').scrollTop(messageForReturn.offsetTop);
$('.return-scroll-bar').remove();
stopTO = undefined;
}
else {
doNotFindFuther = $('.comment h2:eq(0)').text()
$('.has-more:first-child button').click();
stopTO = setTimeout(recursiveFindMessageForReturn, 50);
}
}
function onPostMessage() {
var returnBtn = '<div class="new-messages-bar return-scroll-bar"><button type="button" class="return">'+localeStr.return+'</button><button type="button" class="cancel">'+localeStr.cancel+'</button></div>';
$('.messages-wrapper').prepend(returnBtn);
$('.return-scroll-bar .return').click(function() {
doNotFindFuther = undefined;
recursiveFindMessageForReturn();
});
$('.return-scroll-bar .cancel').click(function() {
$('.return-scroll-bar').remove();
if(stopTO) {
clearTimeout(stopTO);
stopTO = undefined;
}
});
}
$(document).on('keypress.ret-scrl', '.channel-textarea textarea', function(e){
var $messages_scroller = $('.messages.scroller');
if($messages_scroller.scrollTop() < $messages_scroller[0].scrollHeight - $messages_scroller.height()) {
if(e.which == 13) {
$('.comment h2').each(function() {
if(this.offsetTop > $messages_scroller.scrollTop()) {
lastTopMessage = $(this).text();
return false;
}
});
onPostMessage();
}
}
});
};
ReturnScroll.prototype.load = function() {};
ReturnScroll.prototype.unload = function() {
$(document).off("keypress.ret-scrl");
};
ReturnScroll.prototype.stop = function() {
$(document).off("keypress.ret-scrl");
};
ReturnScroll.prototype.getSettingsPanel = function() {
return null;
};
ReturnScroll.prototype.onMessage = function() {
};
ReturnScroll.prototype.onSwitch = function() {
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment