Edge extension side panel populating same data to all tabs

Vignesh kumar Ayyappan 0 Reputation points
2024-04-04T03:56:44.81+00:00

I have created edge extension with side panel to show tab specific data but same data getting reflected to all the tabs containing side panel.

Expected : Side panel should show tab specific data.

Example: I have created Sample Extension that displays URL of the page in side panel.

Issue: Seeing same URL in all the tabs side panel.

Expected Samples:

Code samples

Steps, I followed to implement side panel follows below,

Step 1: "permissions": ["sidePanel", "tabs"]

Step 2: Allows users to open the side panel by clicking on the action toolbar icon


chrome.sidePanel

.setPanelBehavior({ openPanelOnActionClick: true })

.catch((error) => console.error(error));

chrome.tabs.onUpdated.addListener(async (tabId, info, tab) => {

if (!tab.url) return;

const url = new URL(tab.url);

await chrome.sidePanel.setOptions({

tabId,

path: 'sidepanel.html',

enabled: true

});

});

Microsoft Edge
Microsoft Edge
A Microsoft cross-platform web browser that provides privacy, learning, and accessibility tools.
2,144 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Q&A Assist
    2024-04-04T03:56:44.8133333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    The issue you are experiencing with your Edge extension's side panel showing the same data for all tabs can be resolved by setting the tabId property in the chrome.sidePanel.setOptions() method to the tabId of the current tab. This will ensure that the side panel is specific to the current tab and does not show the same data for all tabs. Here is an example code snippet that demonstrates this:

    chrome.tabs.onUpdated.addListener(async (tabId, info, tab) => {
      if (!tab.url) return;
      const url = new URL(tab.url);
      await chrome.sidePanel.setOptions({
        tabId,
        path: 'sidepanel.html',
        enabled: true
      });
    });
    

    Make sure to also include the "tabs" permission in your extension's manifest file to allow access to the chrome.tabs API.

    References:

    0 comments No comments

  2. Yu Zhou-MSFT 12,071 Reputation points Microsoft Vendor
    2024-04-04T07:54:30.4566667+00:00

    Hi @Vignesh kumar Ayyappan

    Do you mean you want to show the current tab URL in your sidebar extension? I don't see the code to get and show current tab URL in your sample. You could refer to the code below:

    sidepanel.html

    <!DOCTYPE html>
    <html>
      <head>
        <title>My Sidebar Extension</title>
      </head>
      <body>
        <h3>Click the button to get the page URL</h3><br>
        <button id="btn1">click me</button>
        <p id="txt1">
        <script src="sidebar.js"></script>
      </body>
    </html>
    

    sidebar.js

    Listen to onActivated event (fires when the active tab in a window changes) and onUpdated event (fired when a tab is updated) so that different URLs getting populated to different tabs that containing sidepanel.

    chrome.tabs.onActivated.addListener(getTabUrl);
    
    chrome.tabs.onUpdated.addListener(getTabUrl)
    
    var btn = document.getElementById("btn1");
    btn.addEventListener("click", getTabUrl);
    
    function getTabUrl() {
      chrome.tabs.query({ active: true, lastFocusedWindow: true }, function(tabs) {
        var tab = tabs[0];
        document.getElementById("txt1").innerText = tab.url;
      });
    }
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Regards,

    Yu Zhou