0

I am trying to create a Chrome extension that automatically generates a small pop up on the side when the user navigates to a particular site.

I went through the documentation on Chrome's developer page & followed the tutorial but I am not sure how to inject a script when the user visits a particular site. I want the popup to show up only on that particular site. In fact I don't see any console logs inside the addListener section of my background.json

manifest.json

{
    "name": "Google helper",
    "version": "1.0",
    "manifest_version": 3,
    "description": "Shows you additional information on google",
    "icons": {
        "128": "icon128.png"
    },
    "action": {
        "default_icon": {
            "128": "icon128.png"
        },
        "default_title": "Google helper",
        "default_popup": "popup.html"
    },
    "background": {
        "service_worker": "background.js"
    },
    "permissions": [
        "tabs",
        "activeTab",
        "scripting"
        // ,
        // "https://google.com/"
    ]
}

background.js

// Register the service worker
console.log("Here in background.js before anything starts"); // Able to see this log in service worker view


// Called when the user clicks on the action.
chrome.action.onClicked.addListener(function(tab) {
  // No tabs or host permissions needed!
  console.log('Turning ' + tab.url + ' red!'); // Don't see this in logs
  chrome.scripting.executeScript({
    code: 'document.body.style.backgroundColor="red"'
  });
});
Piyush
  • 606
  • 4
  • 16
  • 38
  • This is the tutorial for that. [How to make Chrome Extension 04 How to use Service Worker](https://youtu.be/zdUptukxFzo) – Norio Yamamoto Feb 15 '23 at 06:42
  • 1
    Remove `background`, `permissions`, `action` and simply declare a [content script](https://developer.chrome.com/extensions/content_scripts) for this site, then add your stuff as a DOM element into the page. – wOxxOm Feb 15 '23 at 06:52
  • Thanks @wOxxOm. That worked. I want to produce a popup automatically when the user navigates to the site. Should I still be manipulating the DOM or rather injecting the popup via a script? – Piyush Feb 21 '23 at 06:08

0 Answers0