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 เพื่อติดตั้งสคริปต์นี้

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

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

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

(I already have a user script manager, let me install it!)

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.

(I already have a user style manager, let me install it!)

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;
  });
})();