forked from ktmud/owa-chrome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
141 lines (119 loc) · 4.15 KB
/
background.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
var sessionInfo = {
frequency: localStorage.frequency * 60000 || 300000,
isStarted: false,
isOnline: false,
isKissed: false,
isMailing: false,
isLoading: false,
isStartUp: true //启动时不显示新邮件
};
var HOME_URL = URL = localStorage.owaHome;
var initialize = function() {
//默认配置
localStorage.doNotify = true; // 是否显示桌面通知
localStorage.frequency = 10; // 检查新邮件周期,单位为分钟
localStorage.notifyTime = 6; // 新邮件桌面通知显示时间,单位为秒
localStorage.previewNum = 10; // popup窗口显示的预览条数
localStorage.popLogin = true; // 默认在未登录时弹出登录窗口
localStorage.doNotifyLogin = true; // 未登录时弹出提醒
localStorage.doNum = false; //是否始终在预览窗口显示邮件序号
localStorage.username = '';
localStorage.password = '';
localStorage.owaHome = '';
//灰色
BROWSER_ACTION.setBadgeBackgroundColor({color: [139, 139, 139, 255]});
BROWSER_ACTION.setBadgeText({text: UNKNOWN_COUNT});
};
$.ajaxSetup({
beforeSend: function() {
if (sessionInfo.isLoading) {
return;
}
sessionInfo.isLoading = true;
},
complete: function() {
sessionInfo.isLoading = false;
}
});
//图标的点击事件
BROWSER_ACTION.onClicked.addListener(function(tab) {
if (!HOME_URL) {
OCC.notify('notify-setowa.html');
return;
}
if (sessionInfo.isLoading) {
return;
}
if (sessionInfo.isOnline) {
OCC.openMail();
} else {
var usr = localStorage.username; pwd = localStorage.password;
if (usr && pwd) {
occLoader.login(usr, pwd, true, true);
} else {
if (parse(localStorage.doNotifyLogin)) OCC.notify('notify-login.html');
OCC.openMail(true);
}
}
});
//bind things to chrome
var kissBrowser = function() {
//实现同步更新图标
TABS.onUpdated.addListener(function(tabId, info, tab) {
var url = tab.url;
if (info.status != 'complete' || url.indexOf(HOME_URL) < 0) return;
if (url.indexOf('logoff') >= 0) { //如果是在登出页,把图标设为离线状态并打开owa主页
OCC.openMail(true);
OCC.setBA('offline');
return;
}
//在页面中加入content script
TABS.executeScript(null, {file: 'injection.js'}, function() {
//给content script发送请求,返回页面内容,并执行sync操作
TABS.sendRequest(tabId, { theAct: 'tabUpdated' }, function(data) {
if (data.needReSync) {
if (sessionInfo.isOnline == false) OCC.setBA('online');
occLoader.sync(false, data.res);
}
});
});
//if (url.indexOf('a=New') >= 0 || url.indexOf('a=Reply') >= 0) {
////插入kissy 编辑器
//TABS.executeScript(null, {file: 'load-editor.js'}, function() {
//});
//}
});
//切换到owa窗口时停止自动检查新邮件
TABS.onSelectionChanged.addListener(function(tabId) {
TABS.get(tabId, function(tab) {
if (tab.url.indexOf(HOME_URL) >= 0) {
tclear(loT);
sessionInfo.isMailing = true;
}else if (sessionInfo.isMailing) {
sessionInfo.isMailing = false;
loT = setTimeout(occLoader.sync, localStorage.frequency * 60000);
}
});
});
sessionInfo.kissed = true;
};
function start() {
//只有配置了owa路径才会启动
if (!HOME_URL) {
OCC.notify('notify-setowa.html');
return;
}
if (HOME_URL[HOME_URL.length - 1] !== '/') HOME_URL += '/';
localStorage.owaHome = URL = HOME_URL;
var notiLogin = parse(localStorage.doNotifyLogin);
occLoader.auth(notiLogin);
//绑定浏览器事件
if (!sessionInfo.isKissed) kissBrowser();
sessionInfo.isStarted = true;
}
//插件安装后初始化
if (!parse(localStorage.initialized)) {
initialize();
localStorage.initialized = true;
}
start();