Skip to content

Instantly share code, notes, and snippets.

@Lukewh
Created November 27, 2012 14:07
Show Gist options
  • Save Lukewh/4154391 to your computer and use it in GitHub Desktop.
Save Lukewh/4154391 to your computer and use it in GitHub Desktop.
chrome.tabs open function
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);
});
};
getTabs('google.com/search', function(tabs) {
console.log(tabs);
};
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'});
}
};
{
"permissions": [
"tabs"
]
}
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