imgolagil / Alt Watcher v2

// ==UserScript==
// @name         Alt Watcher v2
// @namespace    https://vk.com/shiki_ex
// @version      2.1.1
// @description  [shikimori.org] Добавляет ссылку на Альтернативные сайты просмотра аниме ###
// @author       STorn
// @match https://shikimori.org/*
// @match http://shikimori.org/*
// @match https://shikimori.one/*
// @match http://shikimori.one/*
// @require      https://code.jquery.com/jquery-3.3.1.slim.min.js
// @grant        none
// @license      MIT
// @copyright    2018, Newt300 (https://openuserjs.org/users/Newt300)
// ==/UserScript==

// url, name, allowed page types bitmap(0 - blocked, 1 - hentai)
var selections = [
    ["https://smotret-anime.ru/catalog/search?q=", "Anime 365", 1],
    ["https://hentai365.ru/catalog/search?q=", "Hentai 365", 2],
    ["https://hentaihaven.org/search/", "HentaiHaven", 2],
    ["http://henchan.me/?do=search&subaction=search&story=", "Hentaichan", 2],
    ["https://www.xvideos.com/?k=", "100% Вариант", 2],
    ["https://vk.com/video?q=", "VK", 3],
    ["https://sovetromantica.com/anime?query=", "SR", 1],
    ["https://video.sibnet.ru/search.php?panel=open&sortby=0&duration=0&inname=1&text=", "Sib", 1],
    ["https://online.anidub.com/index.php?do=search&subaction=search&search_start=1&full_search=0&result_from=1&story=", "AniDub", 1],
    ["https://animerost.org/index.php?do=search&subaction=search&search_start=0&full_search=0&result_from=1&story=", "Animerost", 1],
    ["http://animespirit.su/index.php?do=search&subaction=search&search_start=1&full_search=0&result_from=1&result_num=20&story=", "Spirit", 1],
    ["https://rutracker.org/forum/tracker.php?nm=", "Rutracker", 1],
    ["https://www.myvi.tv/search/video?q=", "Myvi", 1],
    ["http://desu.me/search/search?keywords=", "Desu.me", 1],
    ["https://yummyanime.club/search?_token=dzYw0AzOStQWnLEewUPhGhtV8uG0wMn1zJo8xYia&word=", "YummyAnime", 1],
    ["http://adultmult.ru/search.html?searchid=2294179&text=", "AdultMult", 1],
    ["https://aniplay.tv/index.php?do=search&subaction=search&story=", "AniPlay", 1],
    ["https://yandere.top/?do=search&subaction=search&story=", "Yandere.tv", 1],
    ["http://anitokyo.tv/index.php?do=search&subaction=search&story=", "AniTokyo", 1],
    ["https://animelend.info/index.php?do=search&subaction=search&search_start=1&full_search=1&result_from=1&story=", "AnimeLend", 1],
    ["https://risens.team/index.php?do=search&subaction=search&search_start=0&full_search=0&result_from=1&story=", "Risens", 1],
    ["https://anistar.org/index.php?do=search&subaction=search&search_start=1&full_search=0&result_from=1&story=", "Anistar", 1],
    ["https://animebest.org/?do=search&subaction=search&story=", "AnimeBest", 1],
    ["https://animedub.ru/index.php?do=search&subaction=search&search_start=1&full_search=0&result_from=1&story=", "Animedub", 1],
    ["https://animaunt.tv/index.php?do=search&subaction=search&search_start=1&full_search=0&result_from=1&story=", "Animaunt", 1],
    ["https://www.youtube.com/results?search_query=", "Для клипов", 1]
];

var $ = jQuery.noConflict(true);

function getPrefService(pageType) {
    var matches = document.cookie.match(new RegExp('(?:^|; )altWatcherPrefServiceFor' + pageType + '=([^;]*)'));
    if (matches){
        matches = decodeURIComponent(matches[1]);
        matches = selections.find(function(it){
            return it[1] == matches;
        });
        if (matches && matches[2] & pageType) return  matches;
    }

    for (var i = 0, v = selections[i]; i < selections.length; v = selections[++i])
        if ((v[2] & pageType) != 0)
            return v;

    return selections[0];
}

function setPrefService(pageType, value) {
    var d = new Date();
    d.setTime(d.getTime() + 666 * 1000);
    document.cookie = "altWatcherPrefServiceFor" + pageType + "=" + encodeURIComponent(value) + "; path=/; expires=" + d.toUTCString();
}

function getAltWatcherLanguage() {
    var matches = document.cookie.match(new RegExp('(?:^|; )altWatcherLanguage=([^;]*)'));
    if (matches){
        matches = decodeURIComponent(matches[1]);
        return matches;
    }
    return "en";
}

function setAltWatcherLanguage(value) {
    var d = new Date();
    d.setTime(d.getTime() + 666 * 1000);
    document.cookie = "altWatcherLanguage=" + encodeURIComponent(value) + "; path=/; expires=" + d.toUTCString();
}

function start(){
    // if (window.location.pathname.indexOf("animes") && ($(".disabled").length || !$(".watch-online-placeholer").children().length)) {
        let lang = getAltWatcherLanguage();
        var animeName = $("#animes_show > section > div > header > h1").text().split(" / ")[lang === "en" ? 1 : 0],
            link = $('<a target="_blank"/>'),
            bar = $('<div/>'),
            pageType = (!!$('a.b-tag[href*="genre/12"]').length + 1),
            barItemClicked = function (){
                let lang = getAltWatcherLanguage();
                let animeName = $("#animes_show > section > div > header > h1").text().split(" / ")[lang === "en" ? 1 : 0];
                var i = selections[$(this).data('service-index')];
                link.attr('href', i[0] + animeName).text('Смотреть на ' + i[1]);
                setPrefService(pageType, i[1]);
                bar.hide();
            };

        for (var i = 0; i < selections.length; i++) {
            var v = selections[i];
            if ((v[2] & pageType) != 0)
                bar.append($('<a/>').addClass('b-link_button dark watch-online').text(v[1]).data('service-index', i).click(barItemClicked));
        }

        let en = $('<a>').addClass(lang === "en" ? "b-link_button dark" : "b-link_button").text('en').attr('title', "Искать по английскому названию");
        let ru = $('<a>').addClass(lang === "ru" ? "b-link_button dark" : "b-link_button").text('ru').attr('title', "Искать по русскому названию");
        let table = $('<table width="100%"/>').append($('<tr/>')
                                                       .append($('<td/>').append(en.click(function(){
                                                            en.addClass("dark")
                                                            ru.removeClass("dark")
                                                            setAltWatcherLanguage("en")
                                                            start();
                                                        })))
                                                       .append($('<td/>').append(ru.click(function() {
                                                            ru.addClass("dark")
                                                            en.removeClass("dark")
                                                            setAltWatcherLanguage("ru")
                                                            start();
                                                       }))));

        bar.append($('<div/>').addClass('block watch-online').css({top: "5px"}).append(table));


        i = getPrefService(pageType);
        $(".watch-online-placeholer").empty().append(
            $('<div/>').addClass('watch-online').append(
                link.addClass("b-link_button dark").css({float: 'left', width: "calc(100% - 31px)"})
                .attr('href', i[0] + animeName).text('Смотреть на ' + i[1])
            ).append(
                $('<a/>').addClass("b-link_button dark").css({width: "31px", float: 'right', heigth: '31px'}).text('▼').click(function(){
                    bar.toggle();
                })
            ).append($('<div/>').addClass('clearfix'))
        ).append(bar.addClass('block').hide());
    
}

$(document).ready(start);
$(document).on('page:load', start);
$(document).on('turbolinks:load', start);