Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update handler for tabs that are opened without url.
  • Loading branch information
Tonkpils committed Jul 12, 2013
1 parent ec554ae commit 3008626
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions assets/javascript/TabZolo.js
Expand Up @@ -24,8 +24,9 @@ chrome.tabs.onCreated.addListener(function(newTab) {
});
}
});
}
);
});

chrome.tabs.onUpdated.addListener(updateHandler);

chrome.storage.onChanged.addListener(function(changes, namespace) {
if('enabled' in changes)
Expand Down Expand Up @@ -101,25 +102,14 @@ function checkTabs(tabs, newTab){
var newURL = newTab.url;
// if an attempt to open a new tab was made close the new tab
// otherwise change the current url / allow first tab to load.
if(tabs.length > 1){
// to compensate for funky cases where tab urls are blank and get filled in before
// the api responds with it
// simply closes all but the currently open tab
if (newTab.url == ''){
chrome.tabs.query({}, function(allTabs){
var ids = [];
allTabs.map(function(tab){
if(tab.id != newTab.id){
ids.push(tab.id);
}
});
chrome.tabs.remove(ids);
});
} else {
if(tabs.length > 1) {
if (newTab.url != ''){
chrome.tabs.remove(newTab.id, function(){
setURL(newURL);
});
}
} else {
chrome.tabs.onUpdate.addListener(updateHandler);
}
} else {
setURL(newURL);
}
Expand Down Expand Up @@ -156,3 +146,26 @@ function setIcon(active){
chrome.browserAction.setTitle({title:'TabZolo (Disabled)'});
}
}


function updateHandler(tabId, changeInfo, tab){
if (changeInfo.url) {
chrome.storage.local.get('enabled', function(settings){
if (settings.enabled == true) {
chrome.tabs.query({}, function(tabs){
var url = changeInfo.url;
var originalTab = tabs.filter(function(t){
return t.active == false;
});

if (originalTab[0].id != tabId){
chrome.tabs.remove(tabId, function(){
chrome.tabs.update(originalTab[0].id, {url: url});
});
}
});
}
});
}
chrome.tabs.onUpdate.removeListener();
}

1 comment on commit 3008626

@dasmall
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incredibly baller, son.

Please sign in to comment.