dcotter
dcotter

Reputation: 332

Chrome Extension Opening in New Tab

I developed a Chrome Extension and in my Manifest file the permissions are "permissions": ["tabs", "storage"]. When the app got published it shows permissions as Replace the page you see when opening a new tab and Read your browsing history

The main issue is that every time a new tab it replaces the page with my extension.

In my background.js file I'm only using the tabs API to get the current tab's url like this:

const getCurrentTab = async () => {
    let queryOptions = { active: true, currentWindow: true };
    let [tab] = await chrome.tabs.query(queryOptions);

    const recipe = await fetchParsedRecipe(tab.url);

    return recipe;
};

This getCurrentTab function gets called in both tabs.onUpdated and tabs.onActivated

chrome.tabs.onUpdated.addListener(() => {
    getCurrentTab();
});

chrome.tabs.onActivated.addListener(() => {
    getCurrentTab();
});

How do I make sure that the extension doesn't open in the window of the new tab?enter image description here

Upvotes: 1

Views: 2456

Answers (1)

user1731468
user1731468

Reputation: 1000

If you do not want to replace your New Tab page with your page in your Chrome extension manifest v3. You can do the following, this will only open your HTML page when you click the browser button. And it returns the current tab URL to that page (as a query string).

manifest.json

{
  "name": "Open a new tab",
  "action": {},
  "manifest_version": 3,
  "version": "0.1",
  "description": "A click to open my own tab page",
  "permissions": ["storage", "activeTab", "scripting"],
  "background": {
    "service_worker": "background.js"
  },
  "host_permissions": ["<all_urls>"]
}

background.js

chrome.action.onClicked.addListener((tab) => {
  var newURL = chrome.runtime.getURL("tab.html");
  chrome.tabs.create({ url: newURL + "?=" + tab.url });
});

Upvotes: 0

Related Questions