-
Notifications
You must be signed in to change notification settings - Fork 2
/
sidebar.js
102 lines (93 loc) · 3.53 KB
/
sidebar.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
if (window.top === window) {
// The parent frame is the top-level frame, not an iframe.
var hideSidebar = function(sidebar) {
$(sidebar).remove();
};
var showSidebar = function(sidebar) {
$(sidebar).css('display', 'block');
};
var getSidebar = function() {
return document.getElementById('fi_sidebar');
};
var updateSidebar = function(sidebar, about) {
sidebar.src = 'http://' + lovemedoHost + '/infomaniac/' + encodeURIComponent(about);
};
var toggleSidebar = function(settings, about) {
var sidebar = getSidebar();
if (sidebar) {
hideSidebar(sidebar);
}
else {
// There is no sidebar. Create one showing the Fluidinfo object for
// the current document url, and display it.
createSidebar(settings, function(sidebar) {
updateSidebar(sidebar, about);
showSidebar(sidebar);
});
}
};
var createSidebar = function(settings, callback) {
var parent = (document.getElementsByTagName('body')[0] ||
document.getElementsByTagName('html')[0]);
if (parent) {
var sidebar = document.createElement('iframe');
sidebar.id = 'fi_sidebar';
sidebar.classList.add('fluidinfo_sidebar');
sidebar.classList.add('fluidinfo_sidebar_' + settings.sidebarSide);
sidebar.setAttribute('width', settings.sidebarWidth + 'px');
sidebar.title = 'Fluidinfo sidebar';
parent.appendChild(sidebar);
callback(sidebar);
}
else {
console.log('Could not find body or html element on page!');
callback(null);
}
};
// Listen for instructions from notification pop-ups or from the background page.
var handleMessage = function(e) {
if (e.name !== 'background') {
return;
}
var sidebar, msg = e.message, settings = msg.settings;
if (msg.action === 'show sidebar') {
sidebar = getSidebar();
if (sidebar) {
updateSidebar(sidebar, valueUtils.lowercaseAboutValue(e.message.about));
}
else {
createSidebar(settings, function(sidebar) {
updateSidebar(sidebar, valueUtils.lowercaseAboutValue(e.message.about));
showSidebar(sidebar);
});
}
}
else if (msg.action === 'hide sidebar') {
sidebar = getSidebar();
if (sidebar) {
hideSidebar(sidebar);
}
}
else if (msg.action === 'toggle sidebar') {
toggleSidebar(settings, valueUtils.lowercaseAboutValue(e.message.about));
}
else {
console.log('got unknown msg from background:');
console.log(e.message);
}
};
safari.self.addEventListener('message', handleMessage, false);
if (navigator.platform.indexOf("Mac") != -1) {
// Allow toggling the display of the sidebar via Cmd-Shift-F
shortcut.add('Meta+Shift+F', function() {
console.log('Received Cmd+Shift+F');
safari.self.tab.dispatchMessage('content', {toggleSidebar: true});
});
} else {
// Allow toggling the display of the sidebar via Ctrl-Shift-F
shortcut.add('Ctrl+Shift+F', function() {
console.log('Received Ctrl+Shift+F');
safari.self.tab.dispatchMessage('content', {toggleSidebar: true});
});
}
}