joshu / JPOPSUKI Language Switch

// ==UserScript==
// @name         JPOPSUKI Language Switch
// @version      0.2.2
// @description  将jpopsuki的标题文字默认显示语言改为原文
// @author       joshu
// @match        https://jpopsuki.eu/torrents.php
// @match        https://jpopsuki.eu/torrents.php?*
// @match        https://jpopsuki.eu/artist.php?id=*
// @exclude      https://jpopsuki.eu/torrents.php?id=*
// @updateURL    https://openuserjs.org/meta/joshu/JPOPSUKI_Language_Switch.meta.js
// @require      https://code.jquery.com/jquery-3.2.1.min.js
// @grant        GM_log
// ==/UserScript==

var getTitle = function(word) {
    if (word.startsWith(" - ") && word.endsWith(")")) {
        return word.substr(3,word.length-4);
    }
    return word;
};

var ModifyTorrentList = function(enableAjax) {

    const STR_ARTIST_END = " (View Artist)";
    const STR_TORRENT_END = " (View Torrent)";

    const GROUP_SELECTOR = "tr[class='group_redline']";
    const TORRENT_SELECTOR = "tr[class='torrent_redline']";

    const GROUP_CLASS = "group_redline";

    const G_ARTIST_SELECTOR = "td:nth-child(4) > a:nth-child(1)";
    const G_TITLE_SELECTOR = "td:nth-child(4) > a:nth-child(2)";
    const T_ARTIST_SELECTOR = "td:nth-child(4) > a:nth-child(2)";
    const T_TITLE_SELECTOR = "td:nth-child(4) > a:nth-child(3)";

    const TORRENT_TITLE_SELECTOR = "#content > div > h3";

    var moTable = document.querySelector('#ajax_torrents');

    var moOption = {
        'childList': true,
        'arrtibutes': true,
        'characterData': true,
        'subtree': true
    };

    var ajaxCounter = 0;
    var mo;

    var ReplaceWithInfoInPage = function() {

        var ARTIST_SELECTOR = T_ARTIST_SELECTOR;
        var TITLE_SELECTOR = T_TITLE_SELECTOR;
        if ($(this).attr("class")==GROUP_CLASS){
            ARTIST_SELECTOR = G_ARTIST_SELECTOR;
            TITLE_SELECTOR = G_TITLE_SELECTOR;
        }

        var artistOrigin = $(this).find(ARTIST_SELECTOR).attr("title");
        var artistShown =  $(this).find(ARTIST_SELECTOR).text();

        var titleOrigin = $(this).find(TITLE_SELECTOR).attr("title");
        var titleShown =  $(this).find(TITLE_SELECTOR).text();

        // GM_log([artistOrigin, artistShown, titleOrigin, titleShown].join(", "));

        // 有个神奇的见鬼分类叫做Picture
        if (!artistOrigin||!titleOrigin) {
            return;
        }

        if (artistOrigin.endsWith(STR_ARTIST_END)) {
            $(this).find(ARTIST_SELECTOR).attr("title", artistShown + STR_ARTIST_END);
            $(this).find(ARTIST_SELECTOR).text(artistOrigin.replace(STR_ARTIST_END, ""));
        }

        if (titleOrigin.endsWith(STR_TORRENT_END)) {
            $(this).find(TITLE_SELECTOR).attr("title", titleShown + STR_TORRENT_END);
            $(this).find(TITLE_SELECTOR).text(titleOrigin.replace(STR_TORRENT_END, ""));
        }
    };

    var postAjax = function() {
        ajaxCounter--;
        if (ajaxCounter===0) {
            mo.observe(moTable, moOption);
        }
    };

    var ReplaceUsingAJAX = function() {

        var ARTIST_SELECTOR = T_ARTIST_SELECTOR;
        var TITLE_SELECTOR = T_TITLE_SELECTOR;

        if ($(this).attr("class")==GROUP_CLASS){
            ARTIST_SELECTOR = G_ARTIST_SELECTOR;
            TITLE_SELECTOR = G_TITLE_SELECTOR;
        }

        var artistNode = $(this).find(ARTIST_SELECTOR);
        var artistOrigin = artistNode.attr("title");
        var artistShown =  artistNode.text();

        var titleNode = $(this).find(TITLE_SELECTOR);
        var titleOrigin = titleNode.attr("title");
        var titleShown =  titleNode.text();

        // 有个神奇的见鬼分类叫做Picture
        if (!artistOrigin||!titleOrigin) {
            postAjax();
            return;
        }

        var ArtEnd = artistOrigin.endsWith(STR_ARTIST_END);
        var TorEnd = titleOrigin.endsWith(STR_TORRENT_END);
        if (ArtEnd||TorEnd) {
            var torrentUrl = $(this).find(TITLE_SELECTOR)[0].href;
            $.ajax({
                url: torrentUrl,
                async: true,
                success: function(data){
                    var htmlData = $.parseHTML(data);
                    var content = $(TORRENT_TITLE_SELECTOR, $(htmlData));
                    if (!!content[0]) {
                        // GM_log(content);
                        var artist_n = content.find("a").text();
                        var title_n = content[0].childNodes[2].data;
                        if (ArtEnd) {
                            artistNode.attr("title", artistShown + STR_ARTIST_END);
                            artistNode.text(artist_n);
                        }
                        if (TorEnd) {
                            titleNode.attr("title", titleShown + STR_TORRENT_END);
                            titleNode.text(getTitle(title_n));
                        }
                    }
                    postAjax();
                },
                error: function() {
                    postAjax();
                }
            });
        } else {
            postAjax();
        }
    };

    var ModifyFunct = function(){

        if (enableAjax) {
            ajaxCounter=$(GROUP_SELECTOR).length+$(TORRENT_SELECTOR).length;
        }

        $(GROUP_SELECTOR).each(ReplaceFunct);
        $(TORRENT_SELECTOR).each(ReplaceFunct);
    };

    var InitMutationObserver = function() {
        var MutationObserver = window.MutationObserver ||
            window.WebKitMutationObserver ||
            window.MozMutationObserver;

        if (!MutationObserver) {
            return;
        }

        var callback = function() {
            mo.disconnect();
            ModifyFunct();
            if (!enableAjax) {
                mo.observe(moTable, moOption);
            }
        };

        mo = new MutationObserver(callback);

        if (!enableAjax) {
            mo.observe(moTable, moOption);
        }
    };

    var ReplaceFunct = ReplaceWithInfoInPage;

    if (enableAjax) {
        ReplaceFunct = ReplaceUsingAJAX;
    }

    ModifyFunct();
    InitMutationObserver();
};

var ModifyArtistPage = function() {

    var ALBUM_SELECTOR = "tr.group > td > strong > a:nth-child(1)";
    var TITLE_SELECTOR = "#content > div > h3";

    var ModifyAlbumTitle = function() {
        var node = $(this)[0];
        var torrentUrl = node.href;
        $.ajax({
            url: torrentUrl,
            success: function(data){
                var htmlData = $.parseHTML(data);
                var content = $(TITLE_SELECTOR, $(htmlData))[0];
                if (!!content) {
                    node.text = getTitle(content.childNodes[2].data);
                }
            }
        });
    };

    $(ALBUM_SELECTOR).each(ModifyAlbumTitle);
};

(function() {
    'use strict';

    var url = window.location.pathname;
    var para = window.location.search;

    const TORRENT_LIST_URL = /^\/torrents\.php$/i;
    const TORRENT_LIST_PARA = /^\?.*page=\d+.*$|^$/i;
    const ARTIST_URL = /^\/artist\.php$/i;
    const ARTIST_PARA = /^\?id=\d+$/i;

    const ENABLE_AJAX_ON_TORRENT_LIST = false;

    if (url.match(TORRENT_LIST_URL)) {
        ModifyTorrentList(ENABLE_AJAX_ON_TORRENT_LIST);
    }

    if (url.match(ARTIST_URL)&&para.match(ARTIST_PARA)) {
        ModifyArtistPage();
    }
})();