Anon_Loves_Helping / Facebook Messenger Convo Pinner

// ==UserScript==
// @name         Facebook Messenger Convo Pinner
// @version      1.0
// @description  Pin your messages to the top of the list to never search for it again if you ever need it!
// @author       Tropical_Sea
// @license      GPL-3.0-or-later; http://www.gnu.org/licenses/gpl-3.0.txt
// @copyright    Copyright (C) 2019, by Tropical_Sea
// @icon         https://visualpharm.com/assets/407/Pin-595b40b65ba036ed117d3171.svg
// @updateURL    https://openuserjs.org/install/Anon_Loves_Helping/Facebook_Messenger_Convo_Pinner.user.js
// @match        https://www.facebook.com/messages/t/*
// @match        https://www.messenger.com/t/*
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_deleteValue
// @grant        GM_listValues
// @grant        GM_addStyle
// @noframes
// ==/UserScript==

(function() {
    'use strict';

    setTimeout(function(){
        // Styling
        GM_addStyle(`
#MCP_pinner_button:hover, #MCP_sst_btn:hover{
    background-color: rgb(0, 0, 0, .4);
    color: #fff;
}
#MCP_SVG{
    margin-right: 10px;
    cursor: pointer;
}
#MCP_SVG_D1 svg, #MCP_SVG_D2 svg{
    height: 20px;
    width: 20px;
    position: absolute;
    margin-left: -25px;
    margin-top: 5px;
    cursor: pointer;
    transition: transform 0.3s;
}
`);

        if (location.host === 'www.facebook.com') var mofsss = 33;
        else mofsss = 25;
        // Lists
        var convoList = document.querySelector('ul[aria-label="Conversation List"]');
        var menu = document.querySelectorAll('[aria-label="Actions"]');
        var options = document.querySelector('ul[aria-label="Conversation List"] li');
        // GM Values
        var GM = Array.from(GM_listValues());
        var VALS = [];
        var VREF = [];
        for (var q = 0; q < GM.length; q++){
            if (GM_getValue(GM[q]) === undefined || GM_getValue(GM[q]) === 'undefined' || GM[q] === 'undefined' || GM[q] === undefined || GM_getValue(GM[q]) === 0 || GM_getValue(GM[q]) === -1) GM_deleteValue(GM[q])
            VALS.push(GM_getValue(GM[q]));
            VREF.push(GM[q]);
        }
        var alist = convoList.querySelectorAll('a');
        var links = [];
        alist.forEach(function(e){
            links.push(e.getAttribute('data-href').substring(mofsss));
        });

        var pin = document.createElement("li");
        pin.classList.add("_54ni");
        pin.classList.add("__MenuItem");
        pin.setAttribute("role", "presentation");
        pin.id = "MCP_pinner_button";
        pin.innerText = 'Pin This Convo';

        var catg = document.createElement('div');
        catg.id = 'MCP_catg_pinned';
        var s1 = '<div id="MCP_SVG_D1"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 26 26"> <path fill="#cccccc" d="M13,15.405l8.764-8.584c0.392-0.383,1.019-0.38,1.406,0.008l1.536,1.536c0.391,0.391,0.39,1.026-0.002,1.417L13.707,20.707 C13.512,20.902,13.256,21,13,21c-0.256,0-0.512-0.098-0.707-0.293L1.296,9.782c-0.393-0.39-0.394-1.025-0.002-1.417L2.83,6.829 c0.387-0.387,1.015-0.391,1.406-0.008L13,15.405z"/> </svg> </div>';
        var s2 = '<div id="MCP_SVG_D2"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 26 26"> <path fill="#cccccc" d="M13,15.405l8.764-8.584c0.392-0.383,1.019-0.38,1.406,0.008l1.536,1.536c0.391,0.391,0.39,1.026-0.002,1.417L13.707,20.707 C13.512,20.902,13.256,21,13,21c-0.256,0-0.512-0.098-0.707-0.293L1.296,9.782c-0.393-0.39-0.394-1.025-0.002-1.417L2.83,6.829 c0.387-0.387,1.015-0.391,1.406-0.008L13,15.405z"/> </svg> </div>';
        catg.innerHTML = "<h1 style='margin-left: 30px;' class='_6-xo'>"+s1+"Pinned</h1> <div> <ul></ul> </div> <h1 style='margin-left: 30px;' class='_6-xo'>"+s2+"Chats</h1>";
        var c = document.querySelector('[aria-label="Conversations"]');
        c.insertBefore(catg, c.childNodes[0]);
        var d1 = document.querySelector('#MCP_SVG_D1');
        var d2 = document.querySelector('#MCP_SVG_D2');
        var n1 = d1.parentElement.nextElementSibling;
        n1.style = 'heigth: auto; overflow: hidden; transition: height 1s;';
        var n2 = d2.parentElement.parentElement.parentElement.children[1];
        n2.style = 'height: auto; overflow: hidden; transition: height 1s;';
        d1 = d1.children[0];
        d2 = d2.children[0];
        d1.addEventListener('click', function(){
            if (n1.style.height === 'auto')
            {
                d1.style = 'transform: rotate(-90deg);';
                n1.style.height = '0px';
            }
            else
            {
                d1.style = 'transform: rotate(0deg);';
                n1.style.height = 'auto';
            }
        });
        d2.addEventListener('click', function(){
            if (n2.style.height === 'auto')
            {
                n2.style.height = '0px';
                d2.style = 'transform: rotate(-90deg);';
            }
            else
            {
                n2.style.height = 'auto';
                d2.style = 'transform: rotate(0deg);';
            }
        });

        function re(){
            GM = Array.from(GM_listValues());
            VALS = [];
            VREF = [];
            for (var q = 0; q < GM.length; q++){
                VALS.push(GM_getValue(GM[q]));
                VREF.push(GM[q]);
            }
        }
        function unpinn(e, r = undefined, l){
            if (typeof e != 'string') var t = e.href.substring(mofsss);
            else t = e.substring(mofsss);
            if (!r) r = l.querySelector('#MCP_SVG');
            r.remove();
            convoList.insertBefore(l, convoList.childNodes[0]);
            var d = VALS.indexOf(t);
            if (d > -1) GM_deleteValue(VREF[d]);
            re();
        }
        function pinn(e, l=false){
            catg.childNodes[2].insertBefore(e, catg.childNodes[2].childNodes[0]);
            var t = catg.children[1].children[0].children[1];
            var d = document.createElement('div');
            d.id = 'MCP_SVG';
            d.title = 'Pinned by messeneger convo pinner';
            d.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 172 172"><g fill="#cccccc"><path d="M123.625,0c-0.65844,0.01344 -1.29,0.14781 -1.935,0.3225c-2.55312,0.71219 -4.85094,2.40531 -6.665,4.73c-0.04031,0.04031 -0.06719,0.06719 -0.1075,0.1075l-28.7025,40.205c-17.64344,-4.085 -36.89937,0.56438 -50.6325,14.2975c-1.33031,1.34375 -1.33031,3.49375 0,4.8375l25.585,25.4775l-60.415,76.4325c-1.10188,1.37063 -0.99438,3.34594 0.24188,4.59563c1.24969,1.23625 3.225,1.34375 4.59562,0.24187l76.325,-60.415l25.585,25.4775c1.34375,1.33031 3.49375,1.33031 4.8375,0c13.93469,-13.92125 18.49,-33.54 14.0825,-51.385l40.3125,-27.4125c0.04031,-0.04031 0.06719,-0.06719 0.1075,-0.1075c2.35156,-1.74687 3.9775,-3.91031 4.73,-6.45c0.7525,-2.53969 0.37625,-5.75125 -1.72,-7.8475l-40.42,-40.9575c-1.03469,-1.06156 -2.40531,-1.65281 -3.7625,-1.935c-0.68531,-0.14781 -1.38406,-0.22844 -2.0425,-0.215zM124.27,6.88c0.12094,0.01344 0.13438,0.02688 0.215,0.1075l40.5275,40.9575c0.14781,0.14781 0.22844,0.18813 0,0.9675c-0.22844,0.7525 -1.02125,1.935 -2.2575,2.9025l-0.1075,0.1075l-41.8175,28.4875c-0.04031,0.04031 -0.06719,0.06719 -0.1075,0.1075l-0.215,0.1075c-1.14219,0.88688 -1.6125,2.37844 -1.1825,3.7625c4.50156,15.09031 1.06156,31.72594 -9.7825,44.29l-66.3275,-66.22c12.40281,-10.69625 28.82344,-14.23031 43.7525,-9.9975c1.42438,0.38969 2.94281,-0.17469 3.7625,-1.3975l29.67,-41.8175l0.1075,-0.1075c0.99438,-1.22281 2.24406,-1.935 3.01,-2.15c0.40313,-0.1075 0.63156,-0.12094 0.7525,-0.1075zM66.005,94.9225l11.0725,10.965l-52.5675,41.6025z"></path></g></svg>';
            t.appendChild(d);
            d.addEventListener('click', function(){
                unpinn(this.parentElement.parentElement.querySelector('a'), this, this.parentElement.parentElement);
            });
            if (l)
            {
                var c = 'c' + Math.floor(Math.random() * 84381730);
                l = l.substring(mofsss);
                GM_setValue(c, l);
            }
            re();
        }
        for (var i = 0; i < links.length; i++){
            if (VALS.includes(links[i])) pinn(alist[i].parentElement.parentElement);
        }

        menu.forEach(function(elm){
            var e = elm.getAttribute('aria-describedby');
            elm.addEventListener("click", function(){
                var c = this.parentElement.parentElement;
                if (this.parentElement.parentElement.parentElement.parentElement.id === 'MCP_catg_pinned') pin.innerText = 'Unpin This Convo';
                else pin.innerText = 'Pin This Convo';
                setTimeout(function(){
                    var m = document.querySelector("._54nf");
                    var e = m.childNodes[2];
                    m.insertBefore(pin, e);
                }, 50);
            });
        });

        pin.addEventListener("click", function(){
            var c = document.querySelector("._5l-3._1ht1._6zk9._13aa");
            var l = c.querySelector('a').getAttribute('data-href');
            if (pin.innerText === 'Pin This Convo') pinn(c, l);
            else unpinn(l, undefined, c);
            document.querySelector('._6-xk').click();
        });

        setInterval(function(){
            // GM Values
            GM = Array.from(GM_listValues());
            VALS = [];
            VREF = [];
            for (var dde = 0; dde < GM.length; dde++){
                if (GM_getValue(GM[dde]) === undefined || GM_getValue(GM[dde]) === 'undefined' || GM[dde] === 'undefined' || GM[dde] === undefined || GM_getValue(GM[dde]) === 0 || GM_getValue(GM[dde]) === -1) GM_deleteValue(GM[dde])
                VALS.push(GM_getValue(GM[dde]));
                VREF.push(GM[dde]);
            }
        }, 1000);
    }, 7000);
})();