Advertisement
Gissh

custom BTTV v2

Dec 4th, 2020
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         anime2ru BTTV
  3. // @namespace    http://tampermonkey.net/
  4. // @version      2
  5. // @description  добавляет смайлы с BTTV на аниме2ру
  6. // @author       Gissh (&Александр)
  7. // @match        https://dota2.ru/forum/*
  8. // @grant        none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12.     'use strict';
  13.  
  14.     class BTTV {
  15.         constructor () {
  16.             this.api = 'https://api.betterttv.net/3/emotes/shared/';
  17.             this.cdn = 'https://cdn.betterttv.net/emote/';
  18.             this.smileLimit = 60;
  19.             this.default = 'offset=0&limit=60';
  20.             this.storage = 'bttv_FavoriteSmiles';
  21.             this.editorInsert = 'javascript:tinyMCE.activeEditor.insertContent(';
  22.             this.trumbowyg = false;
  23.             this.titlePreview = 'https://pbs.twimg.com/profile_images/571369694137819136/lst8DKUj_400x400.png';
  24.             this.smileOffsetSearch = 0;
  25.             this.smileOffsetPopular = 0;
  26.  
  27.             // Поддержка this kekw
  28.             this.init = this.init.bind(this);
  29.             this.insertFavorite = this.insertFavorite.bind(this);
  30.             this.request = this.request.bind(this);
  31.             this.watching = this.watching.bind(this);
  32.             this.generateUrl = this.generateUrl.bind(this);
  33.             this.getSearch = this.getSearch.bind(this);
  34.             this.getPopular = this.getPopular.bind(this);
  35.             this.getFavorite = this.getFavorite.bind(this);
  36.             this.generateMarkup = this.generateMarkup.bind(this);
  37.             this.generateSmileMarkup = this.generateSmileMarkup.bind(this);
  38.             this.generateTabMarkup = this.generateTabMarkup.bind(this);
  39.             this.generateContentMarkup = this.generateContentMarkup.bind(this);
  40.  
  41.             this.watching ({
  42.                 elem: 'div.smiles-panel ul.tabs',
  43.                 callback: this.init,
  44.                 bool: true
  45.             });
  46.         }
  47.  
  48.         init (el) {
  49.  
  50.             let tabs = $(el),
  51.                 contents = tabs.parents('.smiles-panel').first().find('.tabs-content'),
  52.                 trumbowyg = $(el).parents('.trumbowyg-box'), // helper trumbowyg
  53.                 parent = this;
  54.  
  55.             if (trumbowyg.length > 0) {
  56.                 this.editorInsert = `javascript:Tree.insertEmoji('#${trumbowyg.first().find('.trumbowyg-editor')[0].id}',`;
  57.                 this.trumbowyg = true;
  58.             }
  59.  
  60.             tabs.append(this.generateTabMarkup());
  61.             contents.append(this.generateContentMarkup());
  62.  
  63.             contents.on('click', '.bttv_search_btn', (event) => {
  64.  
  65.                 let currentBtnType = event.currentTarget.dataset.type;
  66.  
  67.                 if(currentBtnType == 'next'){
  68.                     this.smileOffsetSearch += 60;
  69.                 }else if (currentBtnType == 'prev' && this.smileOffsetSearch != 0){
  70.                     this.smileOffsetSearch -= 60;
  71.                 }else if(currentBtnType == 'search' || this.smileOffsetSearch == 0){
  72.                     this.smileOffsetSearch = 0;
  73.                 }
  74.  
  75.                 this.default = `offset=${this.smileOffsetSearch}&limit=60`;
  76.                 console.log(this.smileOffsetSearch);
  77.  
  78.                 let findContent = contents.find('#smile-cat-99').first();
  79.  
  80.                 this.getSearch(contents.find('#bttvsearch').val(), (data) => {
  81.                     findContent.find('.search-content').html(this.generateMarkup('search', data));
  82.                     findContent.find('.search-content .smile-content').on('contextmenu', function (e) {
  83.                         parent.insertFavorite(this, e);
  84.                     });
  85.                 });
  86.             });
  87.  
  88.  
  89.             contents.on('click', '.bttv_popular_btn', (event) => {
  90.  
  91.                 let currentBtnType = event.currentTarget.dataset.type;
  92.  
  93.                 if(currentBtnType == 'next'){
  94.                     this.smileOffsetPopular += 60;
  95.                 }else if (currentBtnType == 'prev' && this.smileOffsetPopular != 0){
  96.                     this.smileOffsetPopular -= 60;
  97.                 }else if(currentBtnType == 'search' || this.smileOffsetPopular == 0){
  98.                     this.smileOffsetPopular = 0;
  99.                 }
  100.  
  101.                 this.default = `offset=${this.smileOffsetPopular}&limit=60`;
  102.                 console.log(this.smileOffsetPopular);
  103.  
  104.                 let findContent = contents.find('#smile-cat-99').first();
  105.  
  106.                 this.getPopular((data) => {
  107.                     findContent.find('.popular-content').html(this.generateMarkup('popular', data));
  108.                     findContent.find('.popular-content .smile-content').on('contextmenu', function (e) {
  109.                         parent.insertFavorite(this, e);
  110.                     });
  111.                 });
  112.             });
  113.  
  114.  
  115.  
  116.             tabs.find('[href="#smile-cat-99"]').first().on('click', () => {
  117.                 let findContent = contents.find('#smile-cat-99').first();
  118.  
  119.                 this.getFavorite((data) => {
  120.                     findContent.find('.favorite-content').html(this.generateMarkup('favorite', data));
  121.                     findContent.find('.favorite-content .smile-content').on('contextmenu', function (e) {
  122.                         parent.removeFavorite(this, e);
  123.                     });
  124.                 });
  125.  
  126.                 this.getPopular((data) => {
  127.                     findContent.find('.popular-content').html(this.generateMarkup('popular', data));
  128.                     findContent.find('.popular-content .smile-content').on('contextmenu', function (e) {
  129.                         parent.insertFavorite(this, e);
  130.                     });
  131.                 });
  132.             });
  133.         }
  134.  
  135.  
  136.  
  137.         removeFavorite (item, event) {
  138.             event.stopPropagation();
  139.             event.preventDefault();
  140.  
  141.             if (item instanceof jQuery)
  142.                 item = item[0];
  143.  
  144.             const id = item.dataset.id,
  145.                 code = item.title,
  146.                 parent = this;
  147.  
  148.             let storage = JSON.parse(localStorage.getItem(this.storage)), flag = true;
  149.             if (storage === null)
  150.                 storage = [];
  151.  
  152.             storage.forEach((item, index) => {
  153.                 if (item.emote.id === id) {
  154.                     storage.splice(index, 1);
  155.                     flag = false;
  156.                 }
  157.             });
  158.  
  159.             if (flag)
  160.                 Utils.notify('Смайл удалён', 'success', 1000);
  161.             localStorage.setItem(this.storage, JSON.stringify(storage));
  162.  
  163.             let content = $(item).parents('#smile-cat-99').find('.favorite-content');
  164.  
  165.             content.html(this.generateMarkup('favorite', storage));
  166.             content.find('.smile-content').on('contextmenu', function (e) {
  167.                 parent.removeFavorite(this, e);
  168.             });
  169.         }
  170.  
  171.         insertFavorite (item, event) {
  172.             event.stopPropagation();
  173.             event.preventDefault();
  174.  
  175.             if (item instanceof jQuery)
  176.                 item = item[0];
  177.  
  178.             const id = item.dataset.id,
  179.                 code = item.title,
  180.                 parent = this;
  181.  
  182.             let storage = JSON.parse(localStorage.getItem(this.storage));
  183.  
  184.             if (storage === null)
  185.                 storage = [];
  186.  
  187.             storage.push({emote: {code: code, id: id}});
  188.             storage = this.removeDuplicates(storage);
  189.  
  190.             Utils.notify('Смайл добавлен', 'success', 1000);
  191.             localStorage.setItem(this.storage, JSON.stringify(storage));
  192.  
  193.             let content = $(item).parents('#smile-cat-99').find('.favorite-content');
  194.  
  195.             content.html(this.generateMarkup('favorite', storage));
  196.             content.find('.smile-content').on('contextmenu', function (e) {
  197.                 parent.removeFavorite(this, e);
  198.             });
  199.         }
  200.  
  201.         removeDuplicates (arr) {
  202.             let result = [];
  203.  
  204.             arr.forEach(item => {
  205.                 let flag = true;
  206.  
  207.                 result.forEach(jtem => {
  208.                     if (item.emote.id === jtem.emote.id)
  209.                         flag = false;
  210.                 });
  211.  
  212.                 if (flag)
  213.                     result.push(item);
  214.             });
  215.  
  216.             return result;
  217.         }
  218.  
  219.         request (generatedUrl = false, callback = () => {}) {
  220.             $.ajax({
  221.                 url: generatedUrl,
  222.                 type: "GET",
  223.                 success: callback
  224.             });
  225.         }
  226.  
  227.         watching ({doc, elem, callback, bool}) {
  228.             let interval = setInterval(() => {
  229.                 doc = (doc)? doc : document;
  230.  
  231.                 let el;
  232.                 if (el = doc.querySelector(`${elem}:not(.watched)`)) {
  233.                     callback(el);
  234.                     el.classList.add('watched');
  235.  
  236.                     if (!bool) clearInterval(interval);
  237.                 }
  238.             }, 100);
  239.         }
  240.  
  241.         generateUrl (type, data = null) {
  242.             let url;
  243.  
  244.             switch (type) {
  245.                 case 'search':
  246.                     url = 'search?query=' + data + '&';
  247.                     break;
  248.                 case 'popular':
  249.                 default:
  250.                     url = 'top?';
  251.             }
  252.             console.log(this.api + url + this.default);
  253.             return this.api + url + this.default;
  254.         }
  255.  
  256.         getSearch (str, callback) {
  257.             let response, url = this.generateUrl('search', str);
  258.             this.request(url, callback);
  259.         }
  260.  
  261.         getPopular (callback) {
  262.             let response, url = this.generateUrl();
  263.             this.request(url, callback);
  264.         }
  265.  
  266.         getFavorite (callback) {
  267.             let storage = JSON.parse(localStorage.getItem(this.storage));
  268.             callback(storage);
  269.             return
  270.         }
  271.  
  272.         generateMarkup (type, array = null) {
  273.             let body = $(`<div class="${type}-content"></div>`);
  274.  
  275.             switch (type) {
  276.                 case 'search':
  277.                     let bttvSearchVal = '';
  278.                     if ($('#bttvsearch').val() != undefined){
  279.                         bttvSearchVal = $('#bttvsearch').val();
  280.                     }
  281.                     body.append(`<p style="padding: 0.5em;">
  282.                         <input id="bttvsearch" class="text-ctrl" placeholder="Название смайлика" value="` + bttvSearchVal + `">
  283.                         <a class="button-theme bttv_search_btn" data-type="search">Поиск</a>
  284.                         <a class="button-theme bttv_search_btn" data-type="prev">Назад</a>
  285.                         <a class="button-theme bttv_search_btn" data-type="next">Вперед</a>
  286.                     </p>`);
  287.                     break;
  288.                 case 'favorite':
  289.                     body.append(`<p style="padding: 0.5em;">Избранные</p>`);
  290.                     break;
  291.                 case 'popular':
  292.                 default:
  293.                     body.append(`<p style="padding: 0.5em;">Самые популярные
  294.                         <a class="button-theme bttv_popular_btn" data-type="prev">Назад</a>
  295.                         <a class="button-theme bttv_popular_btn" data-type="next">Вперед</a>
  296.                 </p>`);
  297.             }
  298.  
  299.             if (array != null && typeof array[Symbol.iterator] === 'function' && array.length > 0)
  300.             for (let smile of array) {
  301.                 let id = type === 'search' ? smile.id : smile.emote.id,
  302.                     code = type === 'search' ? smile.code : smile.emote.code;
  303.  
  304.                 let href = this.generateSmileUrl(id, code),
  305.                     smileMarkup = this.generateSmileMarkup(id, code),
  306.                     el = $(`<div title="${code}" data-id="${id}" style="display: inline-block; margin: 3px;" class="smile-content">
  307.                         <a href="${this.editorInsert}\`${href}\`)">${smileMarkup}</a>
  308.                     </div>`);
  309.  
  310.                 body.append(el);
  311.             }
  312.  
  313.             return body[0].outerHTML;
  314.         }
  315.  
  316.         generateDefaultMarkup () {
  317.             const
  318.                 search = this.generateMarkup('search'),
  319.                 favorite = this.generateMarkup('favorite'),
  320.                 popular = this.generateMarkup('popular');
  321.  
  322.             return [search, favorite, popular].join('');
  323.         }
  324.  
  325.         generateSmileUrl (id, title) {
  326.             if (this.trumbowyg)
  327.                 return `:${title}:\`, \`${this.cdn}${id}/1x`;
  328.  
  329.             return this.generateSmileMarkup(id, title);
  330.         }
  331.  
  332.         generateSmileMarkup (id, title = 'bttv') {
  333.             return `<img width='32' height='32' style='max-width: 32px; max-height: 32px;' class='bttvimg' src='${this.cdn}${id}/2x' title='${title}'>`;
  334.         }
  335.  
  336.         generateTabMarkup () {
  337.             return `<li class="tab-title">
  338.                 <a href="#smile-cat-99" style="padding: 3px 10px;" title="BTTV" data-cat="99">
  339.                     <img data-cat="99" src="${this.titlePreview}" style="width: 24px; vertical-align: middle;">
  340.                 </a>
  341.             </li>`;
  342.         }
  343.  
  344.         generateContentMarkup () {
  345.             return `<div class="content" id="smile-cat-99">
  346.                 ${this.generateDefaultMarkup()}
  347.             </div>`;
  348.         }
  349.     }
  350.  
  351.     $(document).on('load', new BTTV());
  352. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement