简体   繁体   中英

How to add current tab url to chrome.tabs.create url

I have added a conext menu in Chrome that when clicked opens a Wufoo form using chrome.tabs.create. One of the fields I need the user to complete in the wufoo form is the URL of the current tab. So I can simply ad my var a to the url and it should be added. This works if var a is just some text but when I try to add the current tab url "undefined" is added to the url. What is my mistake? How do I get the tab url to be added?

Here is the javascript

chrome.contextMenus.onClicked.addListener(function(info, tabs){  
var a = chrome.tabs.query({'active': true, 'windowId':chrome.windows.WINDOW_ID_CURRENT},
function(tabs){
  return(tabs[0].url);
 }
 );
    if ( info.menuItemId === 'Add a Link' ) 
            chrome.tabs.create( {url:  "https://ownthistown.wufoo.com/forms/m3u64zc05w2a0a/def/field10=" + a  });

You cannot use return variable from an inner function like that. You will have to do something like this

chrome.contextMenus.onClicked.addListener(function(info, tabs){
  chrome.tabs.query({'active': true}, function (tabs) {
      if(info.menuItemId === 'Add a Link')
        chrome.tabs.create({url:  "https://ownthistown.wufoo.com/forms/m3u64zc05w2a0a/def/field10=" + tabs[0].url });
  });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM