-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
231 lines (215 loc) · 6.12 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
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
// const
const WEDATA_IMPORT_URL = 'http://wedata.net/databases/AutoPagerize/items_all.json';
const WEDATA_RULE_REQUIRED_KEYS = ['nextLink', 'pageElement', 'url'];
const MICROFORMATS = [
{
id: 'autopargerize-format',
url: '^https?://.',
nextLink: '//a[@rel="next"] | //link[@rel="next"]',
insertBefore: '//*[contains(concat(" ",@class," "), " autopagerize_insert_before ")]',
pageElement: '//*[contains(concat(" ",@class," "), " autopagerize_page_element ")]',
},
];
const DEFAULT_OPTIONS = {
debug: false, // for develop
enable: true,
detectURLChange: true,
targetWindowName: '_blank',
baseRemainHeight: 400,
minRequestInterval: 2000,
};
const STATUS_COLORS = {
enabled: '#0F9D58', // green
loading: '#F4B400', // yellow
error: '#DB4437', // red
terminated: '#4285F4', // blue
disabled: '#B3B3B3', // gray
};
// debug
function debug() {
if (pagerOptions.getSync().debug) {
// eslint-disable-next-line prefer-rest-params
console.debug('[pagerization background]', ...arguments);
}
}
// xhr wrapper
function fetchJSON(url) {
return new Promise((resolve, reject) => {
const req = new XMLHttpRequest();
req.onload = () => {
req.response ? resolve(req) : reject(req);
};
req.onerror = () => {
reject(req);
};
req.responseType = 'json';
req.open('GET', url, true);
req.send(null);
});
}
// icon generator
function generateIconContext(color, size, x, r, s) {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
const y = x;
const w = size - x * 2;
const h = size - y * 2;
canvas.width = size;
canvas.height = size;
if (s) {
const n = parseInt(color.slice(1), 16);
ctx.shadowColor = `rgba(${n >> 16}, ${(n >> 8) & 255}, ${n & 255}, 0.2)`;
ctx.shadowBlur = 0;
ctx.shadowOffsetX = 0;
ctx.shadowOffsetY = s;
}
ctx.fillStyle = color;
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + w, y, x + w, y + h, r);
ctx.arcTo(x + w, y + h, x, y + h, r);
ctx.arcTo(x, y + h, x, y, r);
ctx.arcTo(x, y, x + w, y, r);
ctx.closePath();
ctx.fill();
return ctx;
}
// storage util
const storage = {
getRawData(key) {
const val = localStorage.getItem(key);
return val ? JSON.parse(val) : null;
},
setRawData(key, val) {
localStorage.setItem(key, JSON.stringify(val));
},
getSync(key, force) {
let data = this.getRawData(key);
if (!force && data && data.expire) {
if (new Date(data.expire).getTime() <= Date.now()) {
data = null;
}
}
return data && ('value' in data) ? data.value : data;
},
setSync(key, value, expire) {
const data = { value };
if (expire) {
if (expire instanceof Date) {
data.expire = expire.toString();
} else if (typeof expire === 'number') {
data.expire = new Date(Date.now() + expire).toString();
} else {
throw new Error('invalid expire type');
}
}
this.setRawData(key, data);
},
touch(key, expire) {
const data = this.getSync(key, true);
if (data) this.setSync(key, data, expire);
},
};
// rules util
const pagerRules = {
data: null,
expire: 1000 * 60 * 60 * 24, // expire 1 day
getSync() {
if (!this.data) {
const data = storage.getSync('wedataRules', true);
this.setup(data && data.rules || []);
}
return this.data;
},
setup(rules) {
// TODO: attach custom rules
this.data = rules;
},
fetch(force) {
if (!force && storage.getSync('wedataRules') !== null) return new Promise(() => {});
debug('[fetch]', 'request');
return fetchJSON(WEDATA_IMPORT_URL).then((req) => {
debug('[fetch]', 'succeed to fetch rules');
const rules = [];
req.response.forEach((datum) => {
const d = datum.data || datum;
const r = {};
for (const k of WEDATA_RULE_REQUIRED_KEYS) { // eslint-disable-line no-restricted-syntax
if (d[k]) r[k] = d[k];
if (!r[k]) return;
}
try {
new RegExp(r.url); // eslint-disable-line no-new
} catch (e) {
debug('[fetch]', 'invalid url', r.url, r);
return;
}
r.id = parseInt(datum.resource_url.match(/\d+$/), 10);
rules.push(r);
});
rules.sort((a, b) => b.url.length - a.url.length);
rules.push(...MICROFORMATS);
storage.setSync('wedataRules', { rules }, this.expire); // expire 1 day
this.setup(rules);
return Promise.resolve(req);
}, (req) => {
debug('[fetch]', 'fail to fetch rules');
storage.touch('wedataRules', this.expire);
return Promise.reject(req);
});
},
};
// options util
const pagerOptions = {
data: storage.getSync('options') || Object.assign({}, DEFAULT_OPTIONS),
getSync() {
return this.data;
},
setSync(data) {
storage.setSync('options', data);
this.data = data;
},
};
// initialize
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
debug('[onMessage]', 'request', request);
switch (request.action) {
case 'Pagerization.initialize':
const url = request.url;
sendResponse({
rules: url ? pagerRules.getSync().filter((rule) => new RegExp(rule.url).test(url)) : [],
options: pagerOptions.getSync(),
});
break;
case 'Pagerization.changeStatus':
const tabId = sender.tab.id;
const ctx = generateIconContext(STATUS_COLORS[request.status], 19, 3, 2);
chrome.pageAction.setIcon({
tabId,
imageData: ctx.getImageData(0, 0, 19, 19),
});
chrome.pageAction.show(tabId);
sendResponse({});
break;
case 'Pagerization.setOptions':
pagerOptions.setSync(request.options);
sendResponse({});
break;
case 'Pagerization.fetchRules':
pagerRules.fetch(request.force).then(() => {
sendResponse({ status: 'success' });
}, () => {
sendResponse({ status: 'failure' });
});
break;
default:
throw new Error(`invalid request action: ${request.action}`);
}
return true;
});
chrome.pageAction.onClicked.addListener((tab) => {
chrome.tabs.sendMessage(tab.id, {
action: 'Pagerization.toggleStatus',
});
});
pagerRules.fetch();