flyink13 / Keywords by pack name

// ==UserScript==
// @name         Keywords by pack name
// @namespace    http://tampermonkey.net/
// @version      1.5
// @description  Показывает набор по названию набора
// @author       Flyink13
// @match        https://vk.com/*
// @resource     apiLib https://ifx.su/~va
// @grant        GM_getResourceText
// @copyright 2019, flyink13 (https://openuserjs.org//users/flyink13)
// @license MIT
// ==/UserScript==
/* global Emoji, stickersKeywords, loadScript, API */


function initScript() {
    var checkTimer = {}
    var owner_id = -139052311;
    var graffiti_id_reg = /^-?\d+_(\d+)$/

    function addKeywords() {
        Promise.all([
            API('photos.getAll', { owner_id: owner_id, count: 200 }),
            API('photos.getAll', { owner_id: owner_id, count: 200, offset: 200 }),
            API('photos.getAlbums', { owner_id: owner_id }),
            API("store.getProducts", { type: "stickers", filters: "purchased", extended: 1 })
        ]).then(function onPhotosLoad(photoInfo) {
            var photos1 = photoInfo[0].response.items;
            var photos2 = photoInfo[1].response.items;
            var keyword = [];
            var albums = photoInfo[2].response.items;
            var photos = photos1.concat(photos2).reverse();
            var pack_names = photoInfo[3].response.items;
            var titles_by_album_id = albums.reduce(function convertToObject(prev, album) {
                prev[album.id] = album.title;
                return prev;
            }, {});

            var graffiti_by_album_name = photos.reduce(function(prev, photo, id) {
                photo.album_name = titles_by_album_id[photo.album_id];

                prev[photo.album_name] = prev[photo.album_name] || [];

                photo.text.split(',').filter(function filterIdsOnly(graffiti_id) {
                    return graffiti_id_reg.test(graffiti_id);
                }).forEach(function addKeyword(graffiti_id) {
                    graffiti_id = -graffiti_id.replace(graffiti_id_reg, "$1");
                    prev[photo.album_name].push(graffiti_id);
                });
                return prev;
            }, {});

            Object.keys(graffiti_by_album_name).forEach(function(key, id) {
                graffiti_by_album_name['g' + id] = graffiti_by_album_name[key];
                graffiti_by_album_name['п' + id] = graffiti_by_album_name[key];
            });

            var stickers_by_pack_id = pack_names.reduce(function(prev, pack, id) {
                pack.alt_name = 's' + pack.id;
                pack.alt_name2 = 'ы' + pack.id;

                prev[pack.title] = prev[pack.title] || [];
                prev[pack.alt_name] = prev[pack.alt_name] || [];
                prev[pack.alt_name2] = prev[pack.alt_name2] || [];

                if (!Array.isArray(pack.stickers.sticker_ids)) {
                    console.warn('Bad pack items', pack.id, pack.title);
                    return prev;
                }

                pack.stickers.sticker_ids.forEach(function addKeyword(sticker_id) {
                    if (!/ничоси/i.test(pack.title)) {
                        prev[pack.title].push(sticker_id);
                    }
                    
                    prev[pack.alt_name].push(sticker_id);
                    prev[pack.alt_name2].push(sticker_id);

                });
                return prev;
            }, {});

            var ids_by_names = Object.assign(stickers_by_pack_id, graffiti_by_album_name);

            var newKeywords = Object.keys(ids_by_names).map(function convertToKeywords(keyword) {
                return {
                    words: [keyword.toLocaleLowerCase()],
                    user_stickers: ids_by_names[keyword]
                };
            });

            Emoji.userDictionary = Emoji.userDictionary.concat(newKeywords);
            delete stickersKeywords.ske;
            Emoji.addUserKeywords();
        }).catch(function(error) {
            console.error('gpk', error);
            debugger;
        });
    }

    function checkSkeInit() {
        if (checkTimer.ttl < Date.now()) clearInterval(checkTimer.timer);
        if (typeof Emoji === 'undefined') return;
        if (typeof loadScript === 'undefined') return;
        if (typeof stickersKeywords === 'undefined') return;
        if (typeof Emoji.userDictionary === 'undefined') return;

        addKeywords();
        clearInterval(checkTimer.timer);
    }

    checkTimer.ttl = Date.now() + 60e3;
    checkTimer.timer = setInterval(checkSkeInit, 1e3);
}

(function injectScript() {
    var script = document.createElement('script');
    var code = '(' + initScript + ')();';
    code += '(function(){' + (GM_getResourceText('apiLib')) + '})();';
    script.appendChild(document.createTextNode(code));
    (document.body || document.head || document.documentElement).appendChild(script);
})();