2GIS phone number copier

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

目前为 2019-07-21 提交的版本。查看 最新版本

// ==UserScript==
// @name         2GIS phone number copier
// @namespace    http://2gis.ru/
// @version      0.2
// @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() {

    // Elements that contain phone number in innerText
    const elems = document.querySelectorAll("._cont_card._state_visible .contact__phonesVisible .contact__phonesItemLinkNumber");
    // Parent elements a[href] that are parents for elems
    const parentHrefs = document.querySelectorAll("._cont_card._state_visible .contact__phonesVisible a.contact__phonesItemLink");
    // Parent elements that are parents for parentHrefs
    const parentElems = document.querySelectorAll("._cont_card._state_visible .contact__phonesVisible .contact__phonesItem._type_phone");
    // Number link like `tel:` containing number. Usually it's the parentHrefs[index]
    const numberLink = document.querySelectorAll("._cont_card._state_visible .contact__phonesVisible a.contact__phonesItemLink");

    // Same for different page layout
    const elemsMedia = document.querySelectorAll(".mediaCard._visible .mediaContacts__group .mediaContacts__phonesNumber");
    const parentHrefsMedia = document.querySelectorAll(".mediaCard._visible .mediaContacts__group .mediaContacts__phonesItemCut");
    const parentElemsMedia = document.querySelectorAll(".mediaCard._visible .mediaContacts__group .mediaContacts__groupItem._phone");
    const numberLinkMedia = document.querySelectorAll(".mediaCard._visible .mediaContacts__group .mediaContacts__phonesNumber");

    placeIcons(elems, parentElems, parentHrefs, numberLink);
    placeIcons(elemsMedia, parentElemsMedia, parentHrefsMedia, numberLinkMedia);

    function placeIcons(elems, parentElems, parentHrefs, numberLink) {
        // console.log(`Got elems: ${elems.length}, parentElems: ${parentElems.length}, parentHrefs: ${parentHrefs.length}, numberLink: ${numberLink.length}`);
        elems.forEach((element, index) => {

            let whatsappLink = preparePhoneNumber(numberLink[index].href, index);
            let phoneLink = preparePhoneNumber(numberLink[index].href, index);
            let phoneNumber = preparePhoneNumber(numberLink[index].href, index);
            // console.log(`whatsappLink: ${whatsappLink}`);

            if (whatsappLink && parentHrefs[index] && !parentElems[index].getAttribute("whatsapp-linked")) {
                let whatsapp = "http://wa.me/" + whatsappLink;
                let button = prepareWhatsappButton(whatsapp, index);
                parentElems[index].insertBefore(button, parentHrefs[index]);
                parentElems[index].setAttribute("whatsapp-linked", true);
                // console.log(`Placed whatsappLink with number: ${whatsapp}`);
            }
            if (phoneLink) {
                let phone = "tel:+" + phoneLink;
                // console.log(`Placed phone with number: ${phone}`);
            }
            if (phoneNumber && parentHrefs[index] && !parentElems[index].getAttribute("phone-linked")) {
                let phone = `+${phoneNumber[0]} ${phoneNumber[1]}${phoneNumber[2]}${phoneNumber[3]} ${phoneNumber[4]}${phoneNumber[5]}${phoneNumber[6]}-${phoneNumber[7]}${phoneNumber[8]}-${phoneNumber[9]}${phoneNumber[10]}`
                let button = preparePhoneButton(phone, index);
                parentElems[index].insertBefore(button, parentHrefs[index]);
                parentElems[index].setAttribute("phone-linked", true);
                // console.log(`Placed phone with number: ${phone}`);
            }
        });

    }

    function preparePhoneNumber(phone, index) {
        // console.log(`Received phone ${phone} with index ${index} in preparePhoneNumber()`);
        const regex = /79\d+/;
        if (regex.test(phone)) {
            let result = regex.exec(phone);
            if (result[0].length < 11) {
                return null;
            }
            // console.log(`Successfully done regex: ${result[0]} in preparePhoneNumber()`);
            return result[0]
        } else { return null }
    }

    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;
    }

}