Greasy Fork

Greasy Fork is available in English.

RYM - Charts - Highlight rated albums

Highlights albums on RYM

当前为 2015-08-11 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        RYM - Charts - Highlight rated albums
// @namespace   rateyourmusic
// @include     http*://rateyourmusic.com/*
// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js
// @require     https://cdnjs.cloudflare.com/ajax/libs/jquery-csv/0.71/jquery.csv-0.71.min.js
// @version     1
// @description Highlights albums on RYM
// ==/UserScript==

/**
// @include     http*://rateyourmusic.com/charts/*
// @include     http*://rateyourmusic.com/customchart*
// @include     http*://rateyourmusic.com/list*
// @include     http*://rateyourmusic.com/release*
 */

GM_addStyle(".custom_album_rated { background-color: lightgreen !important; } " + 
            ".custom_album_wl { background-color: yellow !important; } " + 
            ".custom_album_owned { background-color: orange !important; }");

function reset_rym_id() {
    var uid = GM_getValue("rym_uid");
    window.prompt("Reset RYM User ID (" + uid + ")");
    Ratings.download_ratings();
    Highlighter.highlight();
}

var Config = {
    export_url: function(uid) {
        return "https://rateyourmusic.com/user_albums_export?album_list_id=[uid]&noreview".replace("[uid]", uid);
    }
};

var Ratings = {
    ratings: [],    
    wishlist: [],
    owned: [], // ...but not rated
    download_ratings: function() {        
        var uid = GM_getValue("rym_uid");
        console.log("Downloading ratings: " + Config.export_url(uid));
        jQuery.get(Config.export_url(uid), function(data) {
            console.log("Downloaded ratings");
            var result = data.split("\n");
            
            Ratings.ratings = [];
            Ratings.wishlist = [];
            Ratings.owned = [];
            for(var i = 1; i < result.length; ++i) {
                var cur = result[i];
                var review_removed = cur.substr(0, cur.length-5);
                var csv = $.csv.toArray(review_removed);
                
                var rated = csv[7] !== "0";
                if(rated) {
                    Ratings.ratings.push(csv[0]);
                }
                else {
                    // owned or wishlisted
                    var wishlisted = csv[8] === "w";
                    if(!wishlisted) {
                        Ratings.owned.push(csv[0]);
                    }
                    else {
                        Ratings.wishlist.push(csv[0]);
                    }    
                }
                
                /*var not_wishlist = csv[8] !== "w";
                if(not_wishlist) {
                    var rated = csv[7] !== "0";
                    console.log(csv[7] + " " + rated + " " + csv[5]);
                    if(rated) {
                        Ratings.ratings.push(csv[0]);
                    }
                    else {
                        // owned, but not rated
                        Ratings.owned.push(csv[0]);
                    }
                }
                else {
                    Ratings.wishlist.push(csv[0]);
                }*/
                
                
            }
            Ratings.save();
            Highlighter.highlight();
        });
    },
    load: function() {
        var uid = GM_getValue("rym_uid");
        if(uid === undefined) {
            alert("Missing RYM ID!");
        }
        
        var tmp_ratings = GM_getValue("rym_ratings");
        var tmp_wl = GM_getValue("rym_wishlist");
        var tmp_owned = GM_getValue("rym_owned");
        if(tmp_ratings !== undefined) {
            Ratings.ratings = eval(tmp_ratings);
        }
        if(tmp_wl !== undefined) {
            Ratings.wishlist = eval(tmp_wl);
        }
        if(tmp_owned !== undefined) {
            Ratings.owned = eval(tmp_owned);
        }
    },
    save: function() {
        GM_setValue("rym_ratings", uneval(Ratings.ratings));
        GM_setValue("rym_wishlist", uneval(Ratings.wishlist));
        GM_setValue("rym_owned", uneval(Ratings.owned));
    },
    contains: function(album_id, type="r") {
        var types = {r: Ratings.ratings, w: Ratings.wishlist, o: Ratings.owned};
        if(types.hasOwnProperty(type)) {
            var arr = types[type];
            for(var i = 0; i < arr.length; ++i) {
                if(arr[i] == album_id) {
                    return true;
                }
            }
        }    
        return false;
    }
};

var Highlighter = {
    highlight: function() {
        console.log("Highlighting");
        $albums = jQuery("a.album");
        if($albums.length == 0) {
            $albums = jQuery("a.list_album");
        }
        
        $albums.each(function(index) {
            $t = jQuery(this);
            $t.removeClass("custom_album_rated").removeClass("custom_album_wl").removeClass("custom_album_owned");
            var album_id = $t.attr("title").match(/\[Album([0-9]+)\]/i)[1]; 
            if(Ratings.contains(album_id, "r")) {
                $t.addClass("custom_album_rated");
            }
            else if(Ratings.contains(album_id, "w")) {
                $t.addClass("custom_album_wl");
            }
            else if(Ratings.contains(album_id, "o")) {
                $t.addClass("custom_album_owned");
            }
        });
    }
};

var App = {
    run: function() {
        Ratings.load();
        Highlighter.highlight();
    }
};        

GM_registerMenuCommand("Reload RYM Ratings", Ratings.download_ratings);
GM_registerMenuCommand("Reset RYM User ID", reset_rym_id);                

jQuery(document).ready(App.run);