forked from panicinc/CodaNotes.safariextension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
global.html
204 lines (181 loc) · 6.49 KB
/
global.html
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>global</title>
<script src="jquery-1.8.2.min.js" type="text/javascript"></script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
//console.log('global.html ready begin');
safari.application.addEventListener("command", performCommand, false);
safari.application.addEventListener("validate", validateCommand, false);
safari.application.addEventListener("message", handleMessage, false);
safari.extension.settings.addEventListener("change", settingsChanged, false);
var enabledTabs = new Array();
function copySettings () {
var settings = new Array();
settings['pen_color'] = safari.extension.settings['pen_color'];
settings['highlighter_color'] = safari.extension.settings['highlighter_color'];
settings['from_email'] = safari.extension.settings['from_email'];
settings['coda_notes_serial_number'] = safari.extension.settings['coda_notes_serial_number'];
return settings;
}
function settingsChanged (event) {
for (var windowIndex = 0; windowIndex < safari.application.browserWindows.length; windowIndex++){
for (var tabIndex = 0; tabIndex < safari.application.browserWindows[windowIndex].tabs.length; tabIndex++){
safari.application.browserWindows[windowIndex].tabs[tabIndex].page.dispatchMessage("settingsUpdated", copySettings());
}
}
}
function performCommand(event) {
/* console.log('performing: ' + event.command); */
if (event.command === 'enableBar') {
var index;
safari.application.activeBrowserWindow.activeTab.page.dispatchMessage("enableBar");
}
}
function validateCommand(event) {
/* console.log('validating: ' + event.command); */
var button = getActiveButton();
button.image = safari.extension.baseURI + 'images/btnCodaNotes.png';
event.target.disabled = true;
var bar = getBarForTab(event.target);
for (var windowIndex = 0; windowIndex < safari.application.browserWindows.length; windowIndex++){
var tab = safari.application.browserWindows[windowIndex].activeTab;
if (tab.url && tab.page) {
tab.page.dispatchMessage("documentReady");
tab.page.dispatchMessage("enabled");
}else{
var found = false;
for (var i = 0; i < enabledTabs.length; i++) {
if (enabledTabs[i] === event.target){
found = true;
break;
}
}
if ( ! found){
var bar = getBarForTab(tab);
if(bar.browserWindow == safari.application.browserWindows[windowIndex]){
bar.hide();
}
}
}
}
}
function handleMessage(event) {
var bar = getBarForTab(event.target);
//console.log('global.html handleMessage: ' + event.name);
// console.log(event.message);
if (event.name == 'barEnabled') {
var found = false;
for (var i = 0; i < enabledTabs.length; i++) {
if (enabledTabs[i] === event.target){
found = true;
break;
}
}
if ( ! found){
enabledTabs.push(event.target);
}
if ( ! bar.visible) {
bar.show();
}
}
else if (event.name == 'tabUnloading') {
for (var i = 0; i < enabledTabs.length; i++) {
if (enabledTabs[i] === event.target){
enabledTabs.splice(i, 1);
break;
}
}
if (event.target === event.target.browserWindow.activeTab){
var button = getWindowButton(event.target.browserWindow);
button.disabled = true;
if (bar.visible) {
bar.hide();
}
}
} else if (event.name == 'barDisabled') {
for (var i = 0; i < enabledTabs.length; i++) {
if (enabledTabs[i] === event.target){
enabledTabs.splice(i, 1);
break;
}
}
if (event.target === event.target.browserWindow.activeTab){
var button = getWindowButton(event.target.browserWindow);
button.image = safari.extension.baseURI + 'images/btnCodaNotes.png';
if (bar.visible) {
bar.hide();
}
}
}else if (event.name == 'showEditing') {
if (event.target === event.target.browserWindow.activeTab){
var button = getWindowButton(event.target.browserWindow);
button.image = safari.extension.baseURI + 'images/btnCodaNotes-unsaved.png';
}
}else if (event.name == 'documentReady') {
event.target.page.dispatchMessage('settingsUpdated', copySettings()/*, safari.extension.settings*/);
event.target.page.dispatchMessage("modeRequest");
if (safari.application.activeBrowserWindow.activeTab != event.target) {
return;
}
var button = getActiveButton();
if (event.message) {
button.disabled = false;
}else {
button.disabled = true;
}
}else if (event.name == 'setSetting') {
safari.extension.settings.setItem(event.message['name'], event.message['value']);
}
}
function getActiveButton(){
const buttons = safari.extension.toolbarItems;
for (var i = 0; i < buttons.length; i++) {
if (buttons[i].browserWindow == safari.application.activeBrowserWindow) {
return buttons[i];
}
}
}
function getWindowButton(browserWindow){
const buttons = safari.extension.toolbarItems;
for (var i = 0; i < buttons.length; i++) {
if (buttons[i].browserWindow === browserWindow) {
return buttons[i];
}
}
}
function getBarForTab(tab){
const bars = safari.extension.bars;
for (var i = 0; i < bars.length; ++i) {
var bar = bars[i];
if (bar.browserWindow === tab.browserWindow && bar.identifier === "codaNotes"){
return bar;
}
}
}
function getActiveBar(){
const bars = safari.extension.bars;
const activeBrowserWindow = safari.application.activeBrowserWindow;
for (var i = 0; i < bars.length; ++i) {
var bar = bars[i];
if (bar.browserWindow === activeBrowserWindow && bar.identifier === "codaNotes"){
return bar;
}
}
}
// Reload all tabs
for (var windowIndex = 0; windowIndex < safari.application.browserWindows.length; windowIndex++){
for (var tabIndex = 0; tabIndex < safari.application.browserWindows[windowIndex].tabs.length; tabIndex++){
var tab = safari.application.browserWindows[windowIndex].tabs[tabIndex];
tab.url = tab.url;
}
}
});
</script>
</head>
<body>
</body>
</html>