1

i'm trying to create a new tab from Firefox extension, but it doesn't works.

manifest.js:

{
  "manifest_version": 2,
  "name": "nafy",
  "version": "1.0",
  "description": "NaFy",

  "icons": {
  "48": "icons/icon_48.png",
  "96": "icons/icon_96.png"
  },
  
  "content_scripts": [
   {
        "matches": ["*://*.ebay.de/*"],
        "js": ["background.js"]
   }
  ],
  
  "permissions": [
    "tabs"
  ]
}

background.js:

createNewTab();


function onCreated(tab) {
    console.log('Created new tab: ${tab.id}');
}

function onError(error) {
    console.log('Error: ${error}');
}

function createNewTab()
{
    let newTab = browser.tabs.create({
         url:"https://www.ebay.de"
    });
    newTab.then(onCreated, onError);
};

What I'm doing wrong? (Everything works as expected in Chrome.)

vitaliy-zh
  • 185
  • 9
  • 1
    This is not a background script, this is a content script, which isn't allowed to use `tabs` API. In content script you can use window.open. You can also send a message to your real background script declared in `background` section of manifest.json, and then the real backgournd script can use browser.tabs.create. – wOxxOm Oct 07 '22 at 13:49

1 Answers1

1

Your background.js file is not actually a background script despite it's name, it's defined in your manifest.json as a content script.

Content scripts don't have access to the tabs API (see list of accessible APIs here).

To fix that, you need to move your background.js to be an actual background script:

{
  "manifest_version": 2,
  "name": "nafy",
  "version": "1.0",
  "description": "NaFy",
  "icons": {
    "48": "icons/icon_48.png",
    "96": "icons/icon_96.png"
  },
  "background": {
    "scripts": ["background.js"]
  },
  "permissions": [
    "tabs"
  ]
}
Hamatti
  • 1,210
  • 10
  • 11