Lolec / Alt Watcher v3

// ==UserScript==
// @name         Alt Watcher v3
// @namespace    https://vk.com/shiki_ex
// @version      2.2
// @description  [shikimori.org] Добавляет ссылку на Альтернативные сайты просмотра аниме
// @author       STorn.feat Lolec
// @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 (bit 0 - blocked, 1 - hentai)
var selections = [
    ["https://you-anime.ru/search?keyword=", "YA", 1],
    ["https://smotret-anime.ru/catalog/search?q=", "Anime 365", 1],
    ["https://hentai365.ru/catalog/search?q=", "Hentai 365", 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]
];

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 start(){
    if (window.location.pathname.indexOf("animes") && ($(".disabled").length || !$(".watch-online-placeholer").children().length)) {
        var animeName = $("#animes_show > section > div > header > h1").text().split(" / ")[1],
            link = $('<a target="_blank"/>'),
            bar = $('<div/>'),
            pageType = (!!$(".is-censored").length + 1),
            barItemClicked = function (){
                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));
        }

        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);