ESPNCricInfo Threaded Comments

Convert flat comments on ESPNCricInfo stories into threaded comments.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

Advertisement:

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

Advertisement:

// ==UserScript==
// @name         ESPNCricInfo Threaded Comments
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Convert flat comments on ESPNCricInfo stories into threaded comments.
// @author       Sudhee
// @match        http://www.espncricinfo.com/*/content/story/*
// @grant        none
// @locale       en
// ==/UserScript==

(function () {
    'use strict';
    var once = false;
  $('.user-comments a[href=#all]').on('click', function () {
      if (once) {
          return;
      }
      console.log("Converting comments to threads!!!");
      var $comments = $('ul.container-tab.all li');
      var userNameMap = {};
      var comments = [];
      $comments.find('.user_name').each(function (idx) {
          var userName = $(this).contents().filter(function () {
              return this.nodeType == 3;
          }).text().trim();
          if (!$.isArray(userNameMap[userName])) {
              userNameMap[userName] = [];
          }
          userNameMap[userName].push(idx);
      });
      $comments.each(function (idx) {
          var $textNodes = $(this).find('p').filter(function () {
              return !!$(this).text().trim();
          });
          var comment = "";
          $textNodes.each(function () {
              comment += $(this).text().trim();
          });
          comments.push(comment);
      });
      comments.forEach(function (comment, idx) {
          $.each(userNameMap, function (userName, userCommentIdxArray) {
              var a = comment.toLowerCase().indexOf(userName.toLowerCase());
              if (a == -1 || a > 3) {
                  return;
              }
              var sourceIdx = userCommentIdxArray.filter(function (b) {
                  return b > idx;
              });
              // console.log(sourceIdx.length);
              var $reply = $comments.eq(idx);
              var $moveReply = $reply.clone();
              $moveReply.css('borderBottom', '0');
              $reply.hide();
              /* if (sourceIdx.length > 1) {
                  console.log("Reply to " + userName + " from " + userCommentIdxArray + ": " + comment + " & " + idx);
                  $.each(sourceIdx, function (j, val) {
                      console.log("Original comments: " + comments[val]);
                  });
              } */
              var $moveReplyContainer = $('<ul></ul>').html($moveReply);
              $moveReplyContainer.css({
                  'float': 'left',
                  'borderLeft': '1px solid #e8e8e8',
                  'margin': '0 0 0 30px',
                  'paddingLeft': '20px'
              });
              $comments.eq(sourceIdx[0]).append($moveReplyContainer);
              return false;
          });
      });
      // $(window).scrollTop($('div.user-comments').offset().top);
      once = true;
  });
})();