【问题标题】:where is the definition of callback()? [duplicate]callback() 的定义在哪里? [复制]
【发布时间】:2017-11-25 10:30:22
【问题描述】:

下面是 Chrome 扩展起始页示例的 sn-p。https://developer.chrome.com/extensions/getstarted

我想知道最后调用的 callback() 函数的主体在哪里。

/**                                                                                  
  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    

【问题讨论】:

  • 回调函数作为getCurrentTabUrl函数的参数提供。所以它可以是任何东西。

标签: javascript google-chrome-extension callback


【解决方案1】:

它会像:

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

简而言之,调用 getCurrentTabUrl 的位置!

【讨论】:

    【解决方案2】:

    变量在这里的参数中定义:

    function getCurrentTabUrl(callback) {
    

    您引用的代码中未定义该函数。它在别处定义,然后在调用getCurrentTabUrl 时传递。

    例如

    function foo() { /* do something */ }
    getCurrentTabUrl(foo);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-15
      • 1970-01-01
      • 2016-09-13
      • 2012-10-13
      • 1970-01-01
      • 2014-12-30
      • 2013-04-09
      • 2015-04-05
      相关资源
      最近更新 更多