如何使用实验性的offscreenTab API? [英] How to use the experimental offscreenTab API?

查看:97
本文介绍了如何使用实验性的offscreenTab API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找例子和参考,并没有提出任何建议。我在offscreenTab源代码中发现了一条提示,说明它无法从背景页面实例化(它没有与offscreenTab关联的选项卡)。在其他地方,我发现提及弹出窗口也没有与标签绑定。

I've been searching for examples and reference and have come up with nothing. I found a note in offscreenTab source code mentioning it cannot be instantiated from a background page (it doesn't have a tab for the offscreenTab to relate to). Elsewhere I found mention that popup also has no tie to a tab.

您如何在Chrome扩展程序中成功创建屏幕外Tab?

How do you successfully create an offscreenTab in a Chrome extension?

推荐答案

根据文档, offscreenTabs.create 将不能在后台页面中运行。虽然没有明确提到,但API也不能在内容脚本中使用。通过一个简单的测试,弹出窗口与后台页面具有相同的限制。

According to the documentation, offscreenTabs.create won't function in a background page. Although not explicitly mentioned, the API cannot be used in a Content script either. Through a simple test, it seems that the popup has the same limitation as a background page.

唯一的剩余选项是一个在Chrome扩展程序中运行的选项卡。最简单的方法是在后台/弹出窗口中使用以下代码:

The only leftover option is a tab which runs in the context of a Chrome extension. The easiest way to do that is by using the following code in the background/popup:

chrome.tabs.create({url: chrome.extension.getURL('ost.htm'), active:false});
// active:false, so that the window do not jump to the front

ost.htm 是一个帮助页面,它可以创建标签:

ost.htm is a helper page, which creates the tab:

chrome.experimental.offscreenTabs.create({url: '...'}, function(offscreenTab) {
    // Do something with  offscreenTab.id !
});

要更改网址,请使用 chrome.experimental.offscreenTabs.update

To change the URL, use chrome.experimental.offscreenTabs.update.

offscreenTab.id 是一个tabId,它应该与 chrome.tabs API。但是,至少在Chrome 20.0.1130.1中,情况并非如此。 标签 API的所有方法都无法识别返回的标签ID。

offscreenTab.id is a tabId, which ought to be used with the chrome.tabs API. However, at least in Chrome 20.0.1130.1, this is not the case. All methods of the tabs API do not recognise the returned tabID.

解决方法是将内容脚本使用清单文件,例如:

A work-around is to inject a content script using a manifest file, eg:

{"content_scripts": {"js":["contentscript.js"], "matches":["<all_urls>"]}}
// contentscript.js:
chrome.extension.sendMessage({ .. any request .. }, function(response) {
    // Do something with response.
});

背景页面的附录:

Appendum to the background page:

chrome.extension.onMessage.addListener(function(message, sender, sendResponse) {
    // Instead of checking for index == -1, you can also see if the ID matches
    // the ID of a previously created offscreenTab
    if (sender.tab && sender.tab.index === -1) {
        // index is negative if the tab is invisible
        // ... do something (logic) ...
        sendResponse( /* .. some response .. */ );
    }
});

使用内容脚本,您可以完全访问页面的DOM。但不是全球对象。您必须注入脚本(请参阅 script>>这个答案)。

With content scripts, you've got full access to a page's DOM. But not to the global object. You'll have to inject scripts (see this answer) if you want to run code in the context of the page.

另一个可能有用的API是 chrome.webRequest API。它可以用来修改标题/中止/重定向请求。注意:它不能用于读取或修改响应。

Another API which might be useful is the chrome.webRequest API. It can be used to modify headers/abort/redirect requests. Note: It cannot be used to read or modify the response.

目前, offscreenTabs API是实验性的。要使用它,你必须通过 chrome:// flags 启用实验API,并添加permissions:[experimental] 到您的清单文件。一旦它不再是实验,请使用permissions:[offscreenTabs]

Currently, the offscreenTabs API is experimental. To play with it, you have to enable the experimental APIs via chrome://flags, and add "permissions":["experimental"] to your manifest file. Once it's not experimental any more, use "permissions":["offscreenTabs"].

这篇关于如何使用实验性的offscreenTab API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆