vitaliy-zh
vitaliy-zh

Reputation: 205

Create new tab from Firefox extension doesn't works

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.)

Upvotes: 1

Views: 571

Answers (1)

Hamatti
Hamatti

Reputation: 1220

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"
  ]
}

Upvotes: 1

Related Questions