Skip to content Skip to sidebar Skip to footer

Update And Run Pre Defined Link On Current Tab Chrome Extension

I'm trying to make a chrome extension go to a predefined link on the currently open tab. I can't figure out how to do this. Any ideas?

Solution 1:

Add the following code in the background.js, Which handles the click event on extension.

chrome.browserAction.onClicked.addListener(() => {
    chrome.tabs.query({ active: true }, (tabs) => {
        const tab = tabs[0];
        chrome.tabs.update(tab.id, {
            url: 'https://stackoverflow.com/questions/68945425/update-and-run-pre-defined-link-on-current-tab-chrome-extension',
        });
    });
});

Code Explanation

  1. On-click on the extension, I fetch the active tab information using chrome.tabs.query API.
  2. chrome.tabs.query will return an array of tabs.
  3. Get the 0'th indexed tab from the array and update the URL as per your requirement using chrome.tabs.update API.

Referencehttps://developer.chrome.com/docs/extensions/reference/tabs/#method-update

Post a Comment for "Update And Run Pre Defined Link On Current Tab Chrome Extension"