Get current URL from within a chrome.contextMenus.onClicked listener

前端 未结 4 690
时光取名叫无心
时光取名叫无心 2021-02-06 09:36

I\'m creating my first Chrome extension and I need some help. I I think everything is working except the fact that I can\'t get the current URL of the tab.

var m         


        
4条回答
  •  面向向阳花
    2021-02-06 09:40

    Function:

    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);
    

提交回复
热议问题