Get tab url from page action (WebExtensions, Android)

I would like to get the URL of the current tab in the popup page popup.
This seems obvious at first: use the tabs API . But this is not like Android if I decrypt the documents correctly. So I continued to search for something else and found the onClicked pageAction API event.

The pageAction API appears to be listed as compatible with Android, and the onClicked event is marked as supported. This means that it will actually return a tabs.Tab object. But is it really? Has anyone tried it?

What is the best way to get the url? I know that I can just use the content script, and let it run on every single tab and create a long-lasting messaging connection to send the url to the page popup on every request. But that would be very inefficient and make the code insanely complicated compared to how easy it would be to use the tabs API.

Is there anything else I could do?

+6
source share
2 answers

Current (Firefox 54 and later)

Firefox 54, Firefox Android tabs API. , , Firefox. , chrome.tabs.query() browser.tabs.query(). activeTab / tabs permissions 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 Android.

API Firefox Android, . , webNavigation , 0 URL- , script . webNavigation.onCommitted webNavigation.onCompleted . , , webNavigation, , , webRequest. , , , , , , .

URL ( ) script

URL- - , , webNavigation URL-. , API tabs, , URL- , / (, , tabs.Tab), , script . URL- storage.onChanged /popup script. background/popup API storage - API tabs (.. no tabs.sendMessage).

/ onClicked

, Firefox Android, /. , onClicked ( tabs.Tab URL-), -popup 1.


< > 1. - tabs windows API, Firefox Android. , webNavigation URL- window.open(), , - . , Firefox Android, , .

+4

webextensions. , , " " ( )

var activeTabPromise = browser.tabs.query({active: true, currentWindow: true});
      activeTabPromise.then((tabs) => {

          console.log(tabs[0].url);
      });

, ,

0

Source: https://habr.com/ru/post/1014560/


All Articles