'Navigate tab to a URL and execute script inside

I am struggling to get this simple f-ty working... My scenario is:

  1. get current URL
  2. modify it
  3. navigate/redirect to it
  4. execute custom JS code there

The most problems I have is with 4)

manifest.json

{
  "name": "Hello, World!",
  "description": "Navigate and execute custom js script",
  "version": "1.0",
  "manifest_version": 3,
  "permissions": [
    "tabs",
    "activeTab",
    "scripting"
  ],
  "background": {
    "service_worker": "background.js"
  },
  "action": {}
}

background.js

function myCustomScript() {
    alert('myCustomScript test ok!');
    console.log('myCustomScript test ok!');
}

chrome.action.onClicked.addListener((tab) => {

    chrome.tabs.update({url: "https://example.com"}, myCustomScript);

});

The page got redirected but my js function is not executed! Do you know why and how to fix it?

P.S: this is my first time I am creating my chrome extension, maybe I am doing something wrong...



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source