Created
November 27, 2012 14:07
-
-
Save Lukewh/4154391 to your computer and use it in GitHub Desktop.
chrome.tabs open function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var getTabs = function(urlMatch, callback) { | |
var tabs = []; | |
chrome.tabs.query({ | |
status: 'complete' | |
}, function(results) { | |
for (var i = results.length; i--;) { | |
if (results[i].url.indexOf(urlMatch) !== -1) { | |
tabs.push(results[i].id); | |
} | |
} | |
callback(tabs); | |
}); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getTabs('google.com/search', function(tabs) { | |
console.log(tabs); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var injectInTab = function(tabIds) { | |
if (!Array.isArray(tabIds)) { | |
tabIds = [tabIds]; | |
} | |
for (var i = tabIds.length; i--;) { | |
chrome.tabs.executeScript(tabIds[i], {file: '/script.js'}); | |
chrome.tabs.insertCSS(tabIds[i], {file: '/style.css'}); | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"permissions": [ | |
"tabs" | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var addTabListeners = function(urlMatch) { | |
chrome.tabs.onCreated.addListener(function(result) { | |
if (result.url.indexOf(urlMatch) !== -1) { | |
console.log(result.id); | |
} | |
}); | |
chrome.tabs.onUpdated.addListener(function(result) { | |
if (result.url.indexOf(urlMatch) !== -1) { | |
console.log(result.id); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment