Are you sure you want to go to an external site to donate a monetary value?
WARNING: Some countries laws may supersede the payment processors policy such as the GDPR and PayPal. While it is highly appreciated to donate, please check with your countries privacy and identity laws regarding privacy of information first. Use at your utmost discretion.
This way there is no reason to call start() each time language is changed.
Also dropdown does not hide when clicking the language option (my own preference, easily changed).
Now RegExps are built once at the script start.
Since you used let and didn't use any 'var tricks' I refactored the whole script to use let instead. While in the function scope perf difference is insignificant but it still there and for loops now should be faster. perf test in comments.
The script is licensed under MIT but i still have to ask, do you mind that I made it into a chrome extension?
// url, name, allowed page types bitmap(0 - blocked, 1 - hentai) let selections = [ ["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://online.anidub.com/index.php?do=search&subaction=search&search_start=1&full_search=0&result_from=1&story=", "AniDub", 1], ["https://video.sibnet.ru/search.php?panel=open&sortby=0&duration=0&inname=1&text=", "Sibnet", 3], ["http://www.animespirit.ru/index.php?do=search&story=", "animespirit", 1], ["http://desu.me/search/555/?o=date&c[title_only]=1&c[node]=20+22+21&q=", "Desu.me", 1] ]; let $ = jQuery.noConflict(true); let lang_cookie_regexp = new RegExp('(?:^|; )altWatcherLanguage=([^;]*)'); let service_cookie_regexp = [ new RegExp('(?:^|; )altWatcherPrefServiceFor1=([^;]*)'), new RegExp('(?:^|; )altWatcherPrefServiceFor2=([^;]*)') ]; function getPrefService(pageType) { let matches = document.cookie.match(service_cookie_regexp[pageType-1]); if (matches){ matches = decodeURIComponent(matches[1]); matches = selections.find(function(it){ return it[1] == matches; }); if (matches && matches[2] & pageType) return matches; } for (let idx = 0, v = selections[idx]; idx < selections.length; v = selections[++idx]) if ((v[2] & pageType) != 0) return v; return selections[0]; } function setPrefService(pageType, value) { let d = new Date(Date.now() + 666 * 1000); document.cookie = "altWatcherPrefServiceFor" + pageType + "=" + encodeURIComponent(value) + "; path=/; expires=" + d.toUTCString(); } function getAltWatcherLanguage() { let matches = document.cookie.match(lang_cookie_regexp); if (matches){ return decodeURIComponent(matches[1]); } return "en"; } function setAltWatcherLanguage(value) { let d = new Date(Date.now() + 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(); let pageType = (!!$('a.b-tag[href*="genre/12"]').length + 1) let animeName = $("#animes_show > section > div > header > h1").text().split(" / ") // [ru, en] let link = $('<a/>'); let bar = $('<div/>'); //pageType = (!!$('a.b-tag[href*="genre/12"]').length + 1), let barItemClicked = function (){ let service = selections[$(this).data('service-index')]; link.text('Смотреть на ' + service[1]); setPrefService(pageType, service[1]); bar.hide(); }; for (let idx = 0; idx < selections.length; idx++) { let v = selections[idx]; if ((v[2] & pageType) != 0) bar.append($('<a/>').addClass('b-link_button dark watch-online').text(v[1]).data('service-index', idx).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(ru.click(function() { ru.addClass("dark") en.removeClass("dark") setAltWatcherLanguage("ru") }))) .append($('<td/>').append(en.click(function(){ en.addClass("dark") ru.removeClass("dark") setAltWatcherLanguage("en") })))); bar.append($('<div/>').addClass('block watch-online').css({top: "5px"}).append(table)); let service = getPrefService(pageType); $(".watch-online-placeholer").append( $('<div/>').addClass('watch-online').append( link.addClass("b-link_button dark").css({float: 'left', width: "calc(100% - 31px)"}).append(link.click(function() { let service = getPrefService(pageType); let lang = getAltWatcherLanguage(); let win = window.open(service[0] + encodeURIComponent(animeName[lang === "en" ? 1 : 0]), '_blank'); win.focus(); })).text('Смотреть на ' + service[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);