how to jump to #unread anchor

zawin

how to jump to #unread anchor

Post by zawin »

Hello,
in received email it is following link, which points to the last unread post: /Forum/viewtopic.php?f=5&t=1&e=1&view=unread#unread
It is possible to jump to the anchor #unread, after page with this link is loaded?

Thank you.

User avatar
axew3
w3all User
w3all User
Posts: 2231
Joined: Fri Jan 22, 2016 5:15 pm
Location: Italy
Contact:

Re: how to jump to #unread anchor

Post by axew3 »

yes, you read on my mind thank you for report, known problem.
FIXED V6 will fix this, and any redirected link like these (as well any other):

Code: Select all

https://www.phpbb.com/community/viewtopic.php?f=496&t=2554596&e=1&view=unread#unread
added on queue into this thread
https://www.axew3.com/w3/forums/viewtop ... f=2&t=1585
where V5 code has been also reported to be patched to fix something else in the while.
I will check to fix on fly if i can find out a moment, even before V6

User avatar
axew3
w3all User
w3all User
Posts: 2231
Joined: Fri Jan 22, 2016 5:15 pm
Location: Italy
Contact:

Re: how to jump to #unread anchor

Post by axew3 »

The V5 overall_footer.html code has been updated to resolve this issue:

https://www.axew3.com/w3/2020/01/phpbb- ... iframe-v5/

To fix:

on added overall_footer.html js code, find this line:

Code: Select all

var hash = window.location.hash.substr(1);
immediately after, add this:

Code: Select all


if( hash == 'unread' ){
 	var postN = 1;
 $( "h3 > a" ).each(function (e) { // need to match on DOM
   ns = this.href.split('#p'); 
   if( typeof ns != 'undefined' && /viewtopic.php\?p=/ig.exec(this.href) != null ){
   	if(ns[1] > postN){
	   w3lh = ns[1];
	  }
	 }
 })
 
 if( typeof w3lh != 'undefined' ){
  hash = "p" + w3lh;
 }
}

remember to recompile phpBB template.

Note that the code search for h3 elements, containing a elements, than containing strings like this viewtopic.php?p=
so on theme DOM, the posts titles links, needs to exists wrapped like they are into default subsilver, or this line:
$( "h3 > a" ).each(function (e) {
need to change selectors to match on DOM correct post's a links.

Work fine into all themes where post's links a elements are inside an h3 element, like it is on prosilver, but into all themes?
In case as said, the line of code that search for elements on DOM need to be changed to match.
Do not know if there is another way than this to achieve and resolve. I do not think that there is, at moment.

zawin

Re: how to jump to #unread anchor

Post by zawin »

Hello,
I can confirm that code works. I changed selector based on my page structure. Thank you ;)

zawin

Re: how to jump to #unread anchor

Post by zawin »

Hello,
I found problem with this code - If I click to URL with #unread anchor, then page jumps to the latest post instead of the latest unread post.

User avatar
axew3
w3all User
w3all User
Posts: 2231
Joined: Fri Jan 22, 2016 5:15 pm
Location: Italy
Contact:

Re: how to jump to #unread anchor

Post by axew3 »

It scroll to last post, but should instead scroll to, the last unread post, which could be not the last one inserted on thread, when there are more new replies? Do not know how it work, not checked deeply in true, so i will check asap how phpBB execute this task, and how to mimic it.

P.s i see it exist a template S_LOAD_UNREADS var which
so i assume it is the easy and correct way to accomplish with this

mh... just checked, no it is not the way ... let see asap more deeply

p.s
and on fly, i see that could be possible checking for class unreadpost, so the above code need to be little changed to achieve correctly the result

User avatar
axew3
w3all User
w3all User
Posts: 2231
Joined: Fri Jan 22, 2016 5:15 pm
Location: Italy
Contact:

Re: how to jump to #unread anchor

Post by axew3 »

the way was easy, but there is a problem:
class unreadpost, when the forum page display, do not exist anymore, because reversing things i see that this happen:
when the overall_header.html added code fire, that reload pointing to wordpress page, and that run because direct link to board detected, at this point, phpBB already processed all the code that then, set as read the post, and then when iframe load in WP page, class unreadpost is not existent anymore on DOM. So at moment, i think that better than the proposed solution, there is no solution.

... a solution ever exist.
So may let overall_header.html to not fire, when unread detected, leaving the page to follow to the overall_footer.html code, where hash then is possible to be grabbed: then get the value, assign it to a var, that when passed (with the redirect that will fire at this point), will scroll the page forum to the iframe position, exactly where it need to be. It seem to me there is no other possible solution (without changing something into phpBB php code that process the part on top of all this, that's really not a convenient reliable solution, even if maybe there is a listener that could be used for the scope). The page forum code need to be changed to accomplish with this, to scroll it to the passed value.

The problem is that this solution let the phpbb forum display for a while, because the js redirect done on footer and not header.

User avatar
axew3
w3all User
w3all User
Posts: 2231
Joined: Fri Jan 22, 2016 5:15 pm
Location: Italy
Contact:

Re: how to jump to #unread anchor

Post by axew3 »

I've try out all the thing, since it is the unique way to resolve this issue:
It scroll to last post, but should instead scroll to, the last unread post, which could be not the last one inserted on thread, when there are more new replies? Do not know how it work, not checked deeply in true, so i will check asap how phpBB execute this task, and how to mimic it.
subsequent my assertion:
page-forum need to be changed to accomplish with this
not true.

This is the solution that mimic, may that appear little complicate, but it is not, and works fine.
I will not go to add at moment into online V5 code, since it is an on-fly working code, that i will check on how to add properly into next V6 code, may obscuring with a trick the part where the page reload happen, and shortening code lines, since instructions can be unified. This is extended, to easily understand how the joke work. And it work fine.

Here we go with snippet example

on the js overall_header.html added code, just after
// END MANDATORY CHANGES line,
ADD this:

Code: Select all

 if(/view=unread#unread/ig.exec(window.location.href) !== null && /viewtopic.php\?/ig.exec(window.location.href) !== null)
 { 
 	 var w3allUnreadTopic = 1;
 }
then change/replace this line:
document.location.replace(href0);
into this:

Code: Select all

    if(typeof w3allUnreadTopic == 'undefined'){
     document.location.replace(href0);
    }
Save.
(so avoid code to redirect here, if unread detected: let follow to the overall_footer js code, redirect after, when grabbed what needed (read previous post to know why))

Now open overall_footer.html
on added js code, just after this comment:

Code: Select all

///////////////////////////////////////////////////////////////////////////
// w3all UPDATES for phpBB events
add the follow:

Code: Select all

if(window.self == window.top){
 if(typeof w3allUnreadTopic != 'undefined'){
  var hash = window.location.hash.substr(1);
 if( hash == 'unread' ){

  var w3postN = 1000000; // it's assumed that will never exists 1000000 unread posts x topic's page, isn't it?
  var w3lh,w3ns,w3Rhash;
  
 $( ".unreadpost" ).each(function (e) {

  var hpid = $(this).attr('id');
  w3ns = hpid.split('p');
  
   if(  w3ns[1] != 'undefined' ){
   	if(w3ns[1] < w3postN){  // detect when lower and assign
	   w3lh = w3postN = w3ns[1];
	   w3Rhash = "p" + w3lh;
	  }
	 }
 })

   if(  w3Rhash != 'undefined' ){ 
    // build url and redirect now, with common phpBB url, that point to the right post/hash on thread
    // Ex: https://localhost-subdomain.w3host.com/phpbb3/viewtopic.php?p=138#p138
   	var w3redirToUnread = boardU + 'viewtopic.php?p=' + w3lh + '#' + w3Rhash;
   	document.location.replace(w3redirToUnread);
   }
}
}
} // END if(window.self == window.top){
Save and remember to recompile stale template
This is it

[EDITED]

zawin

Re: how to jump to #unread anchor

Post by zawin »

Hello,
I found two issues with latest code snippet:
1) if I loading forum topic page with this link /viewtopic.php?f=16&t=45554&view=unread#unread and in this topic have not any unread posts then page tries to redirect to this page: /viewtopic.php?p=undefined#undefined
2) I had to a renamed hash variable to hash1 variable because some conflict appears in the console, and code doesn't work.

Thank you for your help.

User avatar
axew3
w3all User
w3all User
Posts: 2231
Joined: Fri Jan 22, 2016 5:15 pm
Location: Italy
Contact:

Re: how to jump to #unread anchor

Post by axew3 »

Ok, thank for report, let check, i will reply with the easy and improved solution asap

User avatar
axew3
w3all User
w3all User
Posts: 2231
Joined: Fri Jan 22, 2016 5:15 pm
Location: Italy
Contact:

Re: how to jump to #unread anchor

Post by axew3 »

To me, it was not throwing any error, anyway, when no unread post class found, due to a subsequent click into those kind of unread links, may clicked into notification email, after the topic already visited, what happen was that the code do not follow on redirect.
To complete the task, and make it redirect even when no unread found, redirecting to the last inserted, that obviously is the right one we want to be redirected to in this case, the complete code is little different, using both above snippets like this:

overall_header.html, leave code as is ( as explained here https://www.axew3.com/w3/forums/viewtop ... 4932#p4932 )

while remove completely the previous code into
overall_footer.html, and as before, just after this code comment:

Code: Select all

///////////////////////////////////////////////////////////////////////////
// w3all UPDATES for phpBB events
add this code
(and do you remember the line to change into very first snippet where it say need to match on DOM! ? Change also here like you did before, this line $( "h3 > a" ).each(function (e) { // need to match on DOM!
that need to match to elements a inside h3 as above:

Code: Select all

if(window.self == window.top){

 if(typeof w3allUnreadTopic != 'undefined'){
 	
 	var hash = window.location.hash.substr(1);
 
  if( hash == 'unread' ){

  var w3postN = 1000000; // it's assumed that will be never 1000000 posts x page, isn't it?
  var w3lh,w3ns,w3nsLowN;

  $( ".unreadpost" ).each(function (e) { 

  var hpid = $(this).attr('id');
  w3ns = hpid.split('p');
      	console.log(w3ns[1]);
   if(  w3ns[1] != 'undefined' ){
   	if(w3ns[1] < w3postN){
   //console.log('w3ns[1] AND w3postN -> '+w3ns[1]+' - '+w3postN);
	   w3lh = w3postN = w3ns[1];
	   w3nsLowN = w3ns[1];
	   w3Rhash = "p" + w3lh;
	  }
	 }
   //console.log('hash is ' + hash);
 })
 
 // https://localhost-subdomain.w3host.com/phpbb3/viewtopic.php?p=138#p138
   if( typeof w3ns != 'undefined' && typeof w3Rhash != 'undefined' ){ 
   	var redirToUnread = boardU + 'viewtopic.php?p=' + w3lh + '#' + w3Rhash;
   	document.location.replace(redirToUnread);
   } else {
 	   var postN = 1;
     $( "h3 > a" ).each(function (e) { // need to match on DOM!
       ns = this.href.split('#p'); 
      if( typeof ns != 'undefined' && /viewtopic.php\?p=/ig.exec(this.href) != null ){
   	   if(ns[1] > postN){
	      w3lh = ns[1];
	     }
	    }
     })

     if( typeof w3lh != 'undefined' ){
      var w3redirToLast = boardU + 'viewtopic.php?p=' + w3lh + '#p' + w3lh;
        var w3all_r = w3all0Normalize_phpBBUrl(w3redirToLast);
       if(/adm\/index\.php/ig.exec(w3all_r) == null){
       var hrefEn = window.btoa(unescape(encodeURIComponent(w3all_r)));
        var hre =  wordpress_url_page_f + '/?'+ w3all_custom_wp_w3fancyurl + '=' + hrefEn;
         document.location.replace(hre);
       }
      }
     }
  }
 }
} // END if(window.self == window.top){
it still lack of a sort of trick, that should obscure things, where time latency until redirect happen

User avatar
axew3
w3all User
w3all User
Posts: 2231
Joined: Fri Jan 22, 2016 5:15 pm
Location: Italy
Contact:

Re: how to jump to #unread anchor

Post by axew3 »

p.s at very late night

to obscure phpBB content before the redirect happen as fast example (could be done better) :

on added js code, just after this line:

Code: Select all

  if( hash == 'unread' ){
add the follow:

Code: Select all

 $( "body" ).prepend( "<div class=\"preUnreadRedirectBG\"></div>" );
 $(".preUnreadRedirectBG").css({ "position":"fixed","top":"0%","bottom":"0%","left":"0%","right":"0%","background": "rgba(0,0,0,1)","z-index": "99999","width":"100%" })

zawin

Re: how to jump to #unread anchor

Post by zawin »

Hello,
thank you for code update. This error appear if I don't rename hash variable.

Image

User avatar
axew3
w3all User
w3all User
Posts: 2231
Joined: Fri Jan 22, 2016 5:15 pm
Location: Italy
Contact:

Re: how to jump to #unread anchor

Post by axew3 »

This error appear if I don't rename hash variable.
What it mean? What kind of url you try to load to get the error?

Should not because if/when there are no unread, the code should redirect to last one, passing urls like

Code: Select all

https://localhost-subdomain.w3host.com/phpbb3/viewtopic.php?f=2&t=26&e=1&view=unread#unread
AND NOTE that as code is
this line need to match on DOM or as you see, it will return error after, because the code search for unread, and if not found, search for the last one inserted, and if not found again, it make fail the code as before was, so it is mandatory that these elements match on DOM
$( "h3 > a" ).each(function (e) { // need to match on DOM!

The error mean that hash var do not contain a valid value, so when the js search for value based on this it return error. Of course the code can be fixed to avoid this in any case: anyway if no unread found, we should want to redirect to the last one inserted

going to repeat steps to resume:

on the js overall_header.html added code, just after
// END MANDATORY CHANGES line,
ADD this:

Code: Select all

 if(/view=unread#unread/ig.exec(window.location.href) !== null && /viewtopic.php\?/ig.exec(window.location.href) !== null)
 { 
 	 var w3allUnreadTopic = 1;
 } 
then change/replace this line:
document.location.replace(href0);
into this:

Code: Select all

  if(typeof w3allUnreadTopic == 'undefined'){
     document.location.replace(href0);
    }
Save.

Now open overall_footer.html
on added js code, just after this comment:

Code: Select all

///////////////////////////////////////////////////////////////////////////
// w3all UPDATES for phpBB events
add the follow:

Code: Select all

if(window.self == window.top){

 if(typeof w3allUnreadTopic != 'undefined'){
 	
 	var hash = window.location.hash.substr(1);
 
  if( hash == 'unread' ){
  	
 $( "body" ).prepend( "<div class=\"preUnreadRedirectBG\"></div>" );
 $(".preUnreadRedirectBG").css({"position":"fixed","color": "red","top":"0%","bottom":"0%","left":"0%","right":"0%","background": "rgba(0,0,0,1)","z-index": "99999","width":"100%"})

  var w3postN = 1000000; // it's assumed that will be never 1000000 replies x page
  var w3lh,w3ns,w3nsLowN;

  $( ".unreadpost" ).each(function (e) { 

  var hpid = $(this).attr('id');
  w3ns = hpid.split('p');
   if(  w3ns[1] != 'undefined' ){
   	if(w3ns[1] < w3postN){
   //console.log('w3ns[1] AND w3postN -> '+w3ns[1]+' - '+w3postN);
	   w3lh = w3postN = w3ns[1];
	   w3nsLowN = w3ns[1];
	   w3Rhash = "p" + w3lh;
	  }
	 }
   //console.log('hash is ' + hash);
 })
 
 // https://localhost-subdomain.w3host.com/phpbb3/viewtopic.php?p=138#p138
   if( typeof w3ns != 'undefined' && typeof w3Rhash != 'undefined' ){ 
   	var redirToUnread = boardU + 'viewtopic.php?p=' + w3lh + '#' + w3Rhash;
   	document.location.replace(redirToUnread);
   } else {
 	   var postN = 1;
     $( "h3 > a" ).each(function (e) { // need to match on DOM!
       ns = this.href.split('#p'); 
      if( typeof ns != 'undefined' && /viewtopic.php\?p=/ig.exec(this.href) != null ){
   	   if(ns[1] > postN){
	      w3lh = ns[1];
	     }
	    }
     })

     if( typeof w3lh != 'undefined' ){
      var w3redirToLast = boardU + 'viewtopic.php?p=' + w3lh + '#p' + w3lh;
        var w3all_r = w3all0Normalize_phpBBUrl(w3redirToLast);
       if(/adm\/index\.php/ig.exec(w3all_r) == null){
       var hrefEn = window.btoa(unescape(encodeURIComponent(w3all_r)));
        var hre =  wordpress_url_page_f + '/?'+ w3all_custom_wp_w3fancyurl + '=' + hrefEn;
         document.location.replace(hre);
       }
      }
     }
  }
 }
} // END if(window.self == window.top){
Remember to recompile stale template.
It is tested working into any browser here, so let know if still in trouble

User avatar
axew3
w3all User
w3all User
Posts: 2231
Joined: Fri Jan 22, 2016 5:15 pm
Location: Italy
Contact:

Re: how to jump to #unread anchor

Post by axew3 »

i'm taking a look for v6 code and looking to this solution, the short and easy:
https://www.axew3.com/w3/forums/viewtop ... 4932#p4932

this part:

Code: Select all

if(window.self == window.top){
 if(typeof w3allUnreadTopic != 'undefined'){
  var hash = window.location.hash.substr(1);
 if( hash == 'unread' ){

  var w3postN = 1000000; // it's assumed that will never exists 1000000 unread posts x topic's page, isn't it?
  var w3lh,w3ns,w3Rhash;
  
 $( ".unreadpost" ).each(function (e) {

  var hpid = $(this).attr('id');
  w3ns = hpid.split('p');
  
   if(  w3ns[1] != 'undefined' ){
   	if(w3ns[1] < w3postN){  // detect when lower and assign
	   w3lh = w3postN = w3ns[1];
	   w3Rhash = "p" + w3lh;
	  }
	 }
 })

   if(  w3Rhash != 'undefined' ){ 
    // build url and redirect now, with common phpBB url, that point to the right post/hash on thread
    // Ex: https://localhost-subdomain.w3host.com/phpbb3/viewtopic.php?p=138#p138
   	var w3redirToUnread = boardU + 'viewtopic.php?p=' + w3lh + '#' + w3Rhash;
   	document.location.replace(w3redirToUnread);
   }
}
}
} // END if(window.self == window.top){
where
// detect when lower and assign
it is clear to me now, that all the joke can be done in this exact point:
why to iterate until the highest, and do not stop to the first?
Isn't it?! This is the solution.

Just a remind for the coming v6 code release, many parts of the v5 code needs to be properly fixed.

Also page-forum.php has been updated, but still not committed. It will be together with all nice fixes coming on v6!

User avatar
axew3
w3all User
w3all User
Posts: 2231
Joined: Fri Jan 22, 2016 5:15 pm
Location: Italy
Contact:

Re: how to jump to #unread anchor

Post by axew3 »

I've give a re-look into this joke, and fixed working into any user (logged or not).
The unique things that 'intentionally' do not will match with default behavior, because it has been intentionally changed to be this way, is the fact that if an user is not logged in and present a link like this:

Code: Select all

https://subdomain.w3host.com/phpbb3/viewtopic.php?f=2&t=15&e=1&view=unread#unread
the code do not redirect the user to the login page, but to the last post on topic.

Resuming all the procedure, that at moment will not be added into v6 code by default, but that work fine, is as follow:



on the js overall_header.html added code, just after
// END MANDATORY CHANGES line,
ADD this:

Code: Select all

 if(/view=unread#unread/ig.exec(window.location.href) !== null && /viewtopic.php\?/ig.exec(window.location.href) !== null)
 { 
 	 var w3allUnreadTopic = 1;
 } 
then change/replace this line:
document.location.replace(href0);
into this:

Code: Select all

  if(typeof w3allUnreadTopic == 'undefined'){
     document.location.replace(href0);
    }
Save.

Now open overall_footer.html
on added js code, just after this comment:

Code: Select all

///////////////////////////////////////////////////////////////////////////
// w3all UPDATES for phpBB events
add the follow:

Code: Select all

if(window.self == window.top){

 if(typeof w3allUnreadTopic != 'undefined'){
 	
 $( "body" ).prepend( "<div class=\"preUnreadRedirectBG\"></div>" );
 $(".preUnreadRedirectBG").css({"position":"fixed","color": "red","top":"0%","bottom":"0%","left":"0%","right":"0%","background": "rgba(0,0,0,1)","z-index": "99999","width":"100%"})

 	var hash = window.location.hash.substr(1);
 
  if( hash == 'unread'){
  	
  	if("{S_USER_LOGGED_IN}" < 1){
  		var usp = window.location.href;
  		w3ns = usp.split('&e=1');
  		document.location.replace(w3ns[0]);
  	}
 
 if("{S_USER_LOGGED_IN}" > 0){ 
  var w3postN = 1000000;
  var w3lh,w3ns,w3nsLowN;

 $( ".unreadpost" ).each(function (e) { 
  var hpid = $(this).attr('id');
  w3ns = hpid.split('p');
   if(  w3ns[1] != 'undefined' ){
   	if(w3ns[1] < w3postN){ // reset to the lower if found
	   w3lh = w3postN = w3ns[1]; // set to actual
	   w3nsLowN = w3ns[1];
	   w3Rhash = "p" + w3nsLowN;
	  }
	 }
 })

 // https://localhost-subdomain.w3host.com/phpbb3/viewtopic.php?p=138#p138
   if( typeof w3ns != 'undefined' && typeof w3Rhash != 'undefined' ){ 
   	
   	var redirToUnread = boardU + 'viewtopic.php?p=' + w3lh + '#' + w3Rhash;
   	document.location.replace(redirToUnread);
   } else {
 	   var postN = 1;
     $( "h3 > a" ).each(function (e) { // need to match on DOM!
       ns = this.href.split('#p'); 
      if( typeof ns != 'undefined' && /viewtopic.php\?p=/ig.exec(this.href) != null ){
   	   if(ns[1] > postN && typeof w3lh == 'undefined'){
	      w3lh = ns[1];
	     }
	    }
     })

     if( typeof w3lh != 'undefined' ){
      var w3redirToLast = boardU + 'viewtopic.php?p=' + w3lh + '#p' + w3lh;
       if(/adm\/index\.php/ig.exec(w3all_r) == null){
         document.location.replace(w3redirToLast);
       }
      }
     }
     
   } // END	if("{S_USER_LOGGED_IN}" > 0){  
     
  }
 }
} // END if(window.self == window.top){
cheers!

oferlaor
User w
User w
Posts: 10
Joined: Wed Jan 27, 2021 2:24 pm

Re: how to jump to #unread anchor

Post by oferlaor »

my proposal for overall_footer.php

Code: Select all

			if( hash == 'unread' ){
				var hash_unread = true; //let's keep in mind we have unread hash
				var postN = 1;
				if ( $("#unread").length > 0) {
					hash= "unread";
				} else {
					$( "h3 > a" ).each(function (e) { // need to match on DOM
						ns = this.href.split('#p');
						if( typeof ns != 'undefined' && /viewtopic.php\?p=/ig.exec(this.href) != null ){
							if(ns[1] > postN){
								w3lh = ns[1];
							}
						}
					})
					if( typeof w3lh != 'undefined' ){
						hash = "p" + w3lh;
					}
				}
			}


Post Reply