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.
So I guess my new (more general) question is, how can I launch my options page from a content script?
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"]
}