-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsidebar.js
126 lines (115 loc) · 3.96 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
var settings = null;
var backgroundPort = chrome.extension.connect({
name: 'content-script'
});
var hideSidebar = function(sidebar){
var options = {};
options[settings.sidebarSide] = -1 * settings.sidebarWidth;
$(sidebar).animate(options, 500, function(){
$(sidebar).css('display', 'none');
});
};
var showSidebar = function(sidebar){
var options = {};
options[settings.sidebarSide] = 0;
$(sidebar).css(settings.sidebarSide, '-' + settings.sidebarWidth + 'px')
.css('display', 'block')
.animate(options, 500);
};
var getSidebar = function(){
return document.getElementById('lovemedo-sidebar-id');
};
var updateSidebar = function(sidebar, about){
sidebar.src = 'http://' + lovemedoHost + '/infomaniac/' + encodeURIComponent(about);
sidebar.onload = function(){
backgroundPort.postMessage({injectSidebarJS: true});
};
};
var toggleSidebar = function(about){
var sidebar = getSidebar();
if (sidebar){
if (sidebar.style.display === 'none'){
updateSidebar(sidebar, about);
showSidebar(sidebar);
}
else {
hideSidebar(sidebar);
}
}
else {
// There is no sidebar. Create one showing the Fluidinfo object for
// the current document url, and display it.
createSidebar(function(sidebar){
updateSidebar(sidebar, about);
showSidebar(sidebar);
});
}
};
var createSidebar = function(callback){
var parent = (document.getElementsByTagName('body')[0] ||
document.getElementsByTagName('html')[0]);
if (parent){
// Get the current settings so we know what style & size to use.
chrome.extension.sendRequest(
{action: 'get-settings'},
function(result){
settings = result;
var sidebar = document.createElement('iframe');
sidebar.id = 'lovemedo-sidebar-id';
sidebar.classList.add('lovemedo-sidebar');
sidebar.classList.add('lovemedo-sidebar-' + settings.sidebarSide);
sidebar.setAttribute('width', settings.sidebarWidth + 'px');
sidebar.title = 'loveme.do 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.
chrome.extension.onConnect.addListener(function(port){
console.assert(port.name === 'sidebar');
port.onMessage.addListener(function(msg) {
var sidebar;
if (msg.action === 'show sidebar'){
sidebar = getSidebar();
if (sidebar){
updateSidebar(sidebar, valueUtils.lowercaseAboutValue(msg.about));
showSidebar(sidebar);
}
else {
createSidebar(function(sidebar){
updateSidebar(sidebar, valueUtils.lowercaseAboutValue(msg.about));
showSidebar(sidebar);
});
}
}
else if (msg.action === 'hide sidebar'){
sidebar = getSidebar();
if (sidebar){
hideSidebar(sidebar);
}
}
else if (msg.action === 'toggle sidebar'){
toggleSidebar(valueUtils.lowercaseAboutValue(msg.about));
}
else {
console.log('got unknown mesg from notification popup:');
console.log(msg);
}
});
});
// Allow toggling the display of the sidebar via Control-Shift-f
shortcut.add('Ctrl+Shift+F', function(){
console.log('Received Ctrl+Shift+F');
toggleSidebar(document.location.toString());
});
// Allow toggling the display of the sidebar via Control-Shift-f
shortcut.add('Ctrl+Shift+Z', function(){
console.log('Received Ctrl+Shift+Z');
toggleSidebar(document.location.toString());
});