Topic Owner

Go down

Solved Topic Owner

Post by TamDonCo on March 31st 2018, 11:00 pm

Hi All

I would like to find the topic owner (who creates topic or thread), my code finds the first one in the page so it only works on page 1 and of course it does not work on page 2 ... Do you have any idea ?


Code:
  $('.postprofile dt').each(function(){
 
  var tOwner = $('.postprofile dt:eq(0)').text();
  console.log(tOwner);
}


Last edited by TamDonCo on April 4th 2018, 3:03 pm; edited 1 time in total
avatar
TamDonCo
Forumember

Posts : 342
Reputation : 2
Language : English

http://nhomcho.forummotion.com/

Back to top Go down

Solved Re: Topic Owner

Post by Wecoc on April 1st 2018, 2:47 am

You don't need
Code:
each
to get the first one of the page, you could simply use something like
Code:
$(selector)[0]
or
Code:
document.querySelector(selector)
:

Code:
var owner = $('.postprofile dt')[0];
var owner = document.querySelector('.postprofile dt');

Here's a more extended example:

Code:
var author_avatar = document.querySelector('.postprofile-avatar').innerHTML,
    author_link = document.querySelector('.postprofile-name').innerHTML,
    author_name = document.querySelector('.postprofile-name').innerText;

To get the topic's poster via Javascript you will need to use
Code:
$.get
to get the first page document and obtain these variables from there.

Code:
var href = window.location.pathname.replace(/p(\d*)?-/, '-');
var author_avatar, author_link, author_name;
$.get(href, function(d){
  author_avatar = document.querySelector('.postprofile-avatar', d).innerHTML;
  author_link = document.querySelector('.postprofile-name', d).innerHTML;
  author_name = document.querySelector('.postprofile-name', d).innerText;
}).done(function(d) {
  // Here do whatever you want with author_avatar, author_link, author_name
  console.log(author_avatar);
  console.log(author_link);
  console.log(author_name);
});

Here's how I would do it, but probably there are other ways.


Last edited by Wecoc on April 1st 2018, 1:39 pm; edited 1 time in total
avatar
Wecoc
Forumember

Male Posts : 127
Reputation : 82
Language : Catalan, Spanish, English

http://www.mundo-maker.com/

Back to top Go down

Solved Re: Topic Owner

Post by TamDonCo on April 1st 2018, 3:49 am

Thanks Wecoc

I will try later and let you know
avatar
TamDonCo
Forumember

Posts : 342
Reputation : 2
Language : English

http://nhomcho.forummotion.com/

Back to top Go down

Solved Re: Topic Owner

Post by TamDonCo on April 1st 2018, 6:33 am

Uncaught TypeError: Cannot read property 'innerHTML' of null

on this line

author_avatar = document.querySelector('.postprofile-avatar', d).innerHTML


I do not see any class .postprofile-avatar
avatar
TamDonCo
Forumember

Posts : 342
Reputation : 2
Language : English

http://nhomcho.forummotion.com/

Back to top Go down

Solved Re: Topic Owner

Post by Wecoc on April 1st 2018, 1:38 pm

Sorry, I thought it was the same in phpBB3 but now I see the profile parts don't have a specific class there.
My code was meant for modernBB Confused

The selectors change but the rest of the code is the same.

Code:
author_avatar = document.querySelector('.postprofile dt', d).firstChild.innerHTML;
author_link = document.querySelector('.postprofile dt', d).lastChild.innerHTML;
author_name = document.querySelector('.postprofile dt', d).lastChild.innerText;

I hope now it works.
avatar
Wecoc
Forumember

Male Posts : 127
Reputation : 82
Language : Catalan, Spanish, English

http://www.mundo-maker.com/

Back to top Go down

Solved Re: Topic Owner

Post by TamDonCo on April 1st 2018, 4:32 pm

Still the same error
avatar
TamDonCo
Forumember

Posts : 342
Reputation : 2
Language : English

http://nhomcho.forummotion.com/

Back to top Go down

Solved Re: Topic Owner

Post by Wecoc on April 2nd 2018, 4:56 pm

I tried it in 2 different phpBB3 forums and it worked well in both, the only issue I found is you have to be logged in to get the user's link, so now I've put an escaper when that occurs. This should work, the only reason this may not be working would be if you have a very edited viewtopic_body.

Code:
var topic_location = /\/t(\d*)/.test(window.location.pathname);
// Return if you are not viewing a topic
if (!topic_location) return false;
var href = window.location.pathname.replace(/p(\d*)?-/, '-');
var author_avatar, author_link, author_name;
$.get(href, function(d){
  author_avatar = document.querySelector('.postprofile dt', d).firstChild.innerHTML;
  author_link = document.querySelector('.postprofile dt', d).lastChild.innerHTML;
  author_name = document.querySelector('.postprofile dt', d).lastChild.innerText;
}).done(function(d) {
  // Return if avatar or link are not obtained (user is a guest)
  if (!author_avatar || !author_link) return false;
  // Here do whatever you want with author_avatar, author_link, author_name
  console.log(author_avatar);
  console.log(author_link);
  console.log(author_name);
});

Also, I don't know where are you inserting this and for what reason exactly, but it must be inside a function.
If you don't put it inside a function you will get this error: Uncaught SyntaxError: Illegal return statement

That's all I can say and do about this, if you still have troubles you will have to give more details on the complete code and what are you trying to accomplish.
Greetings Wink
avatar
Wecoc
Forumember

Male Posts : 127
Reputation : 82
Language : Catalan, Spanish, English

http://www.mundo-maker.com/

Back to top Go down

Solved Re: Topic Owner

Post by TamDonCo on April 3rd 2018, 2:06 am

Thanks Wecoc

I will try later, I am busy now
avatar
TamDonCo
Forumember

Posts : 342
Reputation : 2
Language : English

http://nhomcho.forummotion.com/

Back to top Go down

Solved Re: Topic Owner

Post by TamDonCo on April 3rd 2018, 8:20 pm

Hi Wecoc

There is no error but it shows the first post of current page as my example too, not the owner of toptic
avatar
TamDonCo
Forumember

Posts : 342
Reputation : 2
Language : English

http://nhomcho.forummotion.com/

Back to top Go down

Solved Re: Topic Owner

Post by Wecoc on April 3rd 2018, 9:25 pm

There is no error but it shows the first post of current page as my example too, not the owner of toptic

Luckly now I know why Laughing

Code:
var topic_location = /\/t(\d*)/.test(window.location.pathname);
// Return if you are not viewing a topic
if (!topic_location) return false;
var href = window.location.pathname.replace(/p(\d*)?-/, '-');
var author_avatar, author_link, author_name, postprofile;
$.get(href, function(d){
  postprofile = $('.postprofile dt', d)[0];
  author_avatar = postprofile.firstChild.innerHTML;
  author_link = postprofile.lastChild.innerHTML;
  author_name = postprofile.lastChild.innerText;
}).done(function() {
  // Return if avatar or link are not obtained (user is a guest)
  if (!author_avatar || !author_link) return false;
  // Here do whatever you want with author_avatar, author_link, author_name
  console.log(author_avatar);
  console.log(author_link);
  console.log(author_name);
});

The trouble was with the error, now you don't have that error this is plain sailing.
avatar
Wecoc
Forumember

Male Posts : 127
Reputation : 82
Language : Catalan, Spanish, English

http://www.mundo-maker.com/

Back to top Go down

Solved Re: Topic Owner

Post by TamDonCo on April 4th 2018, 12:21 am

Thank You very much Wecoc


It works now .. héhé
avatar
TamDonCo
Forumember

Posts : 342
Reputation : 2
Language : English

http://nhomcho.forummotion.com/

Back to top Go down

Solved Re: Topic Owner

Post by APE on April 4th 2018, 8:00 pm

Problem solved & topic archived.
Please read our forum rules: ESF General Rules



avatar
APE
Administrator
Administrator

Male Posts : 12458
Reputation : 1304
Language : fluent in dork / mumbojumbo & English haha

http://chatworld.forumotion.co.uk/

Back to top Go down

Back to top


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