`alert` not showing in ManifestV3 background service worker

Viewed 1056

I am trying to make a chrome extension that alerts you in the tab that you are currently moving or highlighting. I have tried reading the chrome migrating to V.3 documentation and have come up with the following code, however, the alerts never appear. Does anybody know what I need to change or add?

My 'manifest.json' file:

{
    "manifest_version": 3,
    "name": "Alert",
    "version": "0.1",
    "description": "alerts you when doing tab functions",
    "permissions": ["tabs", "activeTab"],
    "host_permissions": ["<all_urls>"],
    "background": {
        "service_worker": "background.js"
    }
}

My 'background.js' file:

chrome.tabs.onMoved.addListener(function () {
    alert("You moved this tab");
});

chrome.tabs.onHighlighted.addListener(function () {
    alert("You highlighted this tab");
});

My working directory:

.
├── background.js
├── manifest.json
2 Answers

alert is not defined in a service worker per specification so we'll have to use console.log

Also, I was looking in the wrong place for the alert messages. I needed to look at the service worker link in my unpacked extension page. enter image description here

alert() method cannot be used outside of the browser environment, you can use console.warn() or console.error() instead. But is not a good solution if you want to show an error message to the extension user, as they would never open the console.

If you would like a more user friendly approach use the following:

chrome.notifications.create({
type: 'basic',
iconUrl: './Images/image_if_any.png',
title: `Notification title`,
message: "Your message",
priority: 1
})
Related