SearchPredict for Trakt

SearchPredict for Trakt.TV

// ==UserScript==
// @name         SearchPredict for Trakt
// @namespace    https://pizidavi.altervista.org/
// @description  SearchPredict for Trakt.TV
// @author       pizidavi
// @version      1.2.2
// @copyright    2021, PIZIDAVI
// @license      MIT
// @match        https://trakt.tv/*
// @connect      api.trakt.tv
// @connect      api.themoviedb.org
// @run-at       document-end
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function($) {
    'use strict';

    const TEMPLATE = '<div class="predictions"></div>';
    const TEMPLATE_ITEM = '<a class="prediction-item" target="_self" onclick="event.preventDefault(); redirect(this.href); return false;" href=""> <img class="prediction-item-poster" src="#"> <div class="prediction-item-text"> <span class="prediction-item-type"></span></div> </a>';
    const CSS = '#header-search .predictions { display: none; background: rgba(0,0,0,0.93); padding: 0.3em 0; margin-left: -1px; border-bottom-right-radius: 3px; } #header-search.open .predictions { display: block; } /* Item */ .prediction-item { display: block; height: 52px; white-space: nowrap; border-top: 1px solid rgb(102, 102, 102); padding: 6px 12px 6px 12px; overflow: hidden; text-decoration: none !important; } .prediction-item:first-child { border-top: none; } .prediction-item > * { display: inline-block; vertical-align: middle; } /* Image */ .prediction-item-poster { width: 27px; height: 100%; margin-right: 8px; opacity: 0; transition: opacity 0.2s linear 0s; } /* Text */ .prediction-item .prediction-item-text { width: calc(100% - 42px); white-space: nowrap; text-overflow: ellipsis; overflow: hidden; } /* Type */ .prediction-item-type { background-color: rgb(237, 28, 36); color: rgb(255, 255, 255); font-size: 11px; margin-right: 6px; text-align: center; width: 35px; padding: 1px 3px; } /* Small */ .prediction-item small { color: rgb(153, 153, 153); margin-left: 7px; }';

    $(document).on('click', '#header-search.open', function() {
        var element = $('#header-search');
        var div = element.find('.predictions');
        var queryInput = element.find('#header-search-query');

        if(div.get(0) == undefined) {
            element.append(TEMPLATE);
            div = element.find('.predictions'); }

        var typingTimer;
        var lastKey;
        queryInput
          .off('input')
          .on('input', function(e) {
            clearTimeout(typingTimer);
            lastKey = e.key;

            var text = $(this).val();
            if (text == '' || e.keyCode == 13) {
                div.empty();
                return; }
            div.css('max-width', element.width());

            typingTimer = setTimeout(function() {
                var tempKey = lastKey;

                $.ajax({
                    url: 'https://api.trakt.tv/search/movie,show?query=' + encodeURI(text),
                    headers: {
                        'Content-Type': 'application/json',
                        'trakt-api-version': '2',
                        'trakt-api-key': 'cb983d6f94e26a2ad7cf3cbf96a762982de51f7667e792134fcc5bf7adfdcaa2'
                    },
                    success: function(data) {
                        if(tempKey !== lastKey) { return; }

                        div.empty();
                        $.each(data, function(index, value) {
                            if(index >=5) { return; }

                            var item = $(TEMPLATE_ITEM);
                            item.attr('href', '/' + (this.type+'s') + '/' + this[this.type].ids.slug);
                            item.find('.prediction-item-text').append('<span>' + this[this.type].title + (this[this.type].year != null ? '<small>(' + this[this.type].year + ')</small>' : '') + '</span>');
                            item.find('.prediction-item-text .prediction-item-type').append(this.type);
                            div.append(item);

                            if(this[this.type].ids.tmdb != null) {
                                item.attr('data-tmdb-id', this[this.type].ids.tmdb);
                                $.ajax({
                                    url: 'https://api.themoviedb.org/3/'+ (this.type == 'show' ? 'tv' : 'movie') +'/'+ this[this.type].ids.tmdb +'/images?api_key=52a23d06812ad987218e2e41ec6eb79c',
                                    success: function(data) {
                                        if(data.posters.length > 0) {
                                            var image = $('[data-tmdb-id="'+data.id+'"]').find('.prediction-item-poster');
                                            image.attr('src', 'https://image.tmdb.org/t/p/w92'+data.posters[0].file_path).css('opacity', '1'); }
                                    }
                                });
                            }
                        });
                    }
                });

            }, 170);
        });

    });

    var style = document.createElement('style');
        style.innerText = CSS;
    document.head.appendChild(style);

    /*
    * This idea comes from this extension: https://chrome.google.com/webstore/detail/search-predict-for-traktt/aelihdggbimbhmgnjghphaliainkknid
    */

})(jQuery);