Skip to content

Commit bb0cba0

Browse files
author
Jose Falcon
committed
Add observer for tab title
1 parent 5fbacc7 commit bb0cba0

File tree

5 files changed

+35
-6
lines changed

5 files changed

+35
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules/
22
src/
3+
npm-debug.log

javascript/events.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,8 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
1212
chrome.tabs.onRemoved.addListener(tabId => {
1313
dispatch({type: REMOVE_TAB, tabId: tabId});
1414
})
15+
16+
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
17+
console.log(message, sender);
18+
if (message && message.title) dispatch({type: UPDATE_TAB, tab: sender.tab});
19+
});

javascript/store.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,25 @@ function match(url) {
2929
}
3030

3131
function updateTab(tab) {
32-
let key = match(tab.url);
33-
if (key) {
32+
let handler = match(tab.url);
33+
if (handler) {
3434
get()
3535
.then(state => {
3636
if (!state.tabs) {
3737
state.tabs = {};
3838
}
3939

40-
tab.handler = key;
41-
state.tabs[tab.id] = tab;
40+
// start tracking the tab. and add a title observer.
41+
if (!state.tabs.hasOwnProperty(tab.id)) {
42+
chrome.tabs.executeScript(tab.id, {file: 'src/title-observer.js'});
43+
}
44+
45+
state.tabs[tab.id] = {
46+
id: tab.id,
47+
uri: tab.uri,
48+
title: tab.title,
49+
handler: handler
50+
}
4251
return save(state);
4352
});
4453
} else {

javascript/title-observer.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function notify(title) {
2+
try {
3+
chrome.runtime.sendMessage({'title': title});
4+
} catch(e) {
5+
observer.disconnect();
6+
}
7+
}
8+
9+
let observer = new window.MutationObserver(mutations => {
10+
mutations.forEach(mutation => notify(mutation.target.textContent));
11+
});
12+
13+
let target = document.querySelector('head > title');
14+
observer.observe(target, { subtree: true, characterData: true, childList: true });

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"main": "index.js",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1",
8-
"js": "browserify javascript/app.js -t babelify --outfile src/app.js && browserify javascript/events.js -t babelify --outfile src/events.js",
9-
"clean": "rm src/app.js src/events.js"
8+
"js": "browserify javascript/app.js -t babelify --outfile src/app.js && browserify javascript/events.js -t babelify --outfile src/events.js && browserify javascript/title-observer.js -t babelify --outfile src/title-observer.js",
9+
"clean": "rm -rf src/"
1010
},
1111
"author": "",
1212
"license": "ISC",

0 commit comments

Comments
 (0)