Check only last page
3 posters
Page 1 of 2
Page 1 of 2 • 1, 2
Check only last page
Is there a way to look for last post in the thread with jQuery even I am on the first page? In my case, Let's assume I am on any page but last, could I gather info from truly last post in that thread which can be 10 pages ahead? And if it is possible, how can I get access to the info of last post?
Last edited by The Raven on August 9th 2023, 11:05 am; edited 1 time in total
Guest- Guest
Re: Check only last page
Good evening!
Exactly what information about the last post should be retrieved?
After receiving this information, what should be done with it next?
As I understand, the uploading of information should start after going to the topic. Is it correct?
Exactly what information about the last post should be retrieved?
After receiving this information, what should be done with it next?
As I understand, the uploading of information should start after going to the topic. Is it correct?
Razor12345- Support Moderator
- Posts : 1586
Reputation : 268
Language : Ukr, Rus, Eng
Location : Ukraine
Re: Check only last page
Razor12345 wrote:Good evening!
Exactly what information about the last post should be retrieved?
After receiving this information, what should be done with it next?
As I understand, the uploading of information should start after going to the topic. Is it correct?
So, I made a script (almost done) to show user a message if the difference between today's date and last post date are more than 180 days.
But, I realized this won't work if I gather info of last post date on the first page because actual last post in that topic is what matters, no matter where I am.
Guest- Guest
Re: Check only last page
Send the code you already have.
I'll try to look at this issue tomorrow, but I think the problem might be the date.
I need to see what format your code uses for the date.
I'll try to look at this issue tomorrow, but I think the problem might be the date.
I need to see what format your code uses for the date.
Razor12345- Support Moderator
- Posts : 1586
Reputation : 268
Language : Ukr, Rus, Eng
Location : Ukraine
Re: Check only last page
Razor12345 wrote:Send the code you already have.
I'll try to look at this issue tomorrow, but I think the problem might be the date.
I need to see what format your code uses for the date.
The date is not an issue. I am reffering to locating last post in the thread itself. Let's say thread has 10 pages, but i visited page 3. I want the date of the last post. How to obtain it?
Guest- Guest
Re: Check only last page
I think there will be a problem with the date format - either it's a regular string, a "new Date" constructor object, or whatever.
There are a number of other issues related to writing the code. I don't want to rewrite the code several times just because you didn't want to send your code.
I'm not asking you to submit code just for curiosity's sake.
There are a number of other issues related to writing the code. I don't want to rewrite the code several times just because you didn't want to send your code.
I'm not asking you to submit code just for curiosity's sake.
Razor12345- Support Moderator
- Posts : 1586
Reputation : 268
Language : Ukr, Rus, Eng
Location : Ukraine
Re: Check only last page
I think the only way to do this is match the title from the list of threads in that specific category and get the link from the last post column there. That would consider a lot of work though especially if there are a lot of categories and posts.
Forum of the Forums Forumotion Rules | Tips & Tricks | FAQ | Did you forget your password? |
*** The Support Forum will never ask you for your email or password, so please do not post them anywhere! ***
No support via PM!
Re: Check only last page
Razor12345 wrote:I think there will be a problem with the date format - either it's a regular string, a "new Date" constructor object, or whatever.
There are a number of other issues related to writing the code. I don't want to rewrite the code several times just because you didn't want to send your code.
I'm not asking you to submit code just for curiosity's sake.
I sent you the code in PM because I don't want to share with others, it's unique for my forum.
Guest- Guest
Re: Check only last page
- Code:
$(function() {
let page = 0;
let page2 = 5;
let lastPage = false;
let address = location.pathname;
let lastPost;
let indexOfPage = address.indexOf('-');
let isLoad = true;
let result;
fetchPageData(page);
function fetchPageData(currentPage) {
if (isLoad) {
isLoad = false;
let modifiedAddress;
if (page === 0) {
modifiedAddress = address;
} else if (address.substring(0, indexOfPage).indexOf('p') === -1) {
modifiedAddress = address.substring(0, indexOfPage) + 'p' + currentPage + address.substring(indexOfPage);
} else {
let pageIndex = address.substring(0, indexOfPage).indexOf('p');
modifiedAddress = address.slice(0, pageIndex) + 'p' + currentPage + address.slice(indexOfPage);
}
$.ajax({
url: modifiedAddress,
method: "GET",
data: 'topiclist',
dataType: "html",
success: function(data) {
handleData(data, currentPage);
},
error: function(xhr, status, error) {
console.log("AJAX request error: " + error);
}
});
}
}
function handleData(data, currentPage) {
let tempDiv = document.createElement('div');
tempDiv.innerHTML = data;
let mainContentElement = tempDiv.querySelector('#main-content');
let find_elements = Array.from(mainContentElement.querySelectorAll('.author'));
if (find_elements.length === 0) {
lastPage = true;
let difference = new Date(result) - new Date();
console.log(Math.floor(difference / (1000 * 60 * 60 * 24)));
} else {
lastPost = find_elements.at(-1);
let text = getTextFromAuthorElement(lastPost);
result = formatText(text);
isLoad = true;
}
if (!lastPage) {
page = page + page2;
fetchPageData(page);
}
}
function getTextFromAuthorElement(element) {
let text = '';
let childNodes = element.childNodes;
for (let i = 0; i < childNodes.length; i++) {
let node = childNodes[i];
if (node.nodeType === Node.TEXT_NODE) {
text += node.nodeValue.trim();
}
}
return text;
}
function formatText(text) {
let timeIndex = text.lastIndexOf('-');
let time = text.length - timeIndex;
let strippedText = text.substring(2).slice(0, -time).trim();
return strippedText;
}
});
|
Find this code:
- Code:
if (find_elements.length === 0) {
lastPage = true;
let difference = new Date() - new Date(result);
console.log(Math.floor(difference / (1000 * 60 * 60 * 24)));
|
This is where you remove console.log and add your own logic for displaying the warning. For example:
- Code:
if (Math.floor(difference / (1000 * 60 * 60 * 24)) < 180) {
...
} else {
...
}
Result: (on screenshot last post in topic)
Razor12345- Support Moderator
- Posts : 1586
Reputation : 268
Language : Ukr, Rus, Eng
Location : Ukraine
Re: Check only last page
I fix the code.
I did not take into that the words "Today" and "Yesterday" can be used instead of date.
I've added an additional check at this point.
I did not take into that the words "Today" and "Yesterday" can be used instead of date.
I've added an additional check at this point.
- Code:
function formatText(text) {
let timeIndex = text.lastIndexOf('at');
let time = text.length - timeIndex;
let strippedText = text.substring(2).slice(0, -time).trim();
if (strippedText === 'Yesterday') {
let yesterday = new Date();
let today = new Date();
return yesterday.setDate(today.getDate() - 1);
} else if (strippedText === 'Today') {
return new Date();
} else {
return strippedText;
}
}
Razor12345- Support Moderator
- Posts : 1586
Reputation : 268
Language : Ukr, Rus, Eng
Location : Ukraine
Re: Check only last page
Changed the date check to this one:
Check the result
- Code:
function formatText(text) {
let timeIndex = text.lastIndexOf('at');
let strippedText;
let time;
if (timeIndex < 0) {
timeIndex = text.lastIndexOf('-');
time = text.length - timeIndex;
strippedText = text.substring(2).slice(0, -time).trim();
return strippedText;
} else {
time = text.length - timeIndex;
strippedText = text.substring(2).slice(0, -time).trim();
if (strippedText === 'Yesterday') {
let yesterday = new Date();
let today = new Date();
return yesterday.setDate(today.getDate() - 1);
} else {
return new Date();
}
}
}
Check the result
Razor12345- Support Moderator
- Posts : 1586
Reputation : 268
Language : Ukr, Rus, Eng
Location : Ukraine
TonnyKamper likes this post
Razor12345- Support Moderator
- Posts : 1586
Reputation : 268
Language : Ukr, Rus, Eng
Location : Ukraine
Razor12345- Support Moderator
- Posts : 1586
Reputation : 268
Language : Ukr, Rus, Eng
Location : Ukraine
Re: Check only last page
The code was written for the forum listed in your profile - https://gothicpub.forumotion.com
Version: phpbb3
https://slavunion.forumotion.me - this forum has an Invision version.
Version: phpbb3
https://slavunion.forumotion.me - this forum has an Invision version.
Razor12345- Support Moderator
- Posts : 1586
Reputation : 268
Language : Ukr, Rus, Eng
Location : Ukraine
Re: Check only last page
Razor12345 wrote:The code was written for the forum listed in your profile - https://gothicpub.forumotion.com
Version: phpbb3
https://slavunion.forumotion.me - this forum has an Invision version.
Hmm, in PM i sent you the link of the forum to test out with. It's the slav forum. But you looked for the one in my description (my main one). Invision is a test forum for me.
Guest- Guest
Re: Check only last page
I've looked at classes - the code is identical for Invision.
On the Invision forum, I have swapped the parameters in this line:
You can check result.
On the Invision forum, I have swapped the parameters in this line:
- Code:
let difference = new Date() - new Date(result);
You can check result.
Razor12345- Support Moderator
- Posts : 1586
Reputation : 268
Language : Ukr, Rus, Eng
Location : Ukraine
Re: Check only last page
The console shows the difference between today and the day of the last post in the thread.
Since the last post was written today, the difference is 0.
Since the last post was written today, the difference is 0.
Razor12345 wrote:- the difference between today's date and the date of the most recent post in the topic. Calculated in days.
- Code:
Math.floor(difference / (1000 * 60 * 60 * 24))
This is where you remove console.log and add your own logic for displaying the warning. For example:
- Code:
if (Math.floor(difference / (1000 * 60 * 60 * 24)) < 180) {
...
} else {
...
}
Razor12345- Support Moderator
- Posts : 1586
Reputation : 268
Language : Ukr, Rus, Eng
Location : Ukraine
Re: Check only last page
Okay, this works now like a charm. Thank you for your efforts. You literaly wrote the whole script from nothing. You are the man!
One more thing, will this also work on PhpBB3?
One more thing, will this also work on PhpBB3?
Guest- Guest
Re: Check only last page
On this forum https://gothicpub.forumotion.com I created a Test file and there is already working code from this topic in there.
I initially thought it was about this forum.
I initially thought it was about this forum.
Razor12345- Support Moderator
- Posts : 1586
Reputation : 268
Language : Ukr, Rus, Eng
Location : Ukraine
poesia-verses likes this post
Re: Check only last page
Ye, all good now. Just one more question. When page loads, the box appears and it takes a bit less than a second to vanish which is bad. Text should only appear once the script realize topic is that old.
I tried to put div display to none and then in script to revert it to display but all of the css applied in css file is gone. as if there was never any css applied.
I tried to put div display to none and then in script to revert it to display but all of the css applied in css file is gone. as if there was never any css applied.
Guest- Guest
Re: Check only last page
Did you try it as
also?
|
Forum of the Forums Forumotion Rules | Tips & Tricks | FAQ | Did you forget your password? |
*** The Support Forum will never ask you for your email or password, so please do not post them anywhere! ***
No support via PM!
Re: Check only last page
It's better not to use inline styles - especially with the !important rule.
Set a class for the block. Write
to this class in CSS.
Then, when the script works, write the style through JS -
Set a class for the block. Write
|
Then, when the script works, write the style through JS -
|
Razor12345- Support Moderator
- Posts : 1586
Reputation : 268
Language : Ukr, Rus, Eng
Location : Ukraine
Re: Check only last page
Ok, so the css now looks like this:
And the script looks like this:
I edited to be under 30 days to display message for testing purposes. However, it doesn't show anything.
- Code:
#oldTopic {
background-color:#fff;
display:none;
width:100%;
height:80px;
margin-bottom:20px;
border:1px solid #156FC5;
}
#oldTopicStatus {
display:inline-block;
width:200px;
line-height:80px;
color:#fff;
font-size:16px;
text-align:center;
background-color:#156FC5;
margin-right: 20px;
}
#oldTopicMsg {
font-size:13px;
line-height:80px;
}
And the script looks like this:
- Code:
$(function() {
let page = 0;
let page2 = 25;
let lastPage = false;
let address = location.pathname;
let lastPost;
let indexOfPage = address.indexOf('-');
let isLoad = true;
let result;
var username = _userdata.username;
fetchPageData(page);
function fetchPageData(currentPage) {
if (isLoad) {
isLoad = false;
let modifiedAddress;
if (page === 0) {
modifiedAddress = address;
} else if (address.substring(0, indexOfPage).indexOf('p') === -1) {
modifiedAddress = address.substring(0, indexOfPage) + 'p' + currentPage + address.substring(indexOfPage);
} else {
let pageIndex = address.substring(0, indexOfPage).indexOf('p');
modifiedAddress = address.slice(0, pageIndex) + 'p' + currentPage + address.slice(indexOfPage);
}
$.ajax({
url: modifiedAddress,
method: "GET",
data: 'topiclist',
dataType: "html",
success: function(data) {
handleData(data, currentPage);
},
error: function(xhr, status, error) {
console.log("AJAX request error: " + error);
}
});
}
}
function handleData(data, currentPage) {
let tempDiv = document.createElement('div');
tempDiv.innerHTML = data;
let mainContentElement = tempDiv.querySelector('#main-content');
let find_elements = Array.from(mainContentElement.querySelectorAll('.author'));
if (find_elements.length === 0) {
lastPage = true;
let difference = new Date() - new Date(result);
if (Math.floor(difference / (1000 * 60 * 60 * 24)) < 30) {
$("#oldTopicMsg").text($("#oldTopicMsg").text().replace("[username]", username));
$("#oldTopic").style.disply = 'block';
} else {
//$("#oldTopic").css("display","none");
}
} else {
lastPost = find_elements.at(-1);
let text = getTextFromAuthorElement(lastPost);
result = formatText(text);
isLoad = true;
}
if (!lastPage) {
page = page + page2;
fetchPageData(page);
}
}
function getTextFromAuthorElement(element) {
let text = '';
let childNodes = element.childNodes;
for (let i = 0; i < childNodes.length; i++) {
let node = childNodes[i];
if (node.nodeType === Node.TEXT_NODE) {
text += node.nodeValue.trim();
}
}
return text;
}
function formatText(text) {
let timeIndex = text.lastIndexOf('at');
let strippedText;
let time;
if (timeIndex < 0) {
timeIndex = text.lastIndexOf('-');
time = text.length - timeIndex;
strippedText = text.substring(2).slice(0, -time).trim();
return strippedText;
} else {
time = text.length - timeIndex;
strippedText = text.substring(2).slice(0, -time).trim();
if (strippedText === 'Yesterday') {
let yesterday = new Date();
let today = new Date();
return yesterday.setDate(today.getDate() - 1);
} else {
return new Date();
}
}
}
});
I edited to be under 30 days to display message for testing purposes. However, it doesn't show anything.
Guest- Guest
Page 1 of 2 • 1, 2
Similar topics
» VPS check
» How Do I check IPs?
» IP check
» User Cannot Scroll Page + Sudden Page Format Change
» Can someone please check this part of my css code?
» How Do I check IPs?
» IP check
» User Cannot Scroll Page + Sudden Page Format Change
» Can someone please check this part of my css code?
Page 1 of 2
Permissions in this forum:
You cannot reply to topics in this forum