ČSFD Compare

Show your own ratings on other users ratings list

目前為 2021-05-24 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         ČSFD Compare
// @version      0.3.1
// @namespace    csfd.cz
// @description  Show your own ratings on other users ratings list
// @author       Jan Verner <[email protected]>
// @icon         http://img.csfd.cz/assets/b1733/images/apple_touch_icon.png
// @require      https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js
// @include      *csfd.cz/uzivatel/*/hodnoceni*
// @include      *csfd.cz/film/*
// ==/UserScript==


(() => {
    "use strict";
    /* globals jQuery, $, waitForKeyElements */

    // var __webpack_exports__ = {};

    class Csfd {

        constructor(csfdPage) {
            this.csfdPage = csfdPage;
            this.stars = {};
            // this.stars['jedna'] = 1;
            this.storageKey = undefined;
            this.userUrl = undefined;

            // Ignore the ads... Make the table wider.
            $('.column.column-80').attr('class', '.column column-90');

            // $('.star-rating .stars::after').css('color', 'black');
            // document.styleSheets[0].removeRule('.star-rating .stars:after');
            // document.styleSheets[0].insertRule('.star-rating .stars:after', 'color: green');
            // $('.star-rating .stars:after').attr('style', 'color: black');
        }

        getCurrentUser() {
            let loggedInUser = $('.profile.initialized').attr('href');

            if (loggedInUser == null) {

                throw ("CSFD Compare: You are not logged in... So you can't compare ratings with yours...");  // TODO: Popup informing user
            }
            return loggedInUser;
        }

        forceRefreshCurrentUserRatings() {
            this.refreshCurrentUserRatings();
        }

        getCurrentFilmUrl() {
            let navItemHref = this.csfdPage.find('.tab-nav-item-1 > a').attr('href');

            let foundMatch = navItemHref.match(new RegExp("film/" + "(.*)" + "/prehled"));
            if (foundMatch == null) {
                console.log("TODO: nenaslo to... vyhledat jinym zpusobem!");
            }

            let filmUrl = `/film/${foundMatch[1]}/`;
            return filmUrl;
        }

        updateInLocalStorage(ratingNum) {
            console.log("Checking if in LocalStorage...");

            // Check if film is in LocalStorage
            let filmUrl = this.getCurrentFilmUrl();
            let item = this.stars[filmUrl];

            // Item not in LocalStorage, add it then!
            if (item === undefined) {
                console.log(`Item not in LocalStorage, adding... ${filmUrl}: ${ratingNum}`);
                this.stars[filmUrl] = ratingNum;
                localStorage.setItem(this.storageKey, JSON.stringify(this.stars));
                return true;
            }

            if (item != ratingNum) {
                console.log(`LocalStorage rating [${item}] != current rating [${ratingNum}], updating...`);
                this.stars[filmUrl] = ratingNum;
                localStorage.setItem(this.storageKey, JSON.stringify(this.stars));
                return true;
            }

            console.log(`Rating same in LocalStorage, everything ok`);
            return true;
        }

        removeFromLocalStorage() {
            console.log("Deleting item from LocalStorage...");

            // Check if film is in LocalStorage
            let filmUrl = this.getCurrentFilmUrl();
            let item = this.stars[filmUrl];

            // Item not in LocalStorage, everything is fine
            if (item === undefined) {
                console.log("Item not in LocalStorage, nothing happens");
                return null;
            }

            // Item in LocalStorage, delete it from local dc
            delete this.stars[filmUrl];

            // And resave it to LocalStorage
            console.log("Resaving ratings into LocalStore");
            localStorage.setItem(this.storageKey, JSON.stringify(this.stars));

            return true;
        }

        checkLocalStorageRatings() {
            //TODO: Duplicitni, nejak to sloucit...
            console.log("LOADING RATINGS...");

            this.userUrl = this.getCurrentUser();
            console.log("this.userUrl:", this.userUrl);

            this.storageKey = "CsfdCompare_" + this.userUrl.split("/")[2].split("-")[1];
            console.log("this.storageKey", this.storageKey);

            // Try cache
            if (localStorage[this.storageKey]) {
                this.stars = JSON.parse(localStorage[this.storageKey]);
            }

            // Cache does not exists...
            if (Object.keys(this.stars).length == 0) {
                // TODO: vyresit, pridat tlacitko, upozorneni float
                alert("Načítam hodnocení. Potvrďte a POČTEJTE na další vyskakovací okno");
                console.log("CACHE NOT EXISTING MAN.... LOAD???");

                // TODO: Add floating pregress notification {loading... 354/2049}
                this.refresh();
            }

        }

        getCurrentFilmRating() {
            let $currentUserRating = this.csfdPage.find('.current-user-rating .stars');

            // Exit if no user rating found
            if ($currentUserRating.length == 0) {
                return null;
            }

            // Ignore 'computed' ratings (exit)
            if ($currentUserRating.parent().hasClass('computed')) {
                console.log("Smula, computed....");
                return null;
            }

            // Find the rating
            for (let num = 0; num <= 5; num++) {
                if ($currentUserRating.hasClass(`stars-${num}`)) {
                    return num;
                }
            }

            // If not numeric rating, return 0 (Odpad!)
            if ($currentUserRating.find('.trash').length > 0) {
                return 0;
            }
        }

        refreshCurrentUserRatings() {
            console.log("LOADING RATINGS...");

            this.userUrl = this.getCurrentUser();
            console.log("this.userUrl:", this.userUrl);

            this.storageKey = "CsfdCompare_" + this.userUrl.split("/")[2].split("-")[1];
            console.log("this.storageKey", this.storageKey);

            // Try cache
            if (localStorage[this.storageKey]) {
                this.stars = JSON.parse(localStorage[this.storageKey]);
            }

            // Cache does not exists...
            if (Object.keys(this.stars).length == 0) {
                // TODO: vyresit, pridat tlacitko, upozorneni float
                alert("Načítam hodnocení. Potvrďte a POČTEJTE na další vyskakovací okno");
                console.log("CACHE NOT EXISTING MAN.... LOAD???");

                // TODO: Add floating pregress notification {loading... 354/2049}
                this.refresh();
            }
            // Cache exists
            else {
                if (!location.href.includes(this.userUrl)) {

                    // TODO: Check if {goto users hodnoceni, see MAX page * 50, check length, if less, propose update}
                    this.addRatingsColumn();
                }
            }
        }

        refresh() {
            let url = this.userUrl + "hodnoceni/";
            console.log(`REFRESHING... ${url}`);
            this.loadHodnoceniPage(url);

        }

        exportRatings() {
            console.log("Settings this.stars --> localStorage");
            localStorage.setItem(this.storageKey, JSON.stringify(this.stars));
        }

        importRatings() {
            if (localStorage[this.storageKey]) {
                this.stars = JSON.parse(localStorage[this.storageKey]);
            }
        }

        loadHodnoceniPage(url) {
            console.log(`LOADING URL... ${url}`);
            this.currentRequest = $.ajax({
                type: "GET",
                url: url,
                async: true
            });

            this.currentRequest.done((data) => {
                this.loadPageDone(data);
            });
        }

        // push(filmURL, filmRating) {
        //     this.stars[filmURL] = filmRating;
        // }

        loadPageDone(hodnoceniHTML) {
            if (!hodnoceniHTML) {
                return;
            }

            var $stars = this.stars;
            $(hodnoceniHTML).find("tbody tr").each(function () {
                var $row = $(this);
                var filmURL = $("a.film-title-name", $row).attr("href");
                var $rating = $("span .stars", $row);

                let starsRating = 0;
                for (let stars = 0; stars <= 5; stars++) {
                    if ($rating.hasClass('stars-' + stars)) {
                        starsRating = stars;
                    }
                }

                $stars[filmURL] = starsRating;
            });

            let nextPaginationURL = $(hodnoceniHTML).find("a.page-next").attr("href");
            if (nextPaginationURL) {
                this.loadHodnoceniPage(nextPaginationURL);
            } else {
                this.finishRefresh();
            }
        }

        addRatingsColumn() {
            let $page = this.csfdPage;

            let $tbl = $page.find('#snippet--ratings table tbody');
            let starsDict = this.stars;

            $tbl.find('tr').each(function () {
                let $row = $(this);
                let url = $($row).find('.name').find('a').attr('href');
                let ratingNum = starsDict[url];

                let $span = "";
                if (ratingNum == 0) {
                    $span = `<span class="stars trash">odpad!</span>`;
                }
                else {
                    $span = `<span class="stars stars-${ratingNum}"></span>`;
                }

                $row.find('td:nth-child(2)').after(`
                    <td class="star-rating-only">
                        <span class="star-rating">
                            ${$span}
                        </span>
                    </td>
                `);
            });
        }

        finishRefresh() {
            this.exportRatings();
            console.log("Hotovo, hodnocení načteno");
            alert(`
                Hotovo, hodnoceni nacteno. \n
                Prozatim neni mozno jeho opetovne spusteni. \n
                Musite: F12 > Application > Local Storage > csfd.cz > CsfdCompare_vasNick > delete
            `);

            if (!location.href.includes(this.userUrl)) {
                this.addRatingsColumn();
            }
        }

    }

    // SCRIPT START
    let csfd = new Csfd($('div.page-content'));

    if (location.href.includes('/film/')) {
        console.log("Jsem na filmu...");
        csfd.checkLocalStorageRatings();
        let currentRatingNum = csfd.getCurrentFilmRating();
        if (currentRatingNum == null) {
            // Check if record exists, if yes, remove it
            csfd.removeFromLocalStorage();
            return;
        }
        // Check if current page rating corresponds with that in LocalStorage, if not, update it
        csfd.updateInLocalStorage(currentRatingNum);
    }
    else if (location.href.includes('/uzivatel/')) {
        // csfd.forceRefreshCurrentUserRatings();
        csfd.refreshCurrentUserRatings();
    }

})();