Discussions » Creation Requests

got stuck again, help please

§
Posted: 2015-02-10
Edited: 2015-02-11

got stuck again, help please

Please help again, what i want a simple script i guess, but i don't know how to do it and how to start it, i have this forum page, http://jsfiddle.net// and when you enter on user page http://jsfiddle.net// , i want the numbers to be on forum page next to user. forum does not have any api or json links

wOxxOmMod
§
Posted: 2015-02-10
Edited: 2015-02-11

Vanilla js.
Not tested.

v4.

// ==UserScript==
// @name        add user stats
// @include     ***************************************
// @grant       GM_xmlhttpRequest
// @grant       GM_setValue
// @grant       GM_getValue
// @grant       GM_addStyle
// @run-at      document-start
// ==/UserScript==

GM_addStyle('span.stats {margin-left:1em; margin-right:1em; color:gray}\
             span.stats a {color:black; font-weight:bold}');

new MutationObserver(function(mutations) {
  for (var i=0, ml=mutations.length, m; (i<ml) && (m=mutations[i]); i++)
    for (var j=0, nodes=m.addedNodes, nl=nodes.length, n; (j<nl) && (n=nodes[j]); j++)
      if (n.nodeType == 1)
        processNodes(n.querySelectorAll('a[href*="userdetails.php?id="]'));
}).observe(document, {subtree:true, childList:true});

function processNodes(links) {
  var queue = {};
  for (var i=0, ll=links.length, link; (i<ll) && (link=links[i]); i++) {
    var userID = link.href.match(/id=(\d+)/)[1];

    // display saved numbers temporarily
    var m, s = GM_getValue(userID);
    if (s && (m = s.match(/^(\d+),(\d+)$/)))
      updateUserStats(link, userID, m[1], m[2]);

    // queue an update
    queue[userID] = link;
    GM_xmlhttpRequest({
      method: 'GET',
      url: link.href,
      timeout: 5000,
      onload: function(r) {
        var comments = r.responseText.match(/action=viewcomments.+?>(\d+)</)[1];
        var posts = r.responseText.match(/action=viewposts.+?>(\d+)</)[1];
        var userID = r.finalUrl.match(/id=(\d+)/)[1];
        GM_setValue(userID, posts+','+comments);
        updateUserStats(queue[userID], userID, posts, comments);
        delete queue[userID];
      }
    });
  }
}

function updateUserStats(node, userID, posts, comments) {
  var header = node;
  while (header.className != 'forumPostName')
    header = header.parentNode;
  var avatar = header.nextElementSibling.querySelector('img');
  var stats = avatar.nextElementSibling;
  if (!stats || stats.className != 'stats')
    avatar.insertAdjacentHTML('afterend',
                            '<span class="stats">\
                             <a href="userhistory_posts.php?action=viewposts&id='+userID+'">\
                                <img src="http://i.imgur.com/BkX8Hya.png">'+posts+'</a> \
                             <a href="userhistory.php?action=viewcomments&id='+userID+'">\
                                <img src="http://i.imgur.com/vzS6BLE.png">'+comments+'</a></span>');
  else {
    stats.children[0].childNodes[1].textContent = posts;
    stats.children[1].childNodes[1].textContent = comments;
  }
}
§
Posted: 2015-02-11
Edited: 2015-02-11

Thank you very much! but i get only 1 error in console and the script din not show anything

ReferenceError: links is not defined adduserstats.user.js:29 the line for (var i=0, ll=links.length, link; (i<ll) && (link=links[i]); i++) {

§
Posted: 2015-02-11
Edited: 2015-02-11

apparently, instead of

function processNodes(nodes) {

there should be:

function processNodes(links) {
wOxxOmMod
§
Posted: 2015-02-11

Yup.

§
Posted: 2015-02-11
Edited: 2015-02-11

Yesss!! you guys are awesome! Thank you very much!!!

§
Posted: 2015-02-11
Edited: 2015-02-11

oh, one more little request please and that is it for now, it's can be done if i click on numbers to go on usercomments or userposts window? and instead of P: and C: i want these images , please, you are the best!
 

wOxxOmMod
§
Posted: 2015-02-11

See the updated the code above.

§
Posted: 2015-02-11

this is magic
 
thank you very much!

wOxxOmMod
§
Posted: 2015-02-11

As for the link images, do you need help with embedding those?

§
Posted: 2015-02-11

thank you, i got it how to do it, this is so cool!
 

Thank you again!

wOxxOmMod
§
Posted: 2015-02-11
Edited: 2015-02-11

The two numbers are the same - 5615 - are you sure you got it right?

BTW you may want to lower the images a little: span.stats a {color:black; font-weight:bold; margin-top:3px} or maybe position:relative; top:3px

§
Posted: 2015-02-11
Edited: 2015-02-11

whoops done something wrong, how to insert properly images? :D

wOxxOmMod
§
Posted: 2015-02-11

I've updated updateUserStats function in the code above, now it also uses childNodes property which provides access to both img and the text whereas children only lists HTMLElement nodes.

§
Posted: 2015-02-11
Edited: 2015-02-11

This is getting better and better, now the last one and i will be the happiest person, this info can be placed under avatars? please, this is the last wish for this

wOxxOmMod
§
Posted: 2015-02-11
Edited: 2015-02-11

See the updated updateUserStats function above.

§
Posted: 2015-02-11

Fantastic! Thank you for you hard work!

wOxxOmMod
§
Posted: 2015-02-13

wasn't so hard actually, otherwise I wouldn't do it :D

Post reply

Sign in to post a reply.