0

I'm trying to run a simple alert on newly created tabs.
A content script which alerts 'hello' works perfectly if the page is on a server for example http://www.google.com, but it doesn't work if the HTML page is hosted in the extension

manifest.json

{
    "name" : "Alerter",
    "description" : "Opens an alert !",
    "version" : "1.0",
    "manifest_version" : 2,
    "background" : {
        "scripts" : ["background.js"],
        "persistent" : false
    },
    "content_scripts": [
        {
            "matches": ["<all_urls>"],
            "js": ["/js/content_script.js"]
        }
    ],
    "permissions" : ["tabs"]
}

content_script.js

alert('Hello!');

background.js

chrome.tabs.create({url:"/html/test.html"});
chrome.tabs.create({url:"http://www.google.com"});  

test.html

<b> I am a file locally hosted on extension ! </b>

You will notice that the page of Google alerts a greeting message, while test.html doesn't.

Why is this happening, and how to execute the alert on 'test.html' ?

Edit based on 'valuable' post's comments:
If the documentation is wrong and we cannot inject code to chrome-extension urls, is there a workaround or alternative to achieve the same target ?

Ashraf Bashir
  • 9,686
  • 15
  • 57
  • 82
  • 2
    Content scripts don't run on `chrome-extension:` urls, despite what the docs may say http://stackoverflow.com/questions/10196258 – rsanchez Nov 20 '13 at 15:24
  • I'm sure @rsanchez is correct, but it might be worth trying adding chrome-extension://*/* as an explicit match just in case. – pinoyyid Nov 20 '13 at 15:42
  • @pinoyyid, I already tried it, but this also failed :( – Ashraf Bashir Nov 20 '13 at 15:46
  • Your content script won't run but why would you need it to? Just put your js directly into `test.html`. – hiattp Nov 20 '13 at 15:56
  • @hiattp, I've filtered the issue details to create a basic scenario for readability, the real code has a more complex scenario where I want to inject code on locally hosted pages, and the real injected code will be available only in runtime (i.e. I don't know it in advance, it's a code retrieved from backend in runtime !). So the main question is why despite it's clearly stated in documentation that it can be injected on chrom-extension: this isn't happening ? – Ashraf Bashir Nov 20 '13 at 16:04
  • 1
    @AshrafBashir As I told you above, the documentation is just wrong. If you read the answers and comments to the question I linked you'll find a link to an open issue in Chromium to correct the documentation. – rsanchez Nov 20 '13 at 16:12
  • @rsanchez Isn't there any workaround ? or an alternative to inject the code in chrome-extension urls ? – Ashraf Bashir Nov 20 '13 at 16:19
  • I don't get why you need to run a content script on a page from an extension you own. You can simply add a ` – rsanchez Nov 20 '13 at 16:28
  • Kindly check my reply to @hiattp above .. quote: "I don't know the injected code in advance, it's a code retrieved from backend in runtime" – Ashraf Bashir Nov 20 '13 at 16:40
  • It seems like you'll have to achieve this with messages – hiattp Nov 20 '13 at 17:02

0 Answers0