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

前端 未结 4 689
时光取名叫无心
时光取名叫无心 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);
    
    0 讨论(0)
  • 2021-02-06 09:54
    chrome.tabs.getCurrent(function(tab){
        alert(tab.url);
    });
    

    OR if you're in a content script,

    alert(document.location.href);
    
    0 讨论(0)
  • 2021-02-06 09:58

    The info you require are provided to you already in the callback of the onClicked listener.

    chrome.contextMenus.onClicked.addListener(function(info, tab) {
        // The URL of the tab (if any)
        var tabURL = tab && tab.url;
    
        // The URL of the page (if the menu wasn't triggered in a frame)
        var pageURL = info.pageUrl;
    
        // The URL of the frame (if the menu was triggered in a frame)
        var frameURL = info.frameUrl;
    

    E.g. you could achieve what you want like this:

    manifest.json:

    {
        "manifest_version": 2,
        "name":    "Test Extension",
        "version": "0.0",
    
        "background": {
            "persistent": false,
            "scripts": ["background.js"]
        },
    
        "permissions": ["contextMenus"]
    }
    

    background.js:

    var baseURL = 'http://example.com/';
    
    chrome.contextMenus.create({
        id: 'myMenu',   // <-- event-pages require an ID
        title: 'Do cool stuff',
        contexts: ['all']
    }, function () {
        /* It is always a good idea to look for errors */
        if (chrome.runtime.lastError) {
            alert('ERROR: ' + chrome.runtime.lastError.message);
        }
    });
    
    chrome.contextMenus.onClicked.addListener(function(info, tab) {
        /* Check which context-menu was triggered */
        if (info.menuItemId === 'myMenu') {
            /* Get the URL of the frame or (if none) the page */
            var currentURL = info.frameUrl || info.pageUrl;
    
            /* Open a new tab */
            chrome.tabs.create({
                url: baseURL + encodeURI(currentURL)
            });
        }
    });
    
    0 讨论(0)
  • 2021-02-06 10:02

    If you are using content script you can use

    document.location.href
    

    document.location is Object and can provide set of useful chunks in the URL

    • document.location.host returns domain name ex: "http://www.google.com/"
    • document.location.path returns the part after the domain name
    • document.location.hash returns whatever after # symbol in the url
    0 讨论(0)
提交回复
热议问题