2GIS phone number copier

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

当前为 2019-10-25 提交的版本,查看 最新版本

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

const url = new URL(window.location.href);
setInterval(() => {
    main()
}, 500);

function main() {

    const elems = document.querySelectorAll("._49kxlr > ._b0ke8 > a");
    const parentElems = document.querySelectorAll("._49kxlr > ._b0ke8");
    const parentHrefs = document.querySelectorAll("._49kxlr > ._b0ke8 > a");

    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 = "14px";
        button.style.height = "14px";
        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 = "14px";
        button.style.height = "14px";
        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;
    }

}