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

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

问题描述

我想在页面操作弹出窗口中获取当前选项卡的 URL.
起初似乎很明显:只需使用 tabs API.但如果我正确解读文档,这似乎不适用于 Android.所以我一直在寻找其他东西,并找到了 onClicked pageAction API 的事件.

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.

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

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?

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

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?

推荐答案

当前(Firefox 54 及更高版本)

从 Firefox 54 开始,标签 API 在 Firefox for Android 中可用.这意味着您可以使用桌面 Firefox 可用的常规方法.具体来说,chrome.tabs.query()browser.tabs.query().但是,您将需要 activeTab 和/或 tabs permissions 在你的 manifest.json.

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

Firefox 54 之前

如果您定义了页面/浏览器操作弹出窗口

如果您为页面/浏览器操作定义了弹出窗口,则 onClicked 事件不会触发.您的弹出窗口在创建/显示时不会传递任何信息.因此,您不会收到 tabs.Tab 对象.获取标签信息的正常方法是从 tabs.query,正如您已经确定的,它在 Firefox for Android 中(还)不可用.

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.

适用于 Android 版 Firefox 的 API 非常有限.对于您想要做的事情,使用 webNavigation 事件记录每个选项卡的第 0 帧 URL 将比每个页面中的内容脚本更有效.您可以使用 webNavigation.onCommittedwebNavigation.onCompleted 事件取决于您的需要.您需要假设当前窗口的活动选项卡是最近发生 webNavigation 事件的那个,或者您也可以监视 webRequest 事件.但是,无论您采用哪种方式,您假定哪个标签是当前标签都只是一种假设,并且在某些情况下不准确.

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.

如果正在访问的页面通过某种不会触发导航的方法更改了选项卡的 URL,则使用 webNavigation 事件不会为您提供更新后的 URL.正如您所确定的,在没有 tabs API 的情况下,当您定义实际的页面/浏览器操作弹出窗口(因此不会得到一个 tabs.Tab 对象),用于将内容脚本注入每个页面.您将需要内容脚本来持续更新 URL,或者使用 storage.onChanged 用于从您的背景/弹出脚本中请求该信息.后台/弹出脚本内容脚本的通信必须通过storage API完成,因为没有标签 API(即没有 tabs.sendMessage).

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).

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

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.在我的链接答案中打开伪弹出窗口的实现使用 <代码>标签windows API 目前在 Firefox for Android 中均不可用.可以重写它以使用上面提到的 webNavigation 侦听器来跟踪选项卡 URL,并使用 window.open() 打开用于伪弹出窗口的窗口.同样,我还没有在 Android 上使用 Firefox 进行测试以确定它确实有效.

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.

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

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