Programmatically open a Chrome plugin's options.html page?
Is there a way to open a Google Chrome p开发者_开发技巧lugin's options.html page via Javascript in background.html?
There is a new method that is enabled beginning with Chrome 42:
chrome.runtime.openOptionsPage(function callback)
Open your Extension's options page, if possible.
The precise behavior may depend on your manifest's
options_ui
oroptions_page
key, or what Chrome happens to support at the time. For example, the page may be opened in a new tab, withinchrome://extensions
, within an App, or it may just focus an open options page. It will never cause the caller page to reload.If your Extension does not declare an options page, or Chrome failed to create one for some other reason, the callback will set
lastError
.
chrome.tabs.create({ url: "options.html" });
Update
Starting with version 40, Chrome now uses a new popup options dialog from the extension management page instead of dedicated options pages (which are being deprecated). You can still achieve the same effect with a modification to the URL.
chrome.tabs.create({ 'url': 'chrome://extensions/?options=' + chrome.runtime.id });
Open or switch to already opened options page (instead of opening a duplicate):
var optionsUrl = chrome.extension.getURL('options.html');
chrome.tabs.query({url: optionsUrl}, function(tabs) {
if (tabs.length) {
chrome.tabs.update(tabs[0].id, {active: true});
} else {
chrome.tabs.create({url: optionsUrl});
}
});
Without using the Chrome API, only the standard Web APIs, the following is possible:
window.open("chrome-extension://ghipmampnddcpdlppkkamoankmkmcbmh/options.html")
Or, to navigate from a visible page to an extension page:
location.href = "chrome-extension://ghipmampnddcpdlppkkamoankmkmcbmh/options.html"
This requires hardcoding the extension ID.
Probably the only time this is preferable over using the Chrome API is when it's called from a non-extension context (and not the original "from background page" scenario). However, note that a web context cannot navigate to a chrome-extension://*
page (it will result in about:blank
) unless it's declared as web-accessible.
In such a scenario, one should also consider communicating with the webpage either through a content script or external messaging instead.
精彩评论