从页面操作中获取标签网址(WebExtensions,Android) [英] Get tab URL from page action (WebExtensions, Android)

查看:94
本文介绍了从页面操作中获取标签网址(WebExtensions,Android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要在页面操作弹出窗口中获取当前选项卡的URL。

首先看起来很明显:只需使用标签 API。但是,如果我正确解密文档,这似乎不能在Android上使用。所以我一直在寻找其他东西,并找到 onClicked 事件的pageAction API。



pageAction API 似乎被列为与Android兼容,并且onClicked事件被标记为支持。这意味着它实际上会返回一个tabs.Tab对象。但它真的吗?有人试过吗?



检索网址的最佳方法是什么?我知道我可以使用内容脚本,并让它在每个标签中运行,并创建一个长期的消息传递连接,以便在请求时将URL发送到页面操作弹出窗口。但是这样会非常低效,并且使代码变得非常复杂,与使用选项卡API的容易程度相比。



还有什么我可以做的吗?

解决方案

当前(Firefox 54及更高版本)



标签 API 适用于Android版Firefox。这意味着您可以使用桌面Firefox可用的常规方法。具体来说, chrome.tabs。 query() browser.tabs.query() 。但是,您需要 activeTab 和/或选项卡 permissions manifest.json



chrome.tabs.query

  chrome.tabs.query({active:true,currentWindow:true},function(tabs){
//'tabs'将是一个只包含一个元素的数组:一个描述活动标签的对象
//在当前窗口中。
var currentTabUrl = tabs [0] .url;
});

browser.tabs.query



  browser.tabs.query({active:true,currentWindow:true})。then(function(tabs){
//'tabs'将是一个仅包含一个元素的数组:一个描述当前窗口中活动标签
//的对象
var currentTabUrl = tabs [0] .url;
} );



在Firefox 54之前



如果你定义了一个页面/浏览器动作弹出窗口



如果您已经为页面/浏览器动作定义了一个弹出窗口,那么 onClicked tabs.Tab 对象。获取标签信息的正常方法是从 tabs.query ,如您已经确定的那样,目前尚未在Android版Firefox中使用。



Android上适用于Firefox的 API 相当有限。对于你想要做的事,使用 webNavigation 事件来记录每个选项卡的框架0 URL将比每个页面中的内容脚本更有效。您可以使用 webNavigation。 onCommitted webNavigation.onCompleted 事件取决于您的需求。您需要假设当前窗口的活动选项卡是最近有一个 webNavigation 事件的活动选项卡,或者您也可以监视 webRequest 事件。然而,任何你做这件事的方式,你认为是当前标签的标签只是一个假设,在某些情况下不准确。



更准确的URL(和活动标签确定)需要使用内容脚本



如果正在访问的页面更改使用 webNavigation 事件不会为您提供更新后的网址。正如您已经确定的那样,在没有标签 API的情况下,当您定义实际的页面/浏览器操作弹出窗口时,唯一的方法就是确保您拥有该标签的实际当前URL因此不会得到一个 tabs.Tab 对象),就是将内容脚本插入每个页面。您将需要内容脚本来不断更新网址,或者使用 storage.onChanged ,用于从背景/弹出脚本中获取该信息的请求。从后台/弹出脚本内容脚本的通信必须通过存储 API完成,因为没有标签 API(即没有 $ b

使用页面/浏览器操作 onClicked



另一种方法是,我没有在Android上的Firefox上尝试过,将不定义实际的页面/浏览器操作弹出窗口。如果您没有定义实际的弹出窗口,您会收到 onClicked 事件(获取 tabs.Tab Object with both活动标签的ID和URL),然后可以打开一个伪弹出式菜单 1




<子> 1。在我的链接答案中打开一个伪弹出的实现使用 标签 windows 这些API目前都不适用于Android版Firefox。它可以被重新编写为使用上面提到的 webNavigation 监听器来跟踪标签URL并且 window.open()到打开用于伪弹出窗口的窗口。再次,我没有在Android上测试这个与Firefox的确定它实际上工作。


I would like to get the URL of the current tab within a page action popup.
At first it seems obvious: Just use the tabs API. But that doesn't seem to be available on Android if I deciphering the docs correctly. So I kept looking for something else and found the onClicked event of the pageAction API.

The pageAction API seems to be listed as compatible with Android and the onClicked event is marked as supported. So that implies that it would actually return a tabs.Tab object. But does it really? Has anyone tried it?

What is the best way to retrieve the URL? I know I could just use a content script and let that run in every single tab and create a long lived messaging connection to send the URL to the page action popup whenever it is requested. But that would be very inefficient and make the code insanely complicated compared to how easy it would be using the tabs API.

Is there anything else I could do?

解决方案

Current (Firefox 54 and later)

As of Firefox 54, the tabs API is available in Firefox for Android. This means you can use the normal methods available to desktop Firefox. Specifically, chrome.tabs.query() or browser.tabs.query(). However, you will need the activeTab and/or tabs permissions in your manifest.json.

chrome.tabs.query:

chrome.tabs.query({active:true,currentWindow:true},function(tabs){
    //'tabs' will be an array with only one element: an Object describing the active tab
    //  in the current window.
    var currentTabUrl = tabs[0].url;
});

browser.tabs.query:

browser.tabs.query({active:true,currentWindow:true}).then(function(tabs){
    //'tabs' will be an array with only one element: an Object describing the active tab
    //  in the current window.
    var currentTabUrl = tabs[0].url;
});

Prior to Firefox 54

If you have defined a page/browser action popup

If you have defined a popup for your page/browser action, then the onClicked event does not fire. Your popup is not passed any information when it is created/shown. Thus, you will not receive a tabs.Tab object. The normal way to obtain tab information is from tabs.query, which, as you have already determined, is not (yet) available in Firefox for Android.

The APIs available to Firefox on Android are quite limited. For what you are wanting to do, using webNavigation events to keep a record of each tab's frame 0 URL would be more efficient than a content script in every page. You could use the webNavigation.onCommitted or webNavigation.onCompleted events depending on your needs. You will need to assume that the active tab of the current window is the one which most recently had a webNavigation event, or perhaps you could also monitor webRequest events. However, any way that you do it, which tab you assume to be the current tab will just be an assumption, and will be inaccurate under some circumstances.

A more accurate URL (and active tab determination) requires using a content script

If the page that is being visited changes the tab's URL through some method that does not trigger navigation, using webNavigation events will not give you the updated URL. As you have identified, without the tabs API, the only way to be certain you have the actual current URL for the tab, when you define an actual page/browser action popup (and thus don't get a tabs.Tab object), is to inject a content script into every page. You will either need the content scripts to continuously update the URLs, or be listening with storage.onChanged for a request for that information from your background/popup script. Communication from the background/popup scripts to content scripts must be accomplished through the storage API due to not having the tabs API (i.e. no tabs.sendMessage).

Using page/browser action onClicked

An alternative, which I have not tried on Firefox on Android, would be to not define an actual page/browser action popup. If you don't define an actual popup, you receive the onClicked event (getting the tabs.Tab Object with both the active tab's ID and the URL) and then can open a pseudo-popup1.


1. The implementation of opening a a pseudo-popup in my linked answer uses the tabs and windows APIs which are both currently unavailable for Firefox for Android. It could be re-written to use the above mentioned webNavigation listener to track tab URLs and window.open() to open the window used for the pseudo-popup. Again, I have not tested this with Firefox on Android to determine that it actually works.

这篇关于从页面操作中获取标签网址(WebExtensions,Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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