JavaScript function to get Instagram ID

57 Views Asked by At

I have a JavaScript function with an event button to get ID of instagram users, but the problem is that it will only get the ID of the first user, and if I try to go to another profile, it gives the ID of the old profile, instead of getting of the current page profile?

(function() {
    'use strict';

    // Function to extract profile ID from the Instagram page
    function getProfileId() {
        // Extracting profile ID from the page source
        var pageSource = document.documentElement.innerHTML;
        var profileIdPattern = /"profilePage_(\d+)"/;
        var matches = pageSource.match(profileIdPattern);
        if (matches && matches.length > 1) {
            return matches[1];
        }
        return null;
    }

    // Function to copy text to clipboard
    function copyToClipboard(text) {
        GM_setClipboard(text, 'text');
    }

    // Function to create and append a copy button to the Instagram page
    function createCopyButton() {
        var button = document.createElement('button');
        button.textContent = 'Copy Profile ID';
        button.style.position = 'fixed';
        button.style.top = '20px';
        button.style.right = '20px';
        button.style.zIndex = '9999';
        button.style.padding = '10px';
        button.style.border = 'none';
        button.style.backgroundColor = '#3897f0';
        button.style.color = '#fff';
        button.style.cursor = 'pointer';
        button.addEventListener('click', function() {
            var profileId = getProfileId();
            if (profileId) {
                copyToClipboard(profileId);
                console.log('Profile ID copied to clipboard:', profileId);
                alert('Profile ID copied to clipboard:\n' + profileId);
            } else {
                console.log('Profile ID not found.');
                alert('Profile ID not found.');
            }
        });
        document.body.appendChild(button);
    }

    // Run the function to create the copy button when the page is loaded
    window.addEventListener('load', createCopyButton);

})();

How do I fix it such, that it reloads everytime I go try get the ID of a new profile after the first one?

0

There are 0 best solutions below