0

I'm trying to load an HTML file from my extension's folder into a new tab. There is no URL in this tab. Why doesn't Content Script work in it?

Here is my extension code:

manifest.json

{
    "manifest_version": 2,

    "name": "Content Script test",
    "description": "Content Script test.",
    "version": "1.0",
    
 "icons": {
  "16": "icon-16.png",
  "48": "icon-48.png"
 },
 
 "permissions": [
  "tabs"
 ],
    
    "background": {
     "persistant": false,
        "scripts": ["background.js"]
    },
    
    "content_scripts": [
     {
      //"matches": ["file:///*/inject.html"],
      // Content Script works only for non-empty URL's (stackoverflow.com for example).
      "matches": ["<all_urls>"],
      "js": ["content.js"]
     }
    ],
    
    "web_accessible_resources": [
     "inject.js"
    ],

    "browser_action": {
        "default_icon": "icon-16.png",
        "default_title": "Contents Script test"
    }
}

background.js

chrome.browserAction.onClicked.addListener(function ()
 {
  // Address bar in new tab will be empty.
  chrome.tabs.create(
   {
    url: chrome.extension.getURL('inject.html'),
    active: true
   },
   function (tab)
    {
     chrome.tabs.sendMessage(tab.id, {});
    });
 });

Content Script doesn't work on empty URL but on other URL's works fine. Sending message not working in new tab with empty url.

content.js

alert('test content');

//debugger;

chrome.runtime.onMessage.addListener(function (msg, sender, sendResp)
 {
  alert('test msg');
 });

inject.html

<html>
 <head>
 </head>
 <body>
  <p>Hello</p>
 </body>
</html>
JonMv
  • 21
  • 3
  • The [documentation of `` is pretty clear](https://developer.chrome.com/docs/extensions/mv3/match_patterns/): "Matches any URL that uses a permitted scheme." If the URL is truly blank, then it doesn't have a permitted scheme and therefore doesn't match. – Heretic Monkey Oct 24 '22 at 21:14
  • Content scripts are for web pages, but the newtab page is a chrome UI page and its real internal URL is `chrome://newtab/`. You can't run a content script there (well, [usually](https://stackoverflow.com/q/19042857)). Instead you can [override](https://developer.chrome.com/extensions/override) the newtab page with your own HTML. – wOxxOm Oct 24 '22 at 21:38

0 Answers0