2

I want to get url for use it in variable with an chrome extension. I read here this code :

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

but it doesn't work on this url

http://sail.zezo.org/clipperton-noumea/chart.pl?lat=-15.93361&lon=-176.0022&userid=1577252

manifest.json:

{ "manifest_version": 2, "name": "Route zezo.org", "version": "1.0", "description": "Extraire la route proposée", "permissions": [ "http://sail.zezo.org/*", "tabs" ], "icons": { "16": "icon-16.png", "48": "icon-48.png", "128": "icon-128.png" }, "browser_action": { "default_icon": "icon.png", "default_popup": "popup.html" } } 

I don't understand where I make a mistake ...

(Sorry for my bad English ...)

8
  • 1
    Rightclick the popup, then Inspect and you'll see the console. Also, make sure to use the variable only inside the callback as chrome.* API is asynchronous. Commented Mar 26, 2017 at 19:14
  • @wOxxOm Ok thanks, I didn't now rightclik -> Inspect ... It works fine ;) But if I want to exploit the variable tabURL after ? I need to link var url of the script popup.js to var tabURL of the script background.js Commented Mar 27, 2017 at 6:25
  • You want to pass your variable to background page ? You need to use message passing : developer.chrome.com/extensions/messaging Commented Mar 27, 2017 at 7:04
  • No, I want to pass the variable in background.js to popup.js But it's the same I think Commented Mar 27, 2017 at 8:55
  • Perhaps with chrome.runtime.getBackgroundPage() I have only this in my background.js chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) { var tabURL = tabs[0].url; }); Commented Mar 27, 2017 at 9:19

1 Answer 1

3

OK so I write you the full solution to get the the active url in popup.js:

manifest.json:

{ "manifest_version": 2, "name": "Route zezo.org", "version": "1.0", "description": "Extraire la route proposée", "permissions": [ "http://sail.zezo.org/*", "tabs" ], "content_scripts": [ { "matches": ["http://*/*", "https://*/*"], "js": ["content.js"], "run_at": "document_end" }], "icons": { "16": "icon-16.png", "48": "icon-48.png", "128": "icon-128.png" }, "browser_action": { "default_icon": "icon.png", "default_popup": "popup.html" }, "background": { "scripts": ["background.js"] } } 

popup.js

window.onload=function(){ chrome.runtime.getBackgroundPage(function(backgroundPage){ var currentUrl = backgroundPage.tabURL; //Use the url ........ console.log(currentUrl); }) }; 

background.js

var tabURL = ""; chrome.runtime.onMessage.addListener( function(message, sender, sendResponse) { chrome.tabs.query({active: true, currentWindow: true}, function(arrayOfTabs) { var activeTab = arrayOfTabs[0]; tabURL = activeTab.url; }); } ); 

content.js

chrome.runtime.sendMessage("send"); 

The idea: your extension is run in any url you put in 'matches' in your manifest. The content script send message to background page that you want to get this url , the background page get the message and set it in the variable and then you take this variable in your popup.js page . now you could do whatever you want with this variable .

Good luck!

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot ! I test this tonight ;)
It's works perfectly ;)) Thanks again OriEng, one question ... it's a "generic" code ? I can use it for all ( get url ) ?
@GeGaX Happy to help ! What you mean by "generic" in this case ? It's work for every url right now but you could change it simply by changing the 'matches' in your manifest .
'Generic' in the sense that the code doesn't change, it can be applied everywhere (I keep it under the elbow thank you again) ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.