Chrome extension opens a new tab in a new tab

I created a Chrome extension that, as part of its operation, opens a new tab with the specified URL.

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    if( request.message === "open_new_tab" ) {
      chrome.tabs.create({"url": request.url});
    }
  }
);

      

(Full code available on GitHub )

This works fine on tabs with web pages, but I can't get it to work with empty tabs, e.g .: chrome://apps/

To clarify if there is a tab open and it's on stackoverflow.com then when I click my extension button opens a new tab. by loading the generated url. When I go to a new tab or tab where the url starts with chrome://

, the extension doesn't work.

What permissions need to be enabled for the extension to open in ANY tab? Include new tabs and tab chrome://

?

manifest.json:

{
  "manifest_version": 2,

  "name": "MyMiniCity Checker",
    "short_name": "MyMiniCity Checker",
  "description": "Checks what your city needs most and redirects the browser accordingly.",
  "version": "0.2",
    "author":"Richard Parnaby-King",
    "homepage_url": "https://github.com/richard-parnaby-king/MyMiniCity-Checker/",
    "icons": {
      "128": "icon-big.png"
   },

    "options_page": "options/options.html",

  "browser_action": {
    "default_icon": "icon.png"
  },
    "permissions": ["tabs","storage","http://*.myminicity.com/","http://*/*", "https://*/*"],
    "background": {
    "scripts": ["background.js"],
    "persistent": false
    },
    "content_scripts": [ {
    "matches": [ "http://*/*", "https://*/*"],
    "js": [ "jquery-1.11.3.min.js" ]
  }]
}

      

Background.js:

//When user clicks on button, run script
chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.tabs.executeScript(null, { file: "jquery-1.11.3.min.js" }, function() {
    chrome.tabs.executeScript(null, { file: "contentscript.js" });
    });
});

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    if( request.message === "open_new_tab" ) {
      chrome.tabs.create({"url": request.url});
    }
  }
);

      

It seems that the background.js file is not being executed. I suspect these are permissions. What permissions are required to run this extension on each tab?

+3


source to share


2 answers


Since I wanted this to execute on EVERY page, this meant that I could not have the code in the content of the script. I moved all the code to the background script:

chrome.browserAction.onClicked.addListener(function(tab) {
        //... 
        chrome.tabs.create({"url": newTabUrl});
        //...
});

      



So when I click on my button the above code is called using a private jquery script.

+2


source


Well this message must come from the content of the script you are trying to enter into the current tab.

The most accessible permission you can request , "<all_urls>"

however there are still URLs that are excluded from access.

  • Typically, you can access the circuits http:

    , https:

    , file:

    and ftp:

    .

  • file:

    requires the user to manually approve access to chrome://extensions/

    :



<code> file: </code>
      <br>
        <script async src=
access" data-src="/img/57c8675af1f69a9b866fe87401665867.png" class=" lazyloaded" src="https://fooobar.com//img/57c8675af1f69a9b866fe87401665867.png">

  1. Chrome Web Store URLs are specifically blacklisted for security reasons. There is no override.

  2. chrome://

    URLs (also called WebUIs) are excluded for security reasons . There is a manual override in the flags: chrome://flags/#extensions-on-chrome-urls

    but you can never expect it to be there.

  3. There is an exception to the above, chrome://favicon/

    URLs are available if you claim exact permission.

In general, even with the broadest rights, you cannot be sure that you have access. Check chrome.runtime.lastError

in the callback executeScript

and finish gracefully.

+7


source







All Articles