Skip to content

Commit

Permalink
添加json格式化插件 josdejong/svelte-jsoneditor和1条默认候选规则 (#161)
Browse files Browse the repository at this point in the history
* 添加json格式化插件 josdejong/svelte-jsoneditor

* 添加josdejong/svelte-jsoneditor 源代码

* 添加 默认候选规则,规则 id=9999

* update release date
  • Loading branch information
jingjingxyk authored Feb 21, 2023
1 parent fabc58d commit 62adce1
Show file tree
Hide file tree
Showing 26 changed files with 33,681 additions and 93 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ extension/tools/download-chromium-extension-backup.sh
extension/third_party/
!extension/third_party/README.md
!extension/third_party/jingjingxyk
!extension/third_party/josdejong/svelte-jsoneditor

dist
extension/manifest-backup.json
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG-v3-0.10.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

## [一个完整的 测试环境例子 可用于查看优秀的源码 ](https://github.com/jingjingxyk/extension-v3-test)

## CHANGELOG for v3 0.10.24 [2023-02-19 21:25:00 +0800]

> 1. v3 版本 添加 默认候选规则,规则 id=9999,priority=9999(暂不启用)
> 1. v3 版本 备注信息: 规则 id=9999,用于覆盖其他地址重定向规则
> 1. v3 版本 添加 JSON [(josdejong/svelte-jsoneditor)](https://github.com/josdejong/svelte-jsoneditor)格式化工具
## CHANGELOG for v3 0.10.23 [2023-02-07 11:21:00 +0800]

> 1. v3 版本 修改 静态规则和默认候选项规则 规则编号统一一致
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@

## 扩展选项页使用简述

1. 点击扩展图标-自动打开扩展选项页
1. <strong>点击扩展图标-自动打开扩展选项页</strong>
1. [扩展选项配置原理](extension/options_ui/README.md)

## 功能变更记录[`CHANGELOG-0.10.x`](CHANGELOG-0.10.x.md)
## v3 功能变更记录[`CHANGELOG-0.10.x-V3`](CHANGELOG-v3-0.10.x.md)

## v2 功能变更记录[`CHANGELOG-0.10.x-V2`](CHANGELOG-v2-0.10.x.md)

## [可用 公共 CDN 静态资源库 和 网络拨测工具](extension/rules/README.md)

Expand Down
176 changes: 88 additions & 88 deletions extension-v2/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,100 +50,100 @@ const remove_csp_urls = [
*/

chrome.webRequest.onHeadersReceived.addListener(
(details) => {
//移除响应头 Content-Security-Policy
details.responseHeaders = details.responseHeaders.filter(
(response_header) =>
!remove_csp_item.includes(response_header.name.toLowerCase())
);
return {
responseHeaders: details.responseHeaders,
};
},
{
// urls: ["<all_urls>"],
urls: [
...remove_csp_urls, //需要移除 Content-Security-Policy 的URL地址
],
types: [
"main_frame",
"sub_frame",
"stylesheet",
"script",
"image",
"font",
"object",
"xmlhttprequest",
"ping",
"other",
],
},
["blocking", "responseHeaders"]
(details) => {
//移除响应头 Content-Security-Policy
details.responseHeaders = details.responseHeaders.filter(
(response_header) =>
!remove_csp_item.includes(response_header.name.toLowerCase())
);
return {
responseHeaders: details.responseHeaders,
};
},
{
// urls: ["<all_urls>"],
urls: [
...remove_csp_urls, //需要移除 Content-Security-Policy 的URL地址
],
types: [
"main_frame",
"sub_frame",
"stylesheet",
"script",
"image",
"font",
"object",
"xmlhttprequest",
"ping",
"other",
],
},
["blocking", "responseHeaders"]
);

/*
请求地址重定向
1. https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest/onBeforeRequest
*/
chrome.webRequest.onBeforeRequest.addListener(
function (details) {
let url = details.url.replace("http://", "https://");
url = url.replace("ajax.googleapis.com", "ajax.loli.net");
//url = url.replace("fonts.googleapis.com", "fonts.loli.net");
url = url.replace("fonts.googleapis.com", "fonts.googleapis.cn");
url = url.replace("themes.googleusercontent.com", "themes.loli.net");
//url = url.replace("fonts.gstatic.com", "gstatic.loli.net");
url = url.replace("fonts.gstatic.com", "fonts.gstatic.cn");
url = url.replace(
"www.google.com/recaptcha/",
"www.recaptcha.net/recaptcha/"
);
url = url.replace("secure.gravatar.com", "gravatar.loli.net");
url = url.replace("www.gravatar.com", "gravatar.loli.net");
url = url.replace("en.gravatar.com", "gravatar.loli.net");
url = url.replace("cn.gravatar.com", "gravatar.loli.net");
function (details) {
let url = details.url.replace("http://", "https://");
url = url.replace("ajax.googleapis.com", "ajax.loli.net");
//url = url.replace("fonts.googleapis.com", "fonts.loli.net");
url = url.replace("fonts.googleapis.com", "fonts.googleapis.cn");
url = url.replace("themes.googleusercontent.com", "themes.loli.net");
//url = url.replace("fonts.gstatic.com", "gstatic.loli.net");
url = url.replace("fonts.gstatic.com", "fonts.gstatic.cn");
url = url.replace(
"www.google.com/recaptcha/",
"www.recaptcha.net/recaptcha/"
);
url = url.replace("secure.gravatar.com", "gravatar.loli.net");
url = url.replace("www.gravatar.com", "gravatar.loli.net");
url = url.replace("en.gravatar.com", "gravatar.loli.net");
url = url.replace("cn.gravatar.com", "gravatar.loli.net");

url = url.replace("cdn.jsdelivr.net", "fastly.jsdelivr.net");
//"cdn.bootcdn.net/ajax/libs/twitter-bootstrap/"
//"cdn.jsdelivr.net/npm/bootstrap@$1/dist/$2"
url = url.replace(
/maxcdn\.bootstrapcdn\.com\/bootstrap\/(\d{1,4}\.\d{1,4}\.\d{1,4})\/(.*?)/g,
"lib.baomitu.com/twitter-bootstrap/$1/$2"
);
url = url.replace(
/code\.jquery\.com\/jquery-(\d{1,4}\.\d{1,4}\.\d{1,4})(.*?)/g,
"lib.baomitu.com/jquery/$1/jquery$2"
);
url = url.replace(
/code\.jquery\.com\/ui\/(\d{1,4}\.\d{1,4}\.\d{1,4})\/(.*?)/g,
"ajax.aspnetcdn.com/ajax/jquery.ui/$1/$2"
);
url = url.replace("developers.google.com", "developers.google.cn");
url = url.replace("developer.android.com", "developer.android.google.cn");
url = url.replace("source.android.com", "source.android.google.cn");
url = url.replace("www.gstatic.com", "www.gstatic.cn");
return { redirectUrl: url };
},
{
urls: [
"*://ajax.googleapis.com/*",
"*://fonts.googleapis.com/*",
"*://themes.googleusercontent.com/*",
"*://fonts.gstatic.com/*",
"*://www.google.com/recaptcha/*",
"*://secure.gravatar.com/*",
"*://en.gravatar.com/*",
"*://cn.gravatar.com/*",
"*://www.gravatar.com/*",
"*://maxcdn.bootstrapcdn.com/bootstrap/*",
"*://cdn.jsdelivr.net/*",
"*://code.jquery.com/jquery-*",
"*://code.jquery.com/ui/*",
"*://developers.google.com/*",
"*://developer.android.com/*",
"*://source.android.com/*",
"*://www.gstatic.com/*",
],
},
["blocking"]
url = url.replace("cdn.jsdelivr.net", "fastly.jsdelivr.net");
//"cdn.bootcdn.net/ajax/libs/twitter-bootstrap/"
//"cdn.jsdelivr.net/npm/bootstrap@$1/dist/$2"
url = url.replace(
/maxcdn\.bootstrapcdn\.com\/bootstrap\/(\d{1,4}\.\d{1,4}\.\d{1,4})\/(.*?)/g,
"lib.baomitu.com/twitter-bootstrap/$1/$2"
);
url = url.replace(
/code\.jquery\.com\/jquery-(\d{1,4}\.\d{1,4}\.\d{1,4})(.*?)/g,
"lib.baomitu.com/jquery/$1/jquery$2"
);
url = url.replace(
/code\.jquery\.com\/ui\/(\d{1,4}\.\d{1,4}\.\d{1,4})\/(.*?)/g,
"ajax.aspnetcdn.com/ajax/jquery.ui/$1/$2"
);
url = url.replace("developers.google.com", "developers.google.cn");
url = url.replace("developer.android.com", "developer.android.google.cn");
url = url.replace("source.android.com", "source.android.google.cn");
url = url.replace("www.gstatic.com", "www.gstatic.cn");
return { redirectUrl: url };
},
{
urls: [
"*://ajax.googleapis.com/*",
"*://fonts.googleapis.com/*",
"*://themes.googleusercontent.com/*",
"*://fonts.gstatic.com/*",
"*://www.google.com/recaptcha/*",
"*://secure.gravatar.com/*",
"*://en.gravatar.com/*",
"*://cn.gravatar.com/*",
"*://www.gravatar.com/*",
"*://maxcdn.bootstrapcdn.com/bootstrap/*",
"*://cdn.jsdelivr.net/*",
"*://code.jquery.com/jquery-*",
"*://code.jquery.com/ui/*",
"*://developers.google.com/*",
"*://developer.android.com/*",
"*://source.android.com/*",
"*://www.gstatic.com/*",
],
},
["blocking"]
);
2 changes: 1 addition & 1 deletion extension/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"icons": {
"128": "icons/128.png"
},
"description": "将 Google CDN 替换为国内镜像 v3",
"description": "将 Google CDN 替换为国内镜像 v3 更新日期:2023-02-19",
"background": {
"service_worker": "js/background.js",
"type": "module"
Expand Down
2 changes: 2 additions & 0 deletions extension/options_ui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,10 @@ chrome.declarativeNetRequest.updateDynamicRules(
| 17 | developers.google.com | 启用 |
| 18 | source.android.com | 启用 |
| 19 | www.google.com/recaptcha/api.js | 启用 |
| 9999 | supper-priority-override-rule | 默认未启用 |

> 默认未启用的规则,如何启用? 答:在默认候选项选选项中,选中规则即启用规,规则列表可以看到启用的规则
> 规则 id=9999,priority=9999。规则拥有最高优先级,作用: 覆盖其他地址重定向规则
## 演示地址的规则来源

Expand Down
6 changes: 6 additions & 0 deletions extension/options_ui/advance.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
<meta charset="UTF-8" />
<title>扩展高级选项设置</title>
<link href="/options_ui/css/app.css" type="text/css" rel="stylesheet" />
<link
href="/third_party/josdejong/svelte-jsoneditor/main/themes/jse-theme-dark.css"
type="text/css"
rel="stylesheet"
/>
<style>
.nav-bar-index {
margin-left: 800px;
Expand Down Expand Up @@ -202,6 +207,7 @@ <h5></h5>
</button>
</div>
<div><span class="notice"></span></div>
<div id="jsoneditor" class="jse-theme-dark"></div>
</div>
</div>
</section>
Expand Down
6 changes: 6 additions & 0 deletions extension/options_ui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
<meta charset="UTF-8" />
<title>扩展默认选项设置</title>
<link href="/options_ui/css/app.css" type="text/css" rel="stylesheet" />
<link
href="/third_party/josdejong/svelte-jsoneditor/main/themes/jse-theme-dark.css"
type="text/css"
rel="stylesheet"
/>
<style>
.nav-bar-index {
margin-left: 800px;
Expand Down Expand Up @@ -110,6 +115,7 @@ <h5></h5>
</button>
</div>
<div><span class="notice"></span></div>
<div id="jsoneditor" class="jse-theme-dark"></div>
</div>
</div>
</section>
Expand Down
26 changes: 26 additions & 0 deletions extension/options_ui/js/advance.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
(async () => {
let { JSONEditor } = await import(
"/third_party/josdejong/svelte-jsoneditor/main/index.js"
);
let { main } = await import("/options_ui/js/component/main.js");
let { sync_remote_conf } = await import(
"/options_ui/js/component/sync-remote-conf-rule.js"
Expand All @@ -16,6 +19,29 @@
console.log(count);
});

window.editor = new JSONEditor({
target: document.getElementById("jsoneditor"),
props: {
mode: "text",
onChange: (
updatedContent,
previousContent,
{ contentErrors, patchResult }
) => {
// content is an object { json: JSONData } | { text: string }
console.log("onChange", {
updatedContent,
previousContent,
contentErrors,
patchResult,
});
console.log(updatedContent);
let content_box = document.querySelector("#rule-content-container");
content_box.value = updatedContent.text;
},
},
});

window.addEventListener(
"message",
(event) => {
Expand Down
12 changes: 11 additions & 1 deletion extension/options_ui/js/component/default-domains.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ let default_domains = {
id: 16,
domain: "imgur.com",
},
/*
"supper-priority-override-rule": {
id: 9999,
domain: "supper-priority-override",
},
*/
};

let reset_default_domain_app = () => {
Expand All @@ -88,7 +94,7 @@ let reset_default_domain_app = () => {
{
addRules: [],
removeRuleIds: [
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
9999, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
],
},
(parameter) => {
Expand Down Expand Up @@ -164,6 +170,10 @@ let default_domains_app = () => {

rule["id"] = rule_id;
rule["priority"] = 2;
//特殊定制规则ID=9999
if (rule_id === 9999) {
rule["priority"] = 9999;
}
console.log(rule, rule_id);
chrome.declarativeNetRequest.updateDynamicRules(
{
Expand Down
13 changes: 13 additions & 0 deletions extension/options_ui/js/component/show-rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ let showRuleJSON = (rule) => {
.then((x) => x.json())
.then((x) => {
//console.log(JSON.stringify(x));
editor.set({
json: x,
text: undefined,
});
let content_box = document.querySelector("#rule-content-container");
content_box.value = JSON.stringify(x);
content_box.setAttribute("rule-type", "static");
Expand Down Expand Up @@ -120,6 +124,15 @@ let showRuleList = (type = "all_dynamic_rule") => {
//console.log(event.target.nodeName);
if (event.target.nodeName === "LI") {
let content_box = document.querySelector("#rule-content-container");
let content = decodeURIComponent(
event.target.getAttribute("data-origin")
);
//console.log(content);

editor.set({
json: JSON.parse(content),
text: undefined,
});
content_box.value = decodeURIComponent(
event.target.getAttribute("data-origin")
);
Expand Down
Loading

0 comments on commit 62adce1

Please sign in to comment.