javascript 以编程方式打开 Chrome 插件的 options.html 页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6782391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 21:51:12  来源:igfitidea点击:

Programmatically open a Chrome plugin's options.html page?

javascriptgoogle-chromegoogle-chrome-extension

提问by davidscolgan

Is there a way to open a Google Chrome plugin's options.html page via Javascript in background.html?

有没有办法通过 background.html 中的 Javascript 打开 Google Chrome 插件的 options.html 页面?

回答by Xan

There is a new method that is enabled beginning with Chrome 42:

从 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_uior options_pagekey, or what Chrome happens to support at the time. For example, the page may be opened in a new tab, within chrome://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.runtime.openOptionsPage(function callback)

如果可能,打开扩展程序的选项页面。

确切的行为可能取决于您的清单options_uioptions_page密钥,或者当时 Chrome 恰好支持什么。例如,页面可能会在新选项卡、chrome://extensions应用程序内、应用程序内打开,或者它可能只关注打开的选项页面。它永远不会导致调用者页面重新加载。

如果您的扩展程序未声明选项页面,或者 Chrome 由于其他原因未能创建选项页面,则回调将设置lastError.

回答by serg

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.

从版本 40 开始,Chrome 现在使用扩展管理页面中的新弹出选项对话框,而不是专用选项页面(已弃用)。您仍然可以通过修改 URL 来达到相同的效果。

chrome.tabs.create({ 'url': 'chrome://extensions/?options=' + chrome.runtime.id });

回答by NARKOZ

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});
    }
});

回答by Joseph Marikle

Without using the Chrome API, only the standard Web APIs, the following is possible:

不使用 Chrome API,只使用标准的 Web API,以下是可能的:

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.

这需要对扩展 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.

可能唯一比使用 Chrome API 更可取的是从非扩展上下文(而不是原始的“来自后台页面”场景)调用它。但是,请注意,除非chrome-extension://*将Web 上下文声明为 Web-accessible ,about:blank否则Web 上下文无法导航到页面(它将导致)。

In such a scenario, one should also consider communicating with the webpage either through a content script or external messaginginstead.

在这种情况下,还应该考虑通过内容脚本或外部消息与网页进行通信