2GIS phone number copier

2GIS mobile phone number copier allow you to copy any Russian mobile phone number to your device's clipboard

As of 2019-07-19. See the latest version.

// ==UserScript==
// @name         2GIS phone number copier
// @namespace    http://2gis.ru/
// @version      0.1
// @description  2GIS mobile phone number copier allow you to copy any Russian mobile phone number to your device's clipboard
// @author       Kenya-West
// @include      https://2gis.ru/*
// @grant        GM_setClipboard
// ==/UserScript==

setInterval(function() {
    main()
}, 500)
    
function main() {

    const elems = document.querySelectorAll("._cont_card._state_visible .contact__phonesVisible .contact__phonesItemLinkNumber");
    const parentElems = document.querySelectorAll("._cont_card._state_visible .contact__phonesVisible .contact__phonesItem._type_phone");
    const parentHrefs = document.querySelectorAll("._cont_card._state_visible .contact__phonesVisible a.contact__phonesItemLink");

    elems.forEach((element, index) => {
        let whatsappLink = element.innerText;
        let phoneLink = element.innerText;
        let phoneNumber = element.innerText;
        let regex = /\+7\–9\d+\–\d+\–\d+\–\d+/gi;
        if (regex.test(whatsappLink) && parentHrefs[index] && !parentElems[index].getAttribute("whatsapp-linked")) {
            whatsappLink = whatsappLink.replace(/\+/, "");
            whatsappLink = whatsappLink.replace(/\s/, "");
            whatsappLink = whatsappLink.replace(/\–/g, "");
            whatsappLink = "http://wa.me/" + whatsappLink;
            let button = prepareWhatsappButton(whatsappLink, index);
            parentElems[index].insertBefore(button, parentHrefs[index]);
            parentElems[index].setAttribute("whatsapp-linked", true);
        }
        if (regex.test(phoneLink)) {
            phoneLink = phoneLink.replace(/\s/, "");
            phoneLink = phoneLink.replace(/\–/g, "");
            phoneLink = "tel:" + phoneLink;
        }
        if (regex.test(phoneNumber) && parentHrefs[index] && !parentElems[index].getAttribute("phone-linked")) {
            phoneNumber = phoneNumber.replace(/\–/g, "-");
            phoneNumber = phoneNumber.replace(/\-/, " ");
            phoneNumber = phoneNumber.replace(/\-/, " ");
            let button = preparePhoneButton(phoneNumber, index);
            parentElems[index].insertBefore(button, parentHrefs[index]);
            parentElems[index].setAttribute("phone-linked", true);
        }
    });

    function prepareWhatsappButton(whatsappLink, index) {
        const button = document.createElement('img');
        button.id = "whatsappLink" + index;
        button.src = "https://image.flaticon.com/icons/svg/33/33447.svg";
        button.style.width = "24px";
        button.style.height = "24px";
        button.style.display = "inline-block";
        button.style.cursor = "pointer";
        button.style.marginRight = "5px";
        button.setAttribute("whatsapp-link", whatsappLink);
        button.addEventListener('click', (element) => {
            if (element.target && element.target.id === button.id) {
                GM_setClipboard(whatsappLink);
                element.toElement.style.backgroundImage = "green";
                window.setTimeout(() => {
                    element.toElement.style.backgroundImage = "none";
                }, 1000);
            }
        });
        return button;
    }
    function preparePhoneButton(phone, index) {
        const button = document.createElement('img');
        button.id = "phone" + index;
        button.src = "https://image.flaticon.com/icons/svg/60/60990.svg";
        button.style.width = "24px";
        button.style.height = "24px";
        button.style.display = "inline-block";
        button.style.cursor = "pointer";
        button.style.marginRight = "5px";
        button.setAttribute("phone", phone);
        button.addEventListener('click', (element) => {
            if (element.target && element.target.id === button.id) {
                GM_setClipboard(phone);
                element.toElement.style.backgroundImage = "green";
                window.setTimeout(() => {
                    element.toElement.style.backgroundImage = "none";
                }, 1000);
            }
        });
        return button;
    }

}