Question

I have the following code in my chrome extension content script:

window.open(chrome.extension.getURL("options.html"),'_blank');

But whenever I try this, I just get a new tab with about:blank, not one with my options page.

When I run the same code (through the developer tools console) on the options page, it works properly. If I launch an alert with the text chrome.extension.getURL("options.html"), it works as expected.

This chrome documentation page suggests that what I'm doing should work correctly. So, how should I go about trying to fix this?

EDIT:

I believe this has something to do with the content security policy.

  • If I inject a link to the page and just click on it, I get about:blank
  • If I inject a link and I right-click, and then either open in new tab/window or copy URL, it works fine.
  • If I inject a redirect, I get redirected to about:blank
  • If I inject the text of the URL anywhere, it works fine.
  • If I inject the exact URL (no chrome.extension.getURL, etc.), from ANY non-extension page, it doesn't work.

So I guess my new (more general) question is, how can I launch my options page from a content script?

Was it helpful?

Solution

In order to be able to use your HTML-file (or any other resource) outside of the context of your extension (e.g. in a web-page), you need to add the target HTML file in the web_accessible_resources section of your manifest. E.g.:

Extension file-structure:

          root-dir/
           |_____manifest.json
           |_____content.js
           |_____options.html

content.js:

/* Append a link to the web-page's body */
var a = document.createElement("a");
a.href = chrome.extension.getURL("options.html");
a.target = "_blank";
a.textContent = "My HTML file";
document.body.appendChild(a);

manifest.json:

{
    "manifest_version": 2,
    "name":    "Test Extension",
    "version": "0.0",
    "offline_enabled": true,

    "content_scripts": [{
        "matches":    ["*://*/*"],
        "js":         ["content.js"],
        "run_at":     "document_end",
        "all_frames": false
    }],

    "web_accessible_resources": ["options.html"]
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top