<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Parakath</title>
    <description>The latest articles on DEV Community by Parakath (@parakath_fullst).</description>
    <link>https://dev.to/parakath_fullst</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1413369%2Fc390a2b6-7729-426a-93e1-ba2f0b2c6d7a.png</url>
      <title>DEV Community: Parakath</title>
      <link>https://dev.to/parakath_fullst</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/parakath_fullst"/>
    <language>en</language>
    <item>
      <title>Pin more than 3 Whatsapp contacts</title>
      <dc:creator>Parakath</dc:creator>
      <pubDate>Tue, 09 Apr 2024 04:25:48 +0000</pubDate>
      <link>https://dev.to/parakath_fullst/pin-more-than-3-whatsapp-contacts-ik</link>
      <guid>https://dev.to/parakath_fullst/pin-more-than-3-whatsapp-contacts-ik</guid>
      <description>&lt;p&gt;I am working on a chrome extension which involves DOM manipulation for adding pin button for pinnedContacts iterating the array, the button needs to be added for each contact name in the list, Then need to move on to the top of the chat list.&lt;/p&gt;

&lt;p&gt;The problem is the index for each chat item dynamically changing, so while trying to apply the logic in the pinned contact, the feature doe not retain, keeps changing while scrolling up or down.&lt;/p&gt;

&lt;p&gt;We are using HTML, CSS, Javascript for the extension. here is the javascript code for retrieving pinned contacts and add pin button.&lt;/p&gt;

&lt;p&gt;Could any one please help me to resolve the issue.&lt;/p&gt;

&lt;p&gt;const getPinnedContacts = () =&amp;gt; {&lt;br&gt;
    return new Promise((resolve, reject) =&amp;gt; {&lt;br&gt;
        chrome.storage.sync.get("pinnedContacts", (data) =&amp;gt; {&lt;br&gt;
            if (chrome.runtime.lastError) {&lt;br&gt;
                reject(chrome.runtime.lastError);&lt;br&gt;
            } else {&lt;br&gt;
                const pinnedContacts = (data &amp;amp;&amp;amp; data.pinnedContacts) || [];&lt;br&gt;
                resolve(pinnedContacts);&lt;br&gt;
            }&lt;br&gt;
        });&lt;br&gt;
    });&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;const getChatList = () =&amp;gt; {&lt;br&gt;
    return new Promise((resolve) =&amp;gt; {&lt;br&gt;
        const chatList = document.querySelector('[aria-label="Chat list"]');&lt;br&gt;
        if (chatList) {&lt;br&gt;
            resolve(chatList);&lt;br&gt;
        } else {&lt;br&gt;
            // Retry after 1 second if chat list element is not found&lt;br&gt;
            setTimeout(() =&amp;gt; {&lt;br&gt;
                getChatList().then(resolve);&lt;br&gt;
            }, 1000);&lt;br&gt;
        }&lt;br&gt;
    });&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;const waitForChatList = () =&amp;gt; {&lt;br&gt;
    const chatList = document.querySelector('[aria-label="Chat list"]');&lt;br&gt;
    if (chatList) {&lt;br&gt;
        // Retrieve pinned contacts&lt;br&gt;
        chrome.storage.sync.get("pinnedContacts", (data) =&amp;gt; {&lt;br&gt;
            const pinnedContacts = (data &amp;amp;&amp;amp; data.pinnedContacts) || [];&lt;br&gt;
            // Process chat list and add pin buttons&lt;br&gt;
            processChatList(chatList, pinnedContacts);&lt;br&gt;
        });&lt;br&gt;
    } else {&lt;br&gt;
        // Chat list element not found, wait and retry&lt;br&gt;
        setTimeout(waitForChatList, 1000); // Retry after 1 second&lt;br&gt;
    }&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;// Initial call to start processing&lt;br&gt;
waitForChatList();&lt;/p&gt;

&lt;p&gt;const processChatListSortPinnedContacts = () =&amp;gt; {&lt;br&gt;
    const waitForChatList = () =&amp;gt; {&lt;br&gt;
        const chatList = document.querySelector('[aria-label="Chat list"]');&lt;br&gt;
        if (chatList) {&lt;br&gt;
            // Retrieve pinned contacts&lt;br&gt;
            chrome.storage.sync.get("pinnedContacts", (data) =&amp;gt; {&lt;br&gt;
                const pinnedContacts = (data &amp;amp;&amp;amp; data.pinnedContacts) || [];&lt;br&gt;
                // Process chat list and add pin buttons&lt;br&gt;
                processChatList(chatList, pinnedContacts);&lt;br&gt;
            });&lt;br&gt;
        } else {&lt;br&gt;
            // Chat list element not found, wait and retry&lt;br&gt;
            setTimeout(waitForChatList, 1000); // Retry after 1 second&lt;br&gt;
        }&lt;br&gt;
    };&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Initial call to start processing
waitForChatList();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;};&lt;/p&gt;

&lt;p&gt;const processChatList = async (chatList, pinnedContacts) =&amp;gt; {&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const chatContacts = chatList.querySelectorAll('div[role="listitem"]');

const contactNames = [];
const pinnedContactElementsMap = new Map();

pinnedContacts.forEach(pinnedContact =&amp;gt; {

    chatContacts.forEach((contact, index)  =&amp;gt; {
     //console.log("chat contacts  "+contact.innerHTML+"  index :  "+index);
        const nthChild = index + 1; 
        try {
        const titleElements = contact.querySelectorAll(`#pane-side &amp;gt; div:nth-child(3) &amp;gt; div &amp;gt; div &amp;gt; div:nth-child(${nthChild}) &amp;gt; div &amp;gt; div &amp;gt; div &amp;gt; div._ak8l &amp;gt; div._ak8o &amp;gt; div._ak8q &amp;gt; div`);

       // console.log("titleElements len "+titleElements.length);

        if(titleElements.length&amp;gt;0)
        {
            //console.log("titleElements 0 "+titleElements[0].innerHTML);
            const titleElement1 = titleElements[0].querySelector('span');

            console.log("titleElement 1 "+titleElement1.textContent.trim());
            console.log("pinnedContact  "+pinnedContact);
            console.log("pinnedContact matched "+(pinnedContact === titleElement1.textContent.trim()));
                if(pinnedContact === titleElement1.textContent.trim()) {

                    console.log("gridCellElement required 111 ");
                const gridCellElement = contact.querySelector(`#pane-side &amp;gt; div:nth-child(3) &amp;gt; div &amp;gt; div &amp;gt; div:nth-child(${nthChild}) &amp;gt; div &amp;gt; div &amp;gt; div &amp;gt; div._ak8l &amp;gt; div._ak8j &amp;gt; div._ak8i`);

                const pinButton = gridCellElement.querySelector('span[data-icon="pinned2"]');

                    if (!pinButton ) {
                        const gridCellElement = contact.querySelector(`#pane-side &amp;gt; div:nth-child(3) &amp;gt; div &amp;gt; div &amp;gt; div:nth-child(${nthChild}) &amp;gt; div &amp;gt; div &amp;gt; div &amp;gt; div._ak8l &amp;gt; div._ak8j &amp;gt; div._ak8i`);

                        // Create a new pin button
                        const newPinButton = document.createElement('div');
                        newPinButton.className = '_ahlk _ahll';
                        newPinButton.innerHTML = '&amp;lt;span data-icon="pinned2"&amp;gt;&amp;lt;svg height="15" width="13" preserveAspectRatio="xMidYMid meet" class=""&amp;gt;&amp;lt;title&amp;gt;pinned2&amp;lt;/title&amp;gt;&amp;lt;path fill="currentColor" d="M12.074 4.21 8.7 8.232l.116 4.233a.4.4 0 0 1-.657.318L.43 6.297a.4.4 0 0 1 .199-.702l4.196-.622L8.196.957a.63.63 0 0 1 .887-.078l2.914 2.445a.63.63 0 0 1 .077.887ZM1.294 14.229a.713.713 0 0 1-1.09-.915l2.674-3.64 1.536 1.288-3.12 3.267Z"&amp;gt;&amp;lt;/path&amp;gt;&amp;lt;/svg&amp;gt;&amp;lt;/span&amp;gt;';

                        // Find the span element within the grid cell element
                        const spanElement = gridCellElement.querySelector('span');
                        if (spanElement) {
                            // Insert the new pin button after the span element
                            spanElement.insertAdjacentElement('afterend', newPinButton);
                        }
                        return;
                    }
            }
        }
        else {
            const titleElement2 = contact.querySelector(`#pane-side &amp;gt; div:nth-child(3) &amp;gt; div &amp;gt; div &amp;gt; div:nth-child(${nthChild}) &amp;gt; div &amp;gt; div &amp;gt; div &amp;gt; div._ak8l &amp;gt; div._ak8o &amp;gt; div._ak8q &amp;gt; div &amp;gt; span[title=${pinnedContact}]`);
           // console.log("titleElement 2 "+titleElement2.innerHTML);
            if(titleElement2)
            {
                console.log("gridCellElement required 222 ");
            const gridCellElement = contact.querySelector(`#pane-side &amp;gt; div:nth-child(3) &amp;gt; div &amp;gt; div &amp;gt; div:nth-child(${nthChild}) &amp;gt; div &amp;gt; div &amp;gt; div &amp;gt; div._ak8l &amp;gt; div._ak8j &amp;gt; div._ak8i`);

                const pinButton = gridCellElement.querySelector('span[data-icon="pinned2"]');

                if (!pinButton) {
                    // Create a new pin button
                    const newPinButton = document.createElement('div');
                    newPinButton.className = '_ahlk _ahll';
                    newPinButton.innerHTML = '&amp;lt;span data-icon="pinned2"&amp;gt;&amp;lt;svg height="15" width="13" preserveAspectRatio="xMidYMid meet" class=""&amp;gt;&amp;lt;title&amp;gt;pinned2&amp;lt;/title&amp;gt;&amp;lt;path fill="currentColor" d="M12.074 4.21 8.7 8.232l.116 4.233a.4.4 0 0 1-.657.318L.43 6.297a.4.4 0 0 1 .199-.702l4.196-.622L8.196.957a.63.63 0 0 1 .887-.078l2.914 2.445a.63.63 0 0 1 .077.887ZM1.294 14.229a.713.713 0 0 1-1.09-.915l2.674-3.64 1.536 1.288-3.12 3.267Z"&amp;gt;&amp;lt;/path&amp;gt;&amp;lt;/svg&amp;gt;&amp;lt;/span&amp;gt;';

                    // Find the span element within the grid cell element
                    const spanElement = gridCellElement.querySelector('span');
                    if (spanElement) {
                        // Insert the new pin button after the span element
                        spanElement.insertAdjacentElement('afterend', newPinButton);
                    }
                    return;
                }
            }
        }
        } catch (error) {
            //console.error('Error while querying selector:', error);
            // Handle the error as needed
        }
    });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;});&lt;br&gt;
    await new Promise(resolve =&amp;gt; setTimeout(resolve, 0));&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const pinnedContactEntries = Array.from(pinnedContactElementsMap.entries());

console.log("pinnedContactEntries Element:"+ pinnedContactEntries);

chrome.storage.sync.set({ contactNames: contactNames });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;};&lt;/p&gt;

&lt;p&gt;processChatListSortPinnedContacts();&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
