Skip to content

Commit 7bb2dfb

Browse files
committed
添加 Content-Security-Policy
Signed-off-by: 迷渡 <[email protected]>
1 parent 986b147 commit 7bb2dfb

File tree

3 files changed

+48
-12
lines changed

3 files changed

+48
-12
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ Replace Google CDN
1818

1919
## 原理
2020

21-
目前国内 CDN 使用的[中科大](http://lug.ustc.edu.cn)源。
22-
2321
此插件/扩展包括:
2422

2523
1. `ajax.googleapis.com` - 前端公共库
2624
1. `fonts.googleapis.com` - 免费字体库
2725
1. `themes.googleusercontent.com` - fonts 有时会使用到这个里面的资源
26+
1. `fonts.gstatic.com` - 免费字体库
2827
1. `www.google.com/recaptcha` - Google 图像验证库
28+
1. `secure.gravatar.com` - gravatar 头像
2929

3030
## 安装
3131

chrome/js/background.js

+44-8
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,55 @@
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+
126
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 };
939
},
1040
{
1141
urls: [
1242
"*://ajax.googleapis.com/*",
43+
"*://fonts.googleapis.com/*",
1344
"*://themes.googleusercontent.com/*",
1445
"*://fonts.gstatic.com/*",
15-
"*://www.google.com/recaptcha/*"
46+
"*://www.google.com/recaptcha/*",
47+
"*://secure.gravatar.com/*",
1648
]
1749
},
1850
["blocking"]
1951
);
52+
53+
chrome.tabs.onRemoved.addListener(function (tabId) {
54+
tabinfo.delete(tabId);
55+
});

chrome/manifest.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"name": "Replace Google CDN",
3-
"version": "0.5.0",
3+
"version": "0.6.0",
44
"manifest_version": 2,
55
"icons": {
66
"128": "icons/128.png"
77
},
8-
"description": "Replace Google CDN with USTC CDN.",
8+
"description": " Google CDN 替换为国内镜像",
99
"background": {
1010
"scripts": ["js/background.js"]
1111
},

0 commit comments

Comments
 (0)