Skip to content

Instantly share code, notes, and snippets.

@termie
Last active August 29, 2015 14:10
Show Gist options
  • Save termie/385813aebec96da37f69 to your computer and use it in GitHub Desktop.
Save termie/385813aebec96da37f69 to your computer and use it in GitHub Desktop.
quick dream chrome extension: shorten tab names in busy windows
// Also probably need a background page with tab permissions: https://developer.chrome.com/extensions/tabs
// listening for onCreated, onUpdated and do a
chrome.tabs.onCreated.addListener(tabCreated);
function tabCreated(tab) {
chrome.tabs.query({"windowId": tab.windowId}, function (tabList) {
tablist.forEach(function (aTab) {
aTab.sendMessage({"numTabs": tabList.size()});
})
})
}
// And do similar for tabUpdated to get it to change the new title
// As a stretch goal you could make it revert the change for the active tab
// This is a content_script: https://developer.chrome.com/extensions/content_scripts
// probably need to do something like
chrome.runtime.onMessage.addListener(maybeShorten)
function maybeShorten(msg) {
if (msg.numTabs > 10) {
document.originalTitle = document.title;
document.title = shortenTitle(document.title);
} else {
document.title = document.originalTitle;
}
}
// Should replace things like:
// "Issues · termie/php-oauth-reference"
// with
// "I·t/p-o-r"
function shortenTitle(s) {
s1 = s.replace(/(\w)\w+/g, "$1");
s2 = s1.replace(/\s*([^\s\w]+)\s*/g, "$1"));
return s2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment