ČSFD Compare

Show your own ratings on other users ratings list

当前为 2021-05-24 提交的版本,查看 最新版本

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

})();