kinopoisk comments rating

Добавляет рейтинг, основанный на комментариях. Кликнуть на рейтинг для получения пользовательского.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        kinopoisk comments rating
// @namespace   https://github.com/qiwichupa/userscripts/blob/master/kinopoisk_comments_rating
// @require     https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js
// @include     https://www.kinopoisk.ru/film/*
// @include     https://www.kinopoisk.ru/series/*
// @run-at        document-idle
// @grant         GM_xmlhttpRequest
// @grant         GM_addStyle
// @version     20240323.2501
// @author      qiwichupa
// @description Добавляет рейтинг, основанный на комментариях. Кликнуть на рейтинг для получения пользовательского.
// @license       CC BY-SA
// ==/UserScript==

function CountForm (number, titles) {
// https://gist.github.com/realmyst/1262561?permalink_comment_id=3443551#gistcomment-3443551
// CountForm(count, ['рубль', 'рубля', 'рублей']);
  number = Math.abs(number);
  if (Number.isInteger(number)) {
    cases = [2, 0, 1, 1, 1, 2];
    return titles[ (number%100>4 && number%100<20)? 2 : cases[(number%10<5)?number%10:5] ];
  }
  return titles[1];
}


function waitForKeyElements (
// https://gist.githubusercontent.com/raw/2625891/waitForKeyElements.js
    selectorTxt,    /* Required: The jQuery selector string that
                        specifies the desired element(s).
                    */
    actionFunction, /* Required: The code to run when elements are
                        found. It is passed a jNode to the matched
                        element.
                    */
    bWaitOnce,      /* Optional: If false, will continue to scan for
                        new elements even after the first match is
                        found.
                    */
    iframeSelector  /* Optional: If set, identifies the iframe to
                        search.
                    */
) {
    var targetNodes, btargetsFound;

    if (typeof iframeSelector == "undefined")
        targetNodes     = $(selectorTxt);
    else
        targetNodes     = $(iframeSelector).contents ()
                                           .find (selectorTxt);

    if (targetNodes  &&  targetNodes.length > 0) {
        btargetsFound   = true;
        /*--- Found target node(s).  Go through each and act if they
            are new.
        */
        targetNodes.each ( function () {
            var jThis        = $(this);
            var alreadyFound = jThis.data ('alreadyFound')  ||  false;

            if (!alreadyFound) {
                //--- Call the payload function.
                var cancelFound     = actionFunction (jThis);
                if (cancelFound)
                    btargetsFound   = false;
                else
                    jThis.data ('alreadyFound', true);
            }
        } );
    }
    else {
        btargetsFound   = false;
    }

    //--- Get the timer-control variable for this selector.
    var controlObj      = waitForKeyElements.controlObj  ||  {};
    var controlKey      = selectorTxt.replace (/[^\w]/g, "_");
    var timeControl     = controlObj [controlKey];

    //--- Now set or clear the timer as appropriate.
    if (btargetsFound  &&  bWaitOnce  &&  timeControl) {
        //--- The only condition where we need to clear the timer.
        clearInterval (timeControl);
        delete controlObj [controlKey]
    }
    else {
        //--- Set a timer, if needed.
        if ( ! timeControl) {
            timeControl = setInterval ( function () {
                    waitForKeyElements (    selectorTxt,
                                            actionFunction,
                                            bWaitOnce,
                                            iframeSelector
                                        );
                },
                300
            );
            controlObj [controlKey] = timeControl;
        }
    }
    waitForKeyElements.controlObj   = controlObj;
}


waitForKeyElements('[id^=user-review]', function() {
  var ucommelements = $('[class^=styles_count__]');
  var index;
  var element;
  var ucommscore;
  var ucommpos;
  var ucommcons;
  var ucommneu;
  var ucommtotal;


  for (index = 0; index < ucommelements.length; index++) {
    element = $(ucommelements[index]);

    if (element.next().text()) {
      if (element.parent().next().text() === 'Положительные'){
        ucommpos = Number(element.text());
        //element.parent().next().text('test' + ucommpos);
      }
      if (element.parent().next().text() === 'Отрицательные'){
        ucommcons = Number(element.text());
        //element.parent().next().text('test' + ucommcons);
      }
      if (element.parent().next().text() === 'Нейтральные'){
        ucommneu = Number(element.text());
        //element.parent().next().text('test' + ucommneu);
      }
    } else {
      ucommtotal = Number(element.parent().text());
      //element.parent().text('test' + ucommtotal);
    }
  }

  if (!ucommpos){ucommpos=0;}
  if (!ucommcons){ucommcons=0;}
  if (!ucommneu){ucommneu=0;}

  if (ucommtotal > 1 ){
    ucommscore =  ((10*ucommpos  + 5*ucommneu + 1*ucommcons) / ucommtotal ).toFixed(1);
    commsform = CountForm(ucommtotal, ['комментария', 'комментариев', 'комментариев']);

    $('[class^=film-rating-value]').children('span').each(function() {
      var element = $(this);
      var currenttext = element.text();

      $(this).text(currenttext + ' (' + ucommscore +  ')');
      $(this).attr('title', ucommscore +  ' на основе ' + ucommtotal + ' ' + commsform);
    });
  }

});


$(document).ready(function(){
  $('[class^=film-rating-value]').children('span').each(function() {
    var element = $(this);
    var currenttext = element.text();
    $(this).attr("style", "text-decoration: underline dotted #33AAFF")
    $(this).attr('title', 'Нажмите для рейтинга комментариев');
    element.click(function(event) {
      $('html, body').animate({ scrollTop: $(document).height() - $(window).height() }, 600, function() {
        $(document).scrollTop(0);
      });
    });
  });
});