Chrome, recognize open tab

94 views Asked by At

I'm creating an extenstion for google chrome that will perform checking if a stream on twitch.tv is online and will notify the user evey X minutes, I got that covered. What I'm looking for is a JScirpt code that will recognize if user is already on the streamers channel and will stop notifying him.

var username="$user";

setInterval(check,300000);

function check()
{
    request("https://api.twitch.tv/kraken/streams/" + username, function() {
        var json = JSON.parse(this.response);
        if (json.stream == null)
        {
            chrome.browserAction.setIcon({ path: "offline.png" });
        }
        else
        {
            notify();
        }
    });
    return 1;
}

function notify(){
    var opt = {type: "basic",title: username + " is streaming!",message: "Click to join!",iconUrl: "start.png"};
        chrome.notifications.create("", opt, function(notificationId) 
            { 
              setTimeout(function() 
                 { 
                   chrome.notifications.clear(notificationId, function(wasCleared) { console.log(wasCleared); }); 
                 }, 3000); 
            }); 
chrome.browserAction.setIcon({path:"online.png" });
}

chrome.browserAction.onClicked.addListener(function () {
    chrome.tabs.create({ url: "http://www.twitch.tv/"+username });
});

function request(url, func, post)
{
    var xhr = new XMLHttpRequest();
    xhr.onload = func;
    xhr.open(post == undefined ? 'GET' : 'POST', url, true);
    xhr.send(post || '');
    return 1;
}

check();
1

There are 1 answers

0
Speerian On
  • Use window.location.href to get the complete URL.
  • Use window.location.pathname to get URL leaving the host.

You can read more here.