Google Chrome Plugin: How to get domain from URL (tab.url)

Viewed 20243

Using the Google Chrome API's tab.url value, what is the best method to get just the domain from the entire value?

In JavaScript I would use window.location.protocol & window.location.hostname. For example something like this:

var domain = window.location.protocol + "//" + window.location.hostname;

But that gets the extension domain and not the tab so cannot use that method. So with a function similar to the one below... How would I strip just the domain from the tab.url value?

function show_alert() {
    chrome.tabs.getSelected(null, function(tab) {
        var currentURL = tab.url;
        alert(currentURL);
    });
}
3 Answers
Related