-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
56 lines (51 loc) · 1.87 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
var titles = {};
/**
* Save tab title and send message to content script to update the document title.
* @param {String} title the new page title
* @param {int} tabId the ID of the current tab
*/
function updateTitle(title, tabId) {
titles[tabId] = title;
chrome.tabs.sendMessage(tabId, title);
}
/**
* onMessage listener callback. Fires when a message is received from another script
* @param {Object} msg contains the tab ID and new title
* @param {int} msg.tabId the ID of the tab to change the title of
* @param {string} msg.title the new tab title
*/
function onMessageListener(msg) {
updateTitle(msg.title, msg.tabId);
}
/**
* onUpdated listener callback. Fires when a tab is updated.
* Note: a 2nd parameter can be passed to give the change info which may include the status (either "loading" or "complete")
* Note: a 3rd parameter can be passed to give the updated tab object
* @param {int} tabId the ID of the updated tab
*/
function onUpdatedListener(tabId) {
// if a tab that we already have a new title for has been updated, update its title again.
if (tabId in titles) updateTitle(titles[tabId], tabId);
}
/**
* onOmniboxInputChanged listener callback.
* Fires when a user types in the omnibox after entering the specifies keyword in the manifest and pressing the Tab key.
* Note: A second parameter can be passed to give a suggestion callback
* @param {String} title the text in the omnibox
*/
function onOmniboxInputChanged(title) {
// Get current tab object
var queryInfo = {
active: true,
currentWindow: true
};
chrome.tabs.query(queryInfo, function(tabs) {
var tab = tabs[0];
// update the title.
updateTitle(title, tab.id);
});
}
// Add the listeners
chrome.omnibox.onInputChanged.addListener(onOmniboxInputChanged);
chrome.runtime.onMessage.addListener(onMessageListener);
chrome.tabs.onUpdated.addListener(onUpdatedListener);