The forum of the forums
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Check only last page

4 posters

Page 1 of 2 1, 2  Next

Go down

Solved Check only last page

Post by The Raven August 7th 2023, 4:57 pm

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
The Raven
The Raven
Forumember

Male Posts : 215
Reputation : 11
Language : English

https://timetravel.forumotion.com/

poesia-verses likes this post

Back to top Go down

Solved Re: Check only last page

Post by poesia-verses August 7th 2023, 6:44 pm

interesting idea*
poesia-verses
poesia-verses
Forumember

Male Posts : 316
Reputation : 4
Language : russian

https://stihi-podval.forumotion.com/

Back to top Go down

Solved Re: Check only last page

Post by Razor12345 August 7th 2023, 7:03 pm

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?


Check only last page Screen51
Razor12345
Razor12345
Support Moderator
Support Moderator

Male Posts : 750
Reputation : 150
Language : Ukr, Rus, Eng
Location : Ukraine

https://help.forumotion.com

Back to top Go down

Solved Re: Check only last page

Post by The Raven August 7th 2023, 7:37 pm

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.

Check only last page Screen39

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.
The Raven
The Raven
Forumember

Male Posts : 215
Reputation : 11
Language : English

https://timetravel.forumotion.com/

Back to top Go down

Solved Re: Check only last page

Post by Razor12345 August 7th 2023, 8:45 pm

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.


Check only last page Screen51
Razor12345
Razor12345
Support Moderator
Support Moderator

Male Posts : 750
Reputation : 150
Language : Ukr, Rus, Eng
Location : Ukraine

https://help.forumotion.com

Back to top Go down

Solved Re: Check only last page

Post by The Raven August 7th 2023, 9:50 pm

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?
The Raven
The Raven
Forumember

Male Posts : 215
Reputation : 11
Language : English

https://timetravel.forumotion.com/

Back to top Go down

Solved Re: Check only last page

Post by Razor12345 August 8th 2023, 9:59 am

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.


Check only last page Screen51
Razor12345
Razor12345
Support Moderator
Support Moderator

Male Posts : 750
Reputation : 150
Language : Ukr, Rus, Eng
Location : Ukraine

https://help.forumotion.com

Back to top Go down

Solved Re: Check only last page

Post by TheCrow August 8th 2023, 10:13 am

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.
TheCrow
TheCrow
Manager
Manager

Male Posts : 6878
Reputation : 794
Language : Greek, English

https://forumote.forumotion.com

The Raven likes this post

Back to top Go down

Solved Re: Check only last page

Post by The Raven August 8th 2023, 10:44 am

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.
The Raven
The Raven
Forumember

Male Posts : 215
Reputation : 11
Language : English

https://timetravel.forumotion.com/

Back to top Go down

Solved Re: Check only last page

Post by Razor12345 August 8th 2023, 1:53 pm

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;
}
 
});

Code:
let page2 = 5;
- Here specify the value that corresponds to the field in the AP - General - Messages and emails - Configuration - Posts per page. I have indicated 5 for clarity of the result.

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)));

Code:
Math.floor(difference / (1000 * 60 * 60 * 24))
- the difference between today's date and the date of the most recent post in the topic. Calculated in days.

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)

Check only last page Scree305


Check only last page Screen51
Razor12345
Razor12345
Support Moderator
Support Moderator

Male Posts : 750
Reputation : 150
Language : Ukr, Rus, Eng
Location : Ukraine

https://help.forumotion.com

The Raven likes this post

Back to top Go down

Solved Re: Check only last page

Post by The Raven August 8th 2023, 2:11 pm

I got NaN after I configured as you said in the previous post.

Check only last page Screen40
The Raven
The Raven
Forumember

Male Posts : 215
Reputation : 11
Language : English

https://timetravel.forumotion.com/

Back to top Go down

Solved Re: Check only last page

Post by Razor12345 August 8th 2023, 3:15 pm

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.

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;
  }
}


Check only last page Screen51
Razor12345
Razor12345
Support Moderator
Support Moderator

Male Posts : 750
Reputation : 150
Language : Ukr, Rus, Eng
Location : Ukraine

https://help.forumotion.com

The Raven likes this post

Back to top Go down

Solved Re: Check only last page

Post by The Raven August 8th 2023, 6:37 pm

I am still getting NaN. Shocked
The Raven
The Raven
Forumember

Male Posts : 215
Reputation : 11
Language : English

https://timetravel.forumotion.com/

Back to top Go down

Solved Re: Check only last page

Post by Razor12345 August 8th 2023, 7:04 pm

Changed the date check to this one:

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


Check only last page Screen51
Razor12345
Razor12345
Support Moderator
Support Moderator

Male Posts : 750
Reputation : 150
Language : Ukr, Rus, Eng
Location : Ukraine

https://help.forumotion.com

TonnyKamper and The Raven like this post

Back to top Go down

Solved Re: Check only last page

Post by The Raven August 8th 2023, 7:32 pm

I got -1 value. What does it mean?
The Raven
The Raven
Forumember

Male Posts : 215
Reputation : 11
Language : English

https://timetravel.forumotion.com/

Back to top Go down

Solved Re: Check only last page

Post by Razor12345 August 8th 2023, 7:33 pm

Where?


Check only last page Screen51
Razor12345
Razor12345
Support Moderator
Support Moderator

Male Posts : 750
Reputation : 150
Language : Ukr, Rus, Eng
Location : Ukraine

https://help.forumotion.com

Back to top Go down

Solved Re: Check only last page

Post by The Raven August 8th 2023, 7:35 pm

Razor12345 wrote:Where?

Check only last page Screen41
The Raven
The Raven
Forumember

Male Posts : 215
Reputation : 11
Language : English

https://timetravel.forumotion.com/

Back to top Go down

Solved Re: Check only last page

Post by Razor12345 August 8th 2023, 7:37 pm

Send the link to the topic


Check only last page Screen51
Razor12345
Razor12345
Support Moderator
Support Moderator

Male Posts : 750
Reputation : 150
Language : Ukr, Rus, Eng
Location : Ukraine

https://help.forumotion.com

Back to top Go down

Solved Re: Check only last page

Post by The Raven August 8th 2023, 7:37 pm

The Raven
The Raven
Forumember

Male Posts : 215
Reputation : 11
Language : English

https://timetravel.forumotion.com/

Back to top Go down

Solved Re: Check only last page

Post by Razor12345 August 8th 2023, 7:44 pm

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.


Check only last page Screen51
Razor12345
Razor12345
Support Moderator
Support Moderator

Male Posts : 750
Reputation : 150
Language : Ukr, Rus, Eng
Location : Ukraine

https://help.forumotion.com

Back to top Go down

Solved Re: Check only last page

Post by The Raven August 8th 2023, 7:58 pm

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.
The Raven
The Raven
Forumember

Male Posts : 215
Reputation : 11
Language : English

https://timetravel.forumotion.com/

Back to top Go down

Solved Re: Check only last page

Post by Razor12345 August 8th 2023, 8:26 pm

I've looked at classes - the code is identical for Invision.

On the Invision forum, I have swapped the parameters in this line:

Code:
let difference = new Date() - new Date(result);

You can check result.


Check only last page Screen51
Razor12345
Razor12345
Support Moderator
Support Moderator

Male Posts : 750
Reputation : 150
Language : Ukr, Rus, Eng
Location : Ukraine

https://help.forumotion.com

The Raven likes this post

Back to top Go down

Solved Re: Check only last page

Post by The Raven August 8th 2023, 8:36 pm

Ok, so in browser now I get 0 as a result. Is that good?
The Raven
The Raven
Forumember

Male Posts : 215
Reputation : 11
Language : English

https://timetravel.forumotion.com/

Back to top Go down

Solved Re: Check only last page

Post by Razor12345 August 8th 2023, 8:40 pm

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.

Razor12345 wrote:
Code:
Math.floor(difference / (1000 * 60 * 60 * 24))
- the difference between today's date and the date of the most recent post in the topic. Calculated in days.

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 {
...
}


Check only last page Screen51
Razor12345
Razor12345
Support Moderator
Support Moderator

Male Posts : 750
Reputation : 150
Language : Ukr, Rus, Eng
Location : Ukraine

https://help.forumotion.com

Back to top Go down

Solved Re: Check only last page

Post by The Raven August 8th 2023, 8:50 pm

Okay, this works now like a charm. Thank you for your efforts. You literaly wrote the whole script from nothing. Shocked Shocked You are the man!


One more thing, will this also work on PhpBB3?
The Raven
The Raven
Forumember

Male Posts : 215
Reputation : 11
Language : English

https://timetravel.forumotion.com/

Back to top Go down

Solved Re: Check only last page

Post by Razor12345 August 9th 2023, 9:52 am

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.


Check only last page Screen51
Razor12345
Razor12345
Support Moderator
Support Moderator

Male Posts : 750
Reputation : 150
Language : Ukr, Rus, Eng
Location : Ukraine

https://help.forumotion.com

poesia-verses and The Raven like this post

Back to top Go down

Solved Re: Check only last page

Post by The Raven August 9th 2023, 10:02 am

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.
The Raven
The Raven
Forumember

Male Posts : 215
Reputation : 11
Language : English

https://timetravel.forumotion.com/

Back to top Go down

Solved Re: Check only last page

Post by TheCrow August 9th 2023, 10:16 am

Did you try it as
Code:
<div style="display:none!important">...</div>
also?
TheCrow
TheCrow
Manager
Manager

Male Posts : 6878
Reputation : 794
Language : Greek, English

https://forumote.forumotion.com

Back to top Go down

Solved Re: Check only last page

Post by Razor12345 August 9th 2023, 10:31 am

It's better not to use inline styles - especially with the !important rule.
Set a class for the block. Write
Code:
display:none
to this class in CSS.
Then, when the script works, write the style through JS -
Code:
someBlock.style.disply = 'block';


Check only last page Screen51
Razor12345
Razor12345
Support Moderator
Support Moderator

Male Posts : 750
Reputation : 150
Language : Ukr, Rus, Eng
Location : Ukraine

https://help.forumotion.com

Back to top Go down

Solved Re: Check only last page

Post by The Raven August 9th 2023, 10:38 am

Ok, so the css now looks like this:

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.
The Raven
The Raven
Forumember

Male Posts : 215
Reputation : 11
Language : English

https://timetravel.forumotion.com/

Back to top Go down

Page 1 of 2 1, 2  Next

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum