forked from uku/Unblock-Youku
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.js
275 lines (236 loc) · 9.27 KB
/
config.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* Copyright (C) 2012 - 2014 Bo Zhu http://zhuzhu.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*jslint browser: true */
/*global chrome: false, get_storage: false, set_storage: false, new_random_ip: false */
/*global setup_redirect: false, setup_header: false, setup_proxy: false, setup_timezone: false, setup_extra_header: false */
/*global clear_redirect: false, clear_header: false, clear_proxy: false, clear_timezone: false */
/*global ga_report_event: false, ga_report_ratio: false, ga_report_error: false */
"use strict";
// ====== Constant and Variable Settings ======
var unblock_youku = unblock_youku || {}; // namespace
// only for redirect mode
unblock_youku.default_server = 'www.yōukù.com/proxy';
// unblock_youku.default_server = '127.0.0.1:8888/proxy';
unblock_youku.backup_server = 'bak.yōukù.com/proxy';
unblock_youku.normal_url_list = unblock_youku.common_urls.concat(unblock_youku.chrome_extra_urls);
unblock_youku.redirect_url_list = unblock_youku.common_urls;
unblock_youku.header_extra_url_list = [
// 'http://web-play.pptv.com/*',
// 'http://web-play.pplive.cn/*',
'http://v.api.hunantv.com/*',
'http://live.gslb.letv.com/*',
'http://ac.qq.com/*',
'http://*.ssports.com/*',
'http://ssports.com/*',
'http://ssports.smgbb.cn/*',
'http://www.bilibili.com/*',
'http://interface.bilibili.com/*',
'http://*.xiami.com/*', // xiami is blocked in HK and TW
'http://*.ku6.com/*',
'http://kandian.com/player/getEpgInfo*' // !!!
];
unblock_youku.ip_addr = new_random_ip();
console.log('ip addr: ' + unblock_youku.ip_addr);
// ====== Configuration Functions ======
function set_mode_name(mode_name, callback) {
if (typeof callback === 'undefined') {
var err_msg = 'missing callback function in set_mode_name()';
console.error(err_msg);
ga_report_error('Unexpected Error', err_msg);
}
if (mode_name === 'lite' || mode_name === 'redirect') {
set_storage('unblock_youku_mode', mode_name, callback);
} else {
set_storage('unblock_youku_mode', 'normal', callback);
}
}
function get_mode_name(callback) {
if (typeof callback === 'undefined') {
var err_msg = 'missing callback function in get_mode_name()';
console.error(err_msg);
ga_report_error('Unexpected Error', err_msg);
}
get_storage('unblock_youku_mode', function(current_mode) {
if (typeof current_mode === 'undefined' || (
current_mode !== 'lite' &&
current_mode !== 'normal' &&
current_mode !== 'redirect')) {
set_mode_name('normal', function() {
callback('normal');
});
} else {
callback(current_mode);
}
});
}
function clear_mode_settings(mode_name) {
switch (mode_name) {
case 'lite':
// clear_timezone();
clear_header();
console.log('cleared settings for lite');
break;
case 'redirect':
clear_redirect();
console.log('cleared settings for redirect');
break;
case 'normal':
// clear_timezone();
clear_proxy();
clear_header();
console.log('cleared settings for normal');
break;
default:
var err_msg = 'clear_mode_settings: should never come here';
console.error(err_msg);
ga_report_error('Unexpected Error', err_msg);
break;
}
console.log('cleared the settings for the mode: ' + mode_name);
}
function setup_mode_settings(mode_name) {
switch (mode_name) {
case 'lite':
setup_header();
// setup_timezone();
break;
case 'redirect':
setup_redirect();
break;
case 'normal':
setup_header();
setup_proxy();
// setup_timezone();
break;
default:
var err_msg = 'setup_mode_settings: should never come here';
console.error(err_msg);
ga_report_error('Unexpected Error', err_msg);
break;
}
console.log('initialized the settings for the mode: ' + mode_name);
}
function change_mode(new_mode_name) {
set_mode_name(new_mode_name, function() {});
// the storage change listener would take care about the setting changes
}
function _change_browser_icon(option) {
var today = new Date();
var y = today.getFullYear();
var d = today.getDate();
var m = today.getMonth() + 1;
// hard-coded spring festivals
var is_spring = false;
switch (y) {
case 2014: // Jan 31, 2014
if ((m === 1 && 20 <= d) || (m === 2 && d <= 10)) {
is_spring = true;
}
break;
case 2015: // Feb 19, 2015
if (m === 2 && (9 <= d && d <= 29)) {
is_spring = true;
}
break;
}
if (is_spring) {
chrome.browserAction.setIcon({path: 'chrome/icons/icon19spring.png'});
chrome.browserAction.setTitle({title: 'Happy Spring Festival! (Unblock Youku ' + unblock_youku.version + ')'});
return;
}
// christmas
if (m === 12 && d >= 15) {
chrome.browserAction.setIcon({path: 'chrome/icons/icon19xmas.png'});
chrome.browserAction.setTitle({title: 'Merry Christmas! (Unblock Youku ' + unblock_youku.version + ')'});
return;
}
if (option === 'heart') {
chrome.browserAction.setIcon({path: 'chrome/icons/icon19heart.png'});
chrome.browserAction.setTitle({title: 'Thank you! (Unblock Youku ' + unblock_youku.version + ')'});
} else {
chrome.browserAction.setIcon({path: 'chrome/icons/icon19.png'});
chrome.browserAction.setTitle({title: 'Unblock Youku ' + unblock_youku.version});
}
}
function change_browser_icon(option) {
// check chrome.storage before changing icons
// the mode should already be set in previous get_mode_name()
get_storage('unblock_youku_mode', function(current_mode) {
if (typeof current_mode !== 'undefined') {
_change_browser_icon(option);
} else {
var err_msg = 'chrome.storage has some problems';
console.log(err_msg);
ga_report_error('Unexpected Error', err_msg);
}
});
}
// in case settings are changed (or synced) in background
function storage_monitor(changes, area) {
console.log('storage changes: ' + JSON.stringify(changes));
if (typeof changes.unblock_youku_mode !== 'undefined') {
var mode_change = changes.unblock_youku_mode;
// doesn't run if it's first time to migrate the old settings
if (typeof mode_change.oldValue !== 'undefined' && typeof mode_change.newValue !== 'undefined') {
clear_mode_settings(mode_change.oldValue);
setup_mode_settings(mode_change.newValue);
ga_report_event('Change Mode', mode_change.oldValue + ' -> ' + mode_change.newValue);
}
}
if (typeof changes.custom_server !== 'undefined') {
var server_change = changes.custom_server;
if (typeof server_change.newValue !== 'undefined') {
// have to use a localStorage cache for using in the blocking webRequest listener
localStorage.custom_server = server_change.newValue;
} else {
if (typeof localStorage.custom_server !== 'undefined') {
localStorage.removeItem('custom_server');
}
}
}
}
function setup_storage_monitor() {
if (!chrome.storage.onChanged.hasListener(storage_monitor)) {
chrome.storage.onChanged.addListener(storage_monitor);
console.log('storage_monitor is set');
} else {
var err_msg = 'storage_monitor is already there!';
console.error(err_msg);
ga_report_error('Unexpected Error', err_msg);
}
}
// ====== Initialization ======
document.addEventListener("DOMContentLoaded", function() {
setup_storage_monitor();
unblock_youku.version = chrome.runtime.getManifest().version;
// the latest version to show NEW on the icon; it's usually a big update with new features
unblock_youku.lastest_new_version = '2.8.0.1';
get_storage('previous_new_version', function(version) {
// previous_new_version will be set by the popup page once the page is opened
if (typeof version === 'undefined' || version !== unblock_youku.lastest_new_version) {
chrome.browserAction.setBadgeText({text: 'NEW'});
}
});
get_mode_name(function(current_mode_name) {
setup_mode_settings(current_mode_name);
ga_report_ratio('Init Mode', current_mode_name);
ga_report_ratio('Version', unblock_youku.version);
change_browser_icon('regular'); // set the icon once everything is done
});
setup_extra_header();
// setup_extra_redirector();
});