-
Notifications
You must be signed in to change notification settings - Fork 13
/
common.js
165 lines (150 loc) · 5.2 KB
/
common.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
var _choosed_packages = null;
var update_choosed_packages = function(choosed_packages){
chrome.storage.local.set({choosed_packages: choosed_packages});
};
var get_choosed_packages = function(cb){
chrome.storage.local.get({choosed_packages: {}}, function(items){
get_package_info(function(package_info){
var choosed_packages = {};
var current_package = null;
for (var i = 0; i < package_info.packages.length; i ++) {
current_package = package_info.packages[i];
if ('undefined' === typeof(items.choosed_packages[current_package.id])) {
// 有指定 default=true 才會變成預設包
if (current_package && current_package['default']) {
choosed_packages[current_package.id] = true;
}
} else if (false === items.choosed_packages[current_package.id]) {
} else {
choosed_packages[current_package.id] = true;
}
}
cb(choosed_packages);
});
});
};
var get_package_info = function(cb){
chrome.storage.local.get({packages: {}}, function(items){
if ('undefined' === typeof(items.packages.fetch_at) || ((new Date()).getTime() - items.packages.fetch_at) > 86400 * 1000) {
update_packages(cb);
return;
}
cb(items.packages);
});
};
var update_packages = function(cb){
$.get('https://jobhelper.g0v.ronny.tw/api/getpackages', function(ret){
ret.fetch_at = (new Date()).getTime();
chrome.storage.local.set({packages: ret}, function(){
cb(ret);
});
}, 'json');
};
var _package_csv = null;
var get_package_csv_from_storage = function(cb){
if (null !== _package_csv) {
cb(_package_csv);
return;
}
chrome.storage.local.get({package_csv: {}}, function(items){
_package_csv = items.package_csv;
cb(_package_csv);
});
};
var get_package_info_by_id = function(package_info, id){
for (var i = 0; i < package_info.packages.length; i ++) {
if (package_info.packages[i].id == id) {
return package_info.packages[i];
}
}
return null;
};
var get_package_csv_by_id = function(id, cb){
get_package_info(function(package_info){
get_package_csv_from_storage(function(package_csv){
if ('undefined' !== typeof(package_csv[id]) && package_csv[id].package_time == get_package_info_by_id(package_info, id).package_time) {
cb(package_csv[id].content);
return;
}
$.get('https://jobhelper.g0v.ronny.tw/api/getpackage?id=' + parseInt(id), function(package_csv){
_package_csv[id] = package_csv;
chrome.storage.local.set({package_csv: _package_csv});
cb(_package_csv[id].content);
});
});
});
};
var check_name = function(web_name, db_name){
if ('string' !== typeof(db_name)) {
return false;
}
// 先比對一次
if (web_name.indexOf(db_name) >= 0) {
return true;
}
// 如果不是 "公司" 結尾的(Ex: 宏達國際電子股份有限公司桃園廠), 只判斷到公司
if (db_name.match('公司') && !db_name.match('公司$')) {
if (web_name.indexOf(db_name.match('(^.*公司)')[1]) >= 0) {
return true;
}
}
// 處理 "吳美玉(即玉皇手作茶飲店)" 格式
if (db_name.match('.*\\(即.*\\)')) {
if (web_name.indexOf(db_name.match('.*\\(即(.*)\\)')[1]) >= 0) {
return true;
}
}
// 處理 高雄市私立新東海老人養護中心
if (db_name.match('.*私立(.*)')) {
if (web_name.indexOf(db_name.match('.*私立(.*)')[1]) >= 0) {
return true;
}
}
return false;
};
var search_package_by_name = function(name, cb, checker){
get_choosed_packages(function(choosed_packages){
for (var id in choosed_packages) {
(function(id){
get_package_csv_by_id(id, function(package_csv){
if ('undefined' == typeof(package_csv)) {
return;
}
var rows;
for (var i = 0; i < package_csv.length; i ++) {
rows = package_csv[i];
if (checker(name, rows[0])) {
cb(id, rows);
}
}
});
})(id);
}
});
};
var search_package_by_name_api = function(name, url, cb, failed_cb){
get_choosed_packages(function(choosed_packages){
var packages = [];
for (var id in choosed_packages) {
packages.push(id);
}
$.get('https://jobhelper.g0v.ronny.tw/api/search?name=' + encodeURIComponent(name) + '&url=' + encodeURIComponent(url) + '&packages=' + encodeURIComponent(packages.join(',')), function(ret){
if (ret.error) {
failed_cb(ret.message);
return;
}
var d;
for (var i = 0; i < ret.data.length; i ++) {
d = ret.data[i];
cb(d.package_id, [d.name, d.date, d.reason, d.link, d.snapshot]);
}
}, 'json');
});
};
var htmlspecialchars = function(str){
var span_dom = document.createElement('span');
span_dom.innerText = str;
str = span_dom.innerHTML;
delete(span_dom);
return str;
};