0

Below is a snippet of the Chrome Extension start page example.https://developer.chrome.com/extensions/getstarted

I'm wondering where is the body of callback() function which is invoked at the end.

/**                                                                                  
  6  * Get the current URL.                                                              
  7  *                                                                                   
  8  * @param {function(string)} callback - called when the URL of the current tab       
  9  *   is found.                                                                       
 10  */                                                                                  
 11 function getCurrentTabUrl(callback) {                                                
 12   // Query filter to be passed to chrome.tabs.query - see                            
 13   // https://developer.chrome.com/extensions/tabs#method-query                       
 14   var queryInfo = {                                                                  
 15     active: true,                                                                    
 16     currentWindow: true                                                              
 17   };                                                                                 
 18                                                                                      
 19   chrome.tabs.query(queryInfo, function(tabs) {                                      
 20     // chrome.tabs.query invokes the callback with a list of tabs that match the     
 21     // query. When the popup is opened, there is certainly a window and at least     
 22     // one tab, so we can safely assume that |tabs| is a non-empty array.            
 23     // A window can only have one active tab at a time, so the array consists of     
 24     // exactly one tab.                                                              
 25     var tab = tabs[0];                                                               
 26                                                                                      
 27     // A tab is a plain object that provides information about the tab.              
 28     // See https://developer.chrome.com/extensions/tabs#type-Tab                     
 29     var url = tab.url;                                                               
 30                                                                                      
 31     // tab.url is only available if the "activeTab" permission is declared.          
 32     // If you want to see the URL of other tabs (e.g. after removing active:true     
 33     // from |queryInfo|), then the "tabs" permission is required to see their        
 34     // "url" properties.                                                             
 35     console.assert(typeof url == 'string', 'tab.url should be a string');            
 36                                                                                      
 37     callback(url);                                                                   
 38   });                                                                                
 39    
  • The callback function is provided as parameter of the `getCurrentTabUrl` function. So it can be anything. – Erazihel Jun 22 '17 at 08:43

2 Answers2

0

The variable is defined in the arguments here:

function getCurrentTabUrl(callback) {

The function isn't defined in the code you quoted. It is defined elsewhere and then passed when getCurrentTabUrl is called.

e.g.

function foo() { /* do something */ }
getCurrentTabUrl(foo);
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

its will be like :

getCurrentTabUrl(function(url){
alert(url);
});

inshort where the getCurrentTabUrl is being called !

Beep.exe
  • 1,368
  • 12
  • 21