Chrome extension: how to get current webpage url from background.html

According to my information, this is not possible immediately after receiving tab.url (it is possible only in popup.html), and when sending messages it also requires that popup.html be open. Is there any way around this and get the current page url from background.html?

My best snapshot was with message passing, which I used this code in background.html

var bg = chrome.extension.getPopupPage(); var myURL = bg.myURL; 

then in popup.html I had:

  chrome.tabs.getSelected(null, function(tab) { var myURL = tab.url; }) 

In any case, the above does not work at all. Does anyone know a way to do this without opening a popup?

+6
source share
4 answers

chrome.tabs.query supported from background pages, of course, if you have tabs permission. This is a supported route with Chrome 19.

 chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) { var tab = tabs[0]; var url = tab.url; }); 

Note that currentWindow necessary because otherwise it returned the active tab for each window. This should ensure that you return only one tab.

Of course, keep in mind that this is an asynchronous API - you cannot access any data that it provides, except from the callback function. You can store values ​​(e.g. url here) in a higher scope, so another function can access it, but when making a callback, this will only provide the correct result.


(Below is my initial answer for posterity - this method is no longer needed, it requires an always-running background page, and getSelected() deprecated.)

First put this in background.html and make the myURL variable global:

 var myURL = "about:blank"; // A default url just in case below code doesn't work chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { // onUpdated should fire when the selected tab is changed or a link is clicked chrome.tabs.getSelected(null, function(tab) { myURL = tab.url; }); }); 

Then run this in popup.html if you want to get the page url:

 chrome.extension.getBackgroundPage().myURL; 

So, if I were to appear inside the popup and I go to Google and click on your page or browser action, I will see http://google.com/webhp in the popup.

+12
source

When I saw this post, I felt that there should be a way to mark the discussion as "obsolete."

Causes...

This question should migrate to manifest v2 and ...
The answers both do not work. I am using select onchange and posting the current tab url which is not working.

Perhaps they all work in manifest v1.

My answer...

 var myURL = "not set yet"; window.addEventListener('load', function () { chrome.tabs.getSelected(null,function(tab){ myURL=tab.url; }); 
+2
source
 chrome.tabs.getSelected(null, function(tab) { var myURL = tab.url; }); 

I do not understand, the above code can be used on the original page to get the current tab url.

+1
source

This is a bit more work, but works like a charm ...

I would use a content script; it is relatively simple and allows you to get any information from the current page that you might want. Ask the background image to β€œpaste” the script into the current web page to gather the necessary information. The script then simply brings it back to the background.

background.js:

 // Icon is clicked and triggers content script to be injected into current webpage chrome.browserAction.onClicked.addListener(function(tab) { chrome.tabs.executeScript(null, { file: 'inject.js' }); }); // Listens for message back from content script and then runs chrome.runtime.onMessage.addListener(function (request) { var URL = request.url; }); 

inject.js (contents of script):

 // Gathers up in the information that you need from webpage var pageInfo = { "url": window.location.href }; // Sends the information back to background.js chrome.runtime.sendMessage(pageInfo); 

Hope this helps someone!

0
source

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


All Articles