How to jump to #unread anchor - phpBB iframe js code

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

How to jump to #unread anchor - phpBB iframe js code

Post by axew3 »

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 - phpBB iframe js code

Post by oferlaor »

I made a small mod to the original code, as the jump didn't go to the correct location at all times:

Code: Select all

			if( hash == 'unread' ){
				var hash_unread = true; //let's keep in mind we have unread hash
				var postN = 1;
				if ( typeof $("#unread") != 'undefined') {
					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