-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtitle.js
60 lines (56 loc) · 1.61 KB
/
title.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
57
58
59
60
var webviewTitleInjectionComplete = false;
(function() {
// Prevent multiple injection
if (!webviewTitleInjectionComplete) {
var embedder = null;
var tabName = null;
var listenersAreBound = false;
var title = null;
var postTitle = (function() {
return function(e) {
title = document.title;
var data = {
'name': tabName,
'title': title || '[no title]'
};
embedder.postMessage(JSON.stringify(data), '*');
};
}());
var bindEmbedder = function(e) {
embedder = e.source;
};
var bindTabName = function(e) {
if (e.data) {
var data = JSON.parse(e.data);
if (data.name) {
tabName = data.name;
} else {
console.warn('Warning: Message from embedder contains no tab name');
}
} else {
console.warn('Warning: Message from embedder contains no data');
}
};
// Wait for message that gives us a reference to the embedder
window.addEventListener('message', function(e) {
if (!listenersAreBound) {
// Bind data
bindEmbedder(e);
bindTabName(e);
// Notify the embedder of every title change
var titleElement = document.querySelector('title');
if (titleElement) {
titleElement.addEventListener('change', postTitle);
} else {
console.warn('Warning: No <title> element to bind to');
postTitle();
}
// Ensure initial title notification
if (title === null) {
postTitle();
}
listenersAreBound = true;
}
});
}
}());