problem is fixed:
I uptated nodejs.
I have a script for previewing topics from a recent or unread topic page.
As of version 1.18.3 of NodeBB, the script does not work.
Can you help?
This is the script:
(function() {
'use strict';
function onhover() {
/* globals require, $, socket, app */
var self = $(this);
if (!self.data('preview-loaded')) {
socket.emit('topics.loadMore', {
tid: self.data('my-tid'),
after: 0,
count: 1,
direction: 1
}, (e, d) => {
self.data('preview-loaded', 1);
require('translator').translate(e ? '<span style="color:red;">error: </span>' + e.message : d.mainPost.content,
(d) => {
if (app.user.uid === 0) {
d = d.replace(/\|\|.*\|\|/g,'<a href="https://domain.com/login">You must be logged in to view this link.</a>');
} else {
d = d.replace(/\|\|/g,'');
}
$('#preview-' + self.data('my-tid')).children().html(d.replace(/\|\|/g,''));
});
}
);
}
$('#preview-' + self.data('my-tid')).stop(true).delay(500).fadeIn();
}
function onunhover() {
$('#preview-' + $(this).data('my-tid')).stop(true).fadeOut();
}
$(window).on('action:topics.loaded', (event, data) => {
for (let topic of data.topics) {
let topicElem = $('[data-tid="' + topic.tid + '"]');
createPreview(topicElem);
}
});
function addTopicTools() {
let topicelems = $('[component="category/topic"]');
topicelems.each((i, elem) => {
createPreview($(elem));
});
}
function createPreview(topicElem) {
let tid = parseInt(topicElem.attr('data-tid'));
if (!$('#preview-' + tid).length) {
topicElem.find('.content').append('<div class="post-preview" id="preview-' + tid + '"><div class="wrap-post-preview">טוען...</div></div>');
topicElem.find('.post-preview').hover(function(){$(this).stop(true).fadeIn();},function(){$(this).delay(600).fadeOut();});
topicElem.find('[itemprop="url"]').data('my-tid', tid).hover(
onhover,
onunhover
);
}
}
$(window).on('action:ajaxify.end', addTopicTools);
})();
(The script also exchanges for guests each link to a login page, to encourage registration)
@baris
I was told the problem is in:
topicElem.find
I do not understand this, I just want the code to work again ...
Thank you!
What is your nodebb git hash?
On latest versions topics.loadMore
doesn't return mainPost
anymore.
If you are running latest master update to get these changes https://github.com/NodeBB/NodeBB/commit/906dc5675e5e1e96a7ff693f07f20987cd08c87a.
Then the main post will be the first item in the posts array when you call topics.loadMore
. Then you can use d.posts[0].content
instead of d.mainPost.content