Home > Software engineering >  Migration issue Manifest-2 to Manifest-3 ( service worker inactive )
Migration issue Manifest-2 to Manifest-3 ( service worker inactive )

Time:03-07

I am migrating my chrome extension from manifest version 2 to 3. There is a problem in injecting the file with the click of the extension icon.

Manifest.json
{
    "manifest_version": 3,    
    "name": "Name of Extension",
    "description": "description",
    "version": "1.0.0",
    "background": {
        "service_worker": "background.js"
    },
    "permissions": [
        "activeTab",
        "scripting"
    ]
}

Background.js - Manifest version 3 ( This code is not working )

chrome.action.onClicked.addListener(function() {
    chrome.scripting.executeScript({
      files: ['"function.js"']
    });
});

background.js - Manifest version 2

chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.tabs.executeScript(tab.id, {file: "function.js"});
});

CodePudding user response:

  1. Use tab parameter as described in the documentation for onClicked
  2. Specify tab's id as described in the documentation for executeScript
  3. Remove the nested quotes in the file name
chrome.action.onClicked.addListener(tab => {
  chrome.scripting.executeScript({
    target: {tabId: tab.id},
    files: ['function.js'],
  });
});
  • Related