简体   繁体   中英

Passing variable from a function to a function within a function

I'm trying to create a chrome extension that takes a selected text via context menu, appends it to a url and update an existing tab (from the same domain) with the new URL. This is my code:

function soso(search_target){
    alert(search_target);
    //search_target gets here successfully 

    chrome.tabs.query({
        url: "https://lightning.force.com/lightning/*"},
        function(tabs, search_target){

        if (tabs[0]){

            chrome.tabs.update(tabs[0].id, {
                url: "http://www.facebook.com"+search_target, //search_target doesnt get here
                active: true
            });
        }
        else{
            alert("no window");
        }
        });
}


function selectionHandler (info, tab)
{
        soso( info.selectionText);
}

function resetContextMenus ()
{
    chrome.contextMenus.removeAll(
    function()
    {
            var id = chrome.contextMenus.create( {
                title: "Open in Salesforce",
                contexts: [ "selection" ],
            onclick: selectionHandler
        } );
    }
    );
}
resetContextMenus();

The problem is that I cannot pass search_target to chrome.tabs.update , I get an undefined after the URL.

Any ideas on how to pass this variable?

Let's move backwards with our analysis:

  • you try to call chrome.tabs.update , passing search_target
  • inside a callback function which has a search_target parameter
  • the function is called by chrome.tabs.query
  • and not by your outer function
  • but you have a valid search_target in the outer context
  • which is unreachable from the block of the callback function because a parameter with similar name which is undefined shadows it

Solution: Remove the parameter to allow the variable of the outer context be known inside the callback function :

function soso(search_target){
    alert(search_target);
    //search_target gets here successfully 

    chrome.tabs.query({
        url: "https://lightning.force.com/lightning/*"},
        function(tabs){

        if (tabs[0]){

            chrome.tabs.update(tabs[0].id, {
                url: "http://www.facebook.com"+search_target, //search_target doesnt get here
                active: true
            });
        }
        else{
            alert("no window");
        }
        });
}


function selectionHandler (info, tab)
{
        soso( info.selectionText);
}

function resetContextMenus ()
{
    chrome.contextMenus.removeAll(
    function()
    {
            var id = chrome.contextMenus.create( {
                title: "Open in Salesforce",
                contexts: [ "selection" ],
            onclick: selectionHandler
        } );
    }
    );
}
resetContextMenus();

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