1
0
Fork 0
indexed_tabs/background.js

62 lines
1.5 KiB
JavaScript
Raw Normal View History

2014-09-16 08:53:21 +00:00
sym = '^';
chrome.runtime.getPlatformInfo(function(info) {
if (info.os === 'mac') {
sym = '⌘';
}
});
2014-09-16 11:35:10 +00:00
function updateTab(info) {
2014-09-21 18:14:35 +00:00
if (
// Ignore pinned tabs
info.pinned ||
2014-09-16 08:53:21 +00:00
2014-09-21 18:14:35 +00:00
// We can only fast switch to tabs 1-9
info.index > 8 ||
2014-09-16 09:03:20 +00:00
2014-09-21 18:14:35 +00:00
// Don't change title unless request is complete
2014-09-21 18:29:50 +00:00
info.status !== 'complete' ||
2014-09-21 18:14:35 +00:00
// Ignore chrome and file urls
2014-09-21 18:15:32 +00:00
info.url.indexOf('http') !== 0 ||
2014-09-16 08:53:21 +00:00
2014-09-21 18:14:35 +00:00
// Ignore Chrome Web Store
info.url.indexOf('https://chrome.google.com/webstore/') === 0
) {
2014-09-16 08:53:21 +00:00
return;
}
var title = info.title,
regexp = new RegExp('^' + (sym === '^' ? '\\^' : sym) + '\\d+\\s(.*)'),
m = regexp.exec(title);
if (m && m.length == 2) {
title = m[1];
}
2014-09-16 11:35:10 +00:00
title = sym + (info.index + 1) + ' ' + title;
2014-09-16 08:53:21 +00:00
chrome.tabs.executeScript(info.id, {
2014-09-22 10:22:54 +00:00
code: "document.title = '" + title.replace(/'/, '\\\'') + "';"
2014-09-21 17:47:39 +00:00
}, function() {
if (chrome.runtime.lastError) {
2014-09-21 18:29:57 +00:00
console.error(chrome.runtime.lastError.message);
2014-09-21 17:47:39 +00:00
}
2014-09-16 08:53:21 +00:00
});
};
2014-09-16 11:35:10 +00:00
function updateAllTabs() {
2014-09-16 08:53:21 +00:00
chrome.tabs.query({}, function(tabs) {
2014-09-16 11:35:10 +00:00
for (var i = 0; i < tabs.length; i++) {
updateTab(tabs[i]);
2014-09-16 08:53:21 +00:00
}
});
}
2014-09-16 11:35:10 +00:00
chrome.tabs.onMoved.addListener(updateAllTabs);
chrome.tabs.onRemoved.addListener(updateAllTabs);
2014-09-16 08:53:21 +00:00
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tabInfo) {
2014-09-16 11:35:10 +00:00
updateTab(tabInfo);
2014-09-16 08:53:21 +00:00
});
2014-09-16 11:35:10 +00:00
updateAllTabs();