|
| 1 | +/** |
| 2 | + * @type {Map<string, boolean>} |
| 3 | + */ |
| 4 | +const tabinfo = new Map(); |
| 5 | + |
| 6 | +/** |
| 7 | + * 扫描响应头,是否含有 Content-Security-Policy |
| 8 | + * @param {object[]} headers |
| 9 | + * @returns {boolen} |
| 10 | + */ |
| 11 | +function hasCSP(headers) { |
| 12 | + return headers.some(x => x.name.toLowerCase() === 'content-security-policy') |
| 13 | +}; |
| 14 | + |
| 15 | +chrome.webRequest.onHeadersReceived.addListener( |
| 16 | + function (details) { |
| 17 | + tabinfo.set(details.tabId, hasCSP(details.responseHeaders)) |
| 18 | + }, |
| 19 | + { |
| 20 | + urls: ['<all_urls>'], |
| 21 | + types: ['main_frame'], |
| 22 | + }, |
| 23 | + ['responseHeaders'] |
| 24 | +); |
| 25 | + |
1 | 26 | chrome.webRequest.onBeforeRequest.addListener(
|
2 |
| - function(request) { |
3 |
| - var url = request.url.replace('http://', 'https://') |
4 |
| - url = url.replace('googleapis.com', 'proxy.ustclug.org'); |
5 |
| - url = url.replace('themes.googleusercontent.com', 'google-themes.lug.ustc.edu.cn'); |
6 |
| - url = url.replace('fonts.gstatic.com', 'fonts-gstatic.lug.ustc.edu.cn'); |
7 |
| - url = url.replace('www.google.com/recaptcha/','www.recaptcha.net/recaptcha/'); |
8 |
| - return {redirectUrl: url}; |
| 27 | + function (details) { |
| 28 | + if (tabinfo.get(details.tabId)) { |
| 29 | + return details.url; |
| 30 | + } |
| 31 | + let url = details.url.replace('http://', 'https://') |
| 32 | + url = url.replace('ajax.googleapis.com', 'ajax.loli.net'); |
| 33 | + url = url.replace('fonts.googleapis.com', 'fonts.loli.net'); |
| 34 | + url = url.replace('themes.googleusercontent.com', 'themes.loli.net'); |
| 35 | + url = url.replace('fonts.gstatic.com', 'gstatic.loli.net'); |
| 36 | + url = url.replace('www.google.com/recaptcha/', 'www.recaptcha.net/recaptcha/'); |
| 37 | + url = url.replace('secure.gravatar.com', 'gravatar.loli.net'); |
| 38 | + return { redirectUrl: url }; |
9 | 39 | },
|
10 | 40 | {
|
11 | 41 | urls: [
|
12 | 42 | "*://ajax.googleapis.com/*",
|
| 43 | + "*://fonts.googleapis.com/*", |
13 | 44 | "*://themes.googleusercontent.com/*",
|
14 | 45 | "*://fonts.gstatic.com/*",
|
15 |
| - "*://www.google.com/recaptcha/*" |
| 46 | + "*://www.google.com/recaptcha/*", |
| 47 | + "*://secure.gravatar.com/*", |
16 | 48 | ]
|
17 | 49 | },
|
18 | 50 | ["blocking"]
|
19 | 51 | );
|
| 52 | + |
| 53 | +chrome.tabs.onRemoved.addListener(function (tabId) { |
| 54 | + tabinfo.delete(tabId); |
| 55 | +}); |
0 commit comments