Get current url from chrome.contextMenus.onClicked listener

I am building my first Chrome extension and I need help. I think that everything works, except that I cannot get the current tab URL.

var menu = chrome.contextMenus.create({ "title": "extension", "contexts": ["all"] }); chrome.tabs.query({'active': true, 'lastFocusedWindow': true}, function (tabs) { var siteUrl = tabs[0].url; }); chrome.contextMenus.onClicked.addListener(function(activeTab) { chrome.tabs.query({'active': true, 'lastFocusedWindow': true}, function (tabs) { var siteUrl = tabs[0].url; }); var finalUrl = "http://example.com/"; finalUrl += encodeURI(siteUrl); // Open the page up. chrome.tabs.create( { "url" : finalUrl } ); }); 

Can anybody help me? Thanks.

EDIT:

Thank you for your responses. I earned by moving

 var finalUrl = "http://example.com/"; finalUrl += encodeURI(siteUrl); // Open the page up. chrome.tabs.create( { "url" : finalUrl } 

Inside

 chrome.tabs.query({'active': true, 'lastFocusedWindow': true}, function (tabs) { var siteUrl = tabs[0].url; }); 
+6
source share
4 answers

The information you requested is provided to you in the onClicked listener onClicked .

 chrome.contextMenus.onClicked.addListener(function(info, tab) { // The URL of the tab (if any) var tabURL = tab && tab.url; // The URL of the page (if the menu wasn't triggered in a frame) var pageURL = info.pageUrl; // The URL of the frame (if the menu was triggered in a frame) var frameURL = info.frameUrl; 

eg. you can achieve what you want:

manifest.json:

 { "manifest_version": 2, "name": "Test Extension", "version": "0.0", "background": { "persistent": false, "scripts": ["background.js"] }, "permissions": ["contextMenus"] } 

background.js:

 var baseURL = 'http://example.com/'; chrome.contextMenus.create({ id: 'myMenu', // <-- event-pages require an ID title: 'Do cool stuff', contexts: ['all'] }, function () { /* It is always a good idea to look for errors */ if (chrome.runtime.lastError) { alert('ERROR: ' + chrome.runtime.lastError.message); } }); chrome.contextMenus.onClicked.addListener(function(info, tab) { /* Check which context-menu was triggered */ if (info.menuItemId === 'myMenu') { /* Get the URL of the frame or (if none) the page */ var currentURL = info.frameUrl || info.pageUrl; /* Open a new tab */ chrome.tabs.create({ url: baseURL + encodeURI(currentURL) }); } }); 
+6
source
 chrome.tabs.getCurrent(function(tab){ alert(tab.url); }); 

OR if you are in the content of the script,

 alert(document.location.href); 
+12
source

If you are using content script, you can use

 document.location.href 

document.location is an object and can provide a set of useful snippets in a URL

  • document.location.host returns the ex domain name: " http://www.google.com/ "
  • document.location.path returns the part after the domain name
  • document.location.hash returns the value after the # character in the URL
+7
source

Functions:

 function getCurrentUrl(callBackFuntion){ //you are in content scripts if(null == chrome.tabs || null == chrome.tabs.query){ callBackFuntion(document.location.href); }else{ //you are in popup var queryInfo = { active: true, currentWindow: true }; chrome.tabs.query(queryInfo, function(tabs) { var tab = tabs[0]; callBackFuntion(tab.url); }); } } 

Function call:

 function alertUrl(url){ console.log("currentUrl : " + url); } getCurrentUrl(alertUrl); 
+1
source

All Articles