From 4685d7584c8118665a2b102f6d2e9bedbb7244d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A5=BD=E5=90=A7=EF=BC=8C=E4=BD=A0=E6=83=B3=E8=AF=B4?= =?UTF-8?q?=E5=95=A5?= Date: Fri, 30 Sep 2022 22:20:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=A9=E5=B1=95=E9=80=89=E9=A1=B9=E9=A1=B5?= =?UTF-8?q?=E5=AE=8C=E5=96=84-=E7=AC=AC=E4=BA=8C=E7=89=88=20(#102)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 重新启用对firefox 的支持和衍生新仓库 * 新增 高阶定制玩法 辅助工具 新增 扩展程序选项 使用 prettier 格式化代码 * change options_ui * test update-auth.sh * save update-domain.sh changed * save change * fix error * fix error * add public static library * fix error * 修改打包程序,打包时去除无用文件 * fix test/READM.md test * change v2 version * change auto deploy.yaml setup * change release-archive.sh * 扩展选项:添加规则功能暂时屏蔽 * 完善扩展选项页 * 完善自定义规则 * 完善自定义规则 * 完善自定义规则 * 完善自定义规则 * 完善自定义规则 * 完善自定义规则 * 完善自定义规则 * 完善扩展选项页 * 完善扩展选项页 * 完善扩展选项页 * 完善扩展选项页 * 完善扩展选项页 --- .gitignore | 4 +- CHANGELOG-0.10.x.md | 4 + extension/manifest.json | 10 +- extension/options_ui/css/app.css | 20 +- extension/options_ui/index.html | 58 ++-- extension/options_ui/js/app.js | 265 ++++++++++++++++-- extension/rules/advance-no-use/auth.json | 1 - .../example-no-use/rules_append_header_1.json | 1 - ...remove_content_security_policy_header.json | 1 - extension/sandbox/index.html | 12 + extension/sandbox/js/app.js | 29 ++ extension/test/README.md | 23 ++ 12 files changed, 375 insertions(+), 53 deletions(-) create mode 100644 extension/sandbox/index.html create mode 100644 extension/sandbox/js/app.js diff --git a/.gitignore b/.gitignore index 5d541168..facf038c 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,6 @@ extension/tools/venv/ extension/tools/chrome-linux.zip extension/tools/chrome-linux/ extension/test/nohup.out -dist \ No newline at end of file +dist +extension/manifest-backup.json + diff --git a/CHANGELOG-0.10.x.md b/CHANGELOG-0.10.x.md index ce439bfe..bdf57a74 100644 --- a/CHANGELOG-0.10.x.md +++ b/CHANGELOG-0.10.x.md @@ -4,6 +4,10 @@ ## [一个完整的 测试环境例子 可用于查看优秀的源码 ](https://github.com/jingjingxyk/extension-v3-test) +## CHANGELOG for 0.10.8 [2022-09-30 19:43:40 +0800] +> 1. 扩展选项页添加同步远端规则功能 +> 2. 扩展选项页完善自定义规则功能 + ## CHANGELOG for 0.10.7 [2022-07-31 12:43:40 +0800] > 1. 新增 `developers.google.com` - 替换为 `developers.google.cn` diff --git a/extension/manifest.json b/extension/manifest.json index e3587459..567967fe 100644 --- a/extension/manifest.json +++ b/extension/manifest.json @@ -1,6 +1,6 @@ { "name": "Replace Google CDN", - "version": "0.10.7", + "version": "0.10.8", "manifest_version": 3, "icons": { "128": "icons/128.png" @@ -41,12 +41,18 @@ ] }, "content_security_policy": { - "extension_pages": "default-src 'self';script-src 'self' 'wasm-unsafe-eval' ;object-src 'self'; style-src 'self' 'unsafe-inline';" + "extension_pages": "default-src 'self';script-src 'self' 'wasm-unsafe-eval' ;object-src 'self'; style-src 'self' 'unsafe-inline'; connect-src https://github.com https://www.jingjingxyk.com https://jingjingxyk.github.io http://localhost https://raw.githubusercontent.com https://gitee.com ;", + "sandbox": "sandbox allow-scripts allow-popups; script-src 'unsafe-inline' 'unsafe-eval' " }, "options_ui": { "page": "options_ui/index.html", "open_in_tab": true }, + "sandbox": { + "pages": [ + "sandbox/index.html" + ] + }, "author": "https://github.com/justjavac", "homepage_url": "https://github.com/justjavac/ReplaceGoogleCDN" } diff --git a/extension/options_ui/css/app.css b/extension/options_ui/css/app.css index e5c76764..aa04502a 100644 --- a/extension/options_ui/css/app.css +++ b/extension/options_ui/css/app.css @@ -7,9 +7,23 @@ input { } li { font-size: 16px; + list-style: decimal; +} +.box{ + border-radius: 6px; + color: rgba(0,0,0, 0.87); + background: #fff; + box-shadow: 0 2px 2px 0 rgb(0 0 0 / 14%), 0 3px 1px -2px rgb(0 0 0 / 20%), 0 1px 5px 0 rgb(0 0 0 / 12%); +} + +iframe{ + border:none; } -textarea { - min-width: 200px; - min-height: 200px; +.rule_dynamic_set_list li:hover,.rule_static_set_list li:hover{ + + color: #c5955d; + font-size: 1.3rem; + } + diff --git a/extension/options_ui/index.html b/extension/options_ui/index.html index 5ee31fc6..59ef0c26 100644 --- a/extension/options_ui/index.html +++ b/extension/options_ui/index.html @@ -2,35 +2,55 @@ - Title + 扩展选项

扩展选项

-

添加自定义规则

-
+ +
+

获取远程服务器端配置规则

+ +
+ + +
- - +

添加自定义规则

+ +
+ +
+ + +
+ +
+

已启用的静态规则列表:

+
    +
  • +
+ +

已启用的动态规则列表:

+
    +
  • +
+

规则详情:

+
+ +
+
+
+ +
-
-

已启用的规则列表:

- -

规则详情:

-
-
diff --git a/extension/options_ui/js/app.js b/extension/options_ui/js/app.js index f4c172c0..0ecfa8d4 100644 --- a/extension/options_ui/js/app.js +++ b/extension/options_ui/js/app.js @@ -5,7 +5,7 @@ let showRuleJSON = (rule) => { fetch(url) .then((x) => x.json()) .then((x) => { - console.log(x); + console.log(JSON.stringify(x)); document.querySelector("#rule-content-container").value = JSON.stringify(x); }); @@ -25,30 +25,27 @@ let rules = { "/rules/rules_remove_content_security_policy_header.json", }; -let updateRule = (ruleinfo) => { - let id = 1; - let domain = "example.com"; - chrome.declarativeNetRequest.updateDynamicRules({ - addRules: [ - { - id: id, - priority: 1, - action: { type: "block" }, - condition: { urlFilter: domain, resourceTypes: ["main_frame"] }, - }, - ], - removeRuleIds: [id], - }); -}; - let getRuleList = () => { chrome.declarativeNetRequest.getAvailableStaticRuleCount((count) => { console.log(count); }); + chrome.declarativeNetRequest.getDynamicRules((rules) => { + console.log(rules); + let list_box = document.querySelector(".rule_dynamic_set_list"); + let list = ""; + rules.forEach((value, key, array) => { + console.log(value.id, value); + list += `
  • ${value.id}
  • `; + }); + list_box.innerHTML = list; + }); + chrome.declarativeNetRequest.getEnabledRulesets((rulesetIds) => { console.log(rulesetIds); - let list_box = document.querySelector(".rule_set_list"); + let list_box = document.querySelector(".rule_static_set_list"); let list = ""; rulesetIds.map((value, index) => { list += `
  • ${value}
  • `; @@ -57,25 +54,68 @@ let getRuleList = () => { }); document - .querySelector(".rule_set_list") + .querySelector(".rule_static_set_list") .addEventListener("click", (event) => { - console.log(event.target); - console.log(event.target.nodeType); - console.log(event.target.nodeName); + event.stopPropagation(); + event.preventDefault(); + //console.log(event.target); + //console.log(event.target.nodeType); + //console.log(event.target.nodeName); if (event.target.nodeName === "LI") { showRuleJSON(event.target.getAttribute("data-rule")); } }); + document + .querySelector(".rule_dynamic_set_list") + .addEventListener("click", (event) => { + event.stopPropagation(); + event.preventDefault(); + //console.log(event.target); + //console.log(event.target.nodeType); + //console.log(event.target.nodeName); + if (event.target.nodeName === "LI") { + console.log( + decodeURIComponent(event.target.getAttribute("data-origin")) + ); + document.querySelector("#rule-content-container").value = + decodeURIComponent(event.target.getAttribute("data-origin")); + } + }); + + /* + if (location.href.indexOf("problematic/url") !== -1) { + chrome.declarativeNetRequest.updateEnabledRulesets({"disableRulesetIds": ["rules"]}); + } else { + chrome.declarativeNetRequest.updateEnabledRulesets({"enableRulesetIds": ["rules"]}); + } + + */ + return; - chrome.declarativeNetRequest.getDynamicRules((rules) => { - console.log(rules); - }); + chrome.declarativeNetRequest.getMatchedRules({}, (RulesMatchedDetails) => { console.log(RulesMatchedDetails); }); }; +let deleteDynamicRules = () => { + chrome.declarativeNetRequest.getDynamicRules((rules) => { + console.log(rules); + let del_ids = []; + rules.forEach((value, key, array) => { + console.log(value, value.id); + del_ids.push(value.id); + }); + if (del_ids) { + chrome.declarativeNetRequest.updateDynamicRules({ + addRules: [], + removeRuleIds: del_ids, + }); + } + }); +}; + (async () => { let { encodeBase64, @@ -84,6 +124,181 @@ let getRuleList = () => { addClass, removeClass, createJSONFile, + fetchAll, + getContent, } = await import("/third_party/frontend-utils/utils.js"); getRuleList(); + + document + .querySelector(".delete-sync-remote-rule") + .addEventListener("click", (event) => { + event.stopPropagation(); + event.preventDefault(); + deleteDynamicRules(); + }); + + document + .querySelector(".goto-sync-remote-rule") + .addEventListener("click", async (event) => { + event.stopPropagation(); + event.preventDefault(); + let rule_server_urls = document + .querySelector(".remote-rule-urls") + .value.trim(); + let rules_urls = rule_server_urls.split("\n"); + let new_rules_urls = []; + rules_urls.forEach((value, index, array) => { + value = value.trim(); + value = value.replace(/^\s|\s$|'|,|,|。|"/g, ""); + if (value.length > 1) { + console.log(value); + new_rules_urls.push(value); + } + }); + console.log(new_rules_urls); + let default_rules_urls = []; + /* + default_rules_urls = [ + "https://github.com/jingjingxyk/extension-v3-test/blob/main/rules/auth.json?raw=true", + "https://github.com/jingjingxyk/extension-v3-test/blob/main/rules/rules_advance_redirect_1.json?raw=true", + "https://github.com/jingjingxyk/extension-v3-test/blob/main/rules/rules_advance_redirect_2.json?raw=true", + "https://github.com/jingjingxyk/extension-v3-test/blob/main/rules/rules_block_request.json?raw=true", + "https://github.com/jingjingxyk/extension-v3-test/blob/main/rules/rules_redirect_extra.json?raw=true", + "https://github.com/jingjingxyk/extension-v3-test/blob/main/rules/rules_remove_content_security_policy_header.json?raw=true", + ]; + */ + default_rules_urls = []; + rules_urls = new_rules_urls ? new_rules_urls : default_rules_urls; + + let result = await fetchAll(rules_urls, getContent); + console.log(result); + if (result.length > 1) { + deleteDynamicRules(); + + let dynamic_id_index = parseInt(new Date().getTime() / 1000); + let need_rules = []; + result.forEach((rules) => { + rules.forEach((rule, index, array) => { + console.log(rule); + rule.id = ++dynamic_id_index; + console.log(rule); + need_rules.push(rule); + }); + }); + console.log(need_rules); + chrome.declarativeNetRequest.updateDynamicRules( + { + addRules: need_rules, + removeRuleIds: [], + }, + (info) => { + console.log(info); + } + ); + } + }); + document.querySelector(".add-rule").addEventListener("click", (event) => { + event.stopPropagation(); + event.preventDefault(); + let rule_str = document.querySelector(".new-add-rule-pannel").value; + rule_str = rule_str.trim(); + if (rule_str.length) { + rule_str = JSON.parse(rule_str); + if (rule_str) { + let need_rules = []; + let dynamic_id_index = parseInt(new Date().getTime() / 1000); + if (rule_str.id) { + rule_str.id = ++dynamic_id_index; + need_rules = [rule_str]; + } else { + rule_str.forEach((value, key, array) => { + value.id = ++dynamic_id_index; + need_rules.push(value); + }); + } + + console.log(need_rules); + chrome.declarativeNetRequest.updateDynamicRules( + { + addRules: need_rules, + removeRuleIds: [], + }, + (info) => { + console.log(info); + } + ); + } + } + }); + + document + .querySelector(".add-rule-from-file") + .addEventListener("click", (event) => { + event.stopPropagation(); + event.preventDefault(); + document.querySelector("#upload-file-to-rule").click(); + }); + document + .querySelector("#upload-file-to-rule") + .addEventListener("change", (event) => { + const files = event.target.files; + if (files && files[0]) { + const file = files[0]; + console.log(file); + let reader = new FileReader(); + reader.onload = function () { + document.querySelector(".new-add-rule-pannel").value = this.result; + }; + reader.readAsText(file); + } + }); + document + .querySelector(".back-new-rule-to-json") + .addEventListener("click", (event) => { + event.stopPropagation(); + event.preventDefault(); + let rule_str = document.querySelector(".new-add-rule-pannel").value; + rule_str = rule_str.trim(); + if (rule_str.length) { + rule_str = JSON.parse(rule_str); + if (rule_str) { + createJSONFile( + rule_str, + "ReplaceGoogleCDN-backup-" + + parseInt(new Date().getTime() / 1000).toString() + + ".json" + ); + } + } + }); + + /* + let url = chrome.runtime.getURL("sandbox/index.html"); + chrome.tabs.create({url}, (callback) => { + console.log(callback) + }); + let iframe_src = document.querySelector("#external_page").getAttribute('src') + */ + window.addEventListener( + "message", + (event) => { + console.log(event, event.source); + event.source.postMessage("hi there yourself! the secret response ", "*"); + //event.source.postMessage("hi there yourself! the secret response ",event.origin); + //event.source.postMessage("hi there yourself! the secret response ",location.origin+iframe_src); + console.log(event.data); + if (event.origin === "null") { + let data = JSON.parse(event.data); + console.log(data); + + if (data.url) { + let url = data.url; + chrome.tabs.create({ url }, (callback) => { + console.log(callback); + }); + } + } + }, + false + ); })(); diff --git a/extension/rules/advance-no-use/auth.json b/extension/rules/advance-no-use/auth.json index 4e343dca..291afd98 100644 --- a/extension/rules/advance-no-use/auth.json +++ b/extension/rules/advance-no-use/auth.json @@ -28,7 +28,6 @@ ] }, "condition": { - "isUrlFilter": false, "urlFilter": "*", "requestDomains": ["*.proxy.domain.com"], "resourceTypes": [ diff --git a/extension/rules/example-no-use/rules_append_header_1.json b/extension/rules/example-no-use/rules_append_header_1.json index dfaf832d..727ef5ee 100644 --- a/extension/rules/example-no-use/rules_append_header_1.json +++ b/extension/rules/example-no-use/rules_append_header_1.json @@ -24,7 +24,6 @@ ] }, "condition": { - "isUrlFilter": false, "urlFilter": "*.com", "requestDomains": ["example.com"], "resourceTypes": [ diff --git a/extension/rules/rules_remove_content_security_policy_header.json b/extension/rules/rules_remove_content_security_policy_header.json index d8f2b2bc..d13b05f8 100644 --- a/extension/rules/rules_remove_content_security_policy_header.json +++ b/extension/rules/rules_remove_content_security_policy_header.json @@ -36,7 +36,6 @@ ] }, "condition": { - "isUrlFilter": false, "urlFilter": "*", "requestDomains": [ "ajax.googleapis.com", diff --git a/extension/sandbox/index.html b/extension/sandbox/index.html new file mode 100644 index 00000000..b5e3b0c3 --- /dev/null +++ b/extension/sandbox/index.html @@ -0,0 +1,12 @@ + + + + + 测试地址 + + +

    体验浏览chromium、webRTC源码

    + + + + \ No newline at end of file diff --git a/extension/sandbox/js/app.js b/extension/sandbox/js/app.js new file mode 100644 index 00000000..578be8c4 --- /dev/null +++ b/extension/sandbox/js/app.js @@ -0,0 +1,29 @@ +let preview_url = [ + "https://www.chromium.org/chromium-projects/", + "https://source.chromium.org/", + "https://summerofcode.withgoogle.com/programs/2022/organizations", + "https://cs.opensource.google/", + "https://webrtc.org/", + "https://chromium.googlesource.com/external/webrtc", +]; +let list_box = document.querySelector(".box"); +let list = ""; +preview_url.map((value, index) => { + list += `
  • ${value}
  • `; +}); +list_box.innerHTML = list; + +list_box.addEventListener("click", (event) => { + //console.log(event.target); + //console.log(event.target.nodeName); + if (event.target.nodeName === "PRE") { + let url = event.target.innerText; + //top.open(url, "_blank"); + window.parent.postMessage( + JSON.stringify({ + url: url, + }), + location.origin + "/options_ui/index.html" + ); + } +}); diff --git a/extension/test/README.md b/extension/test/README.md index 008ef375..852a194a 100644 --- a/extension/test/README.md +++ b/extension/test/README.md @@ -8,3 +8,26 @@ 4. [`cdn.jsdelivr.net` 替换为 `fastly.jsdelivr.net` 打开 `https://cdn.jsdelivr.net/` 查看结果 ](https://cdn.jsdelivr.net/) 5. [`cdnjs.cloudflare.com` 替换为 `cdnjs.loli.net` 打开 `https://cdnjs.cloudflare.com/` 查看结果 ](https://cdnjs.cloudflare.com/ajax/libs/reveal.js/4.1.2/reveal.min.css) 6. [`developers.google.com` 替换为 `developers.google.cn` 打开 `https://developers.google.com/` 查看结果 ](https://developers.google.com) + + +## 扩展选项页,同步服务器端规则例子一 +> [规则文件源地址](https://github.com/jingjingxyk/extension-v3-test/tree/main/rules/) +```text + https://www.jingjingxyk.com/chromium-extension/extension-v3-test/rules/auth.json?raw=true + https://www.jingjingxyk.com/chromium-extension/extension-v3-test/rules/rules_advance_redirect_1.json?raw=true + https://www.jingjingxyk.com/chromium-extension/extension-v3-test/rules/rules_advance_redirect_2.json?raw=true + https://www.jingjingxyk.com/chromium-extension/extension-v3-test/rules/rules_block_request.json?raw=true + https://www.jingjingxyk.com/chromium-extension/extension-v3-test/rules/rules_redirect_extra.json?raw=true + https://www.jingjingxyk.com/chromium-extension/extension-v3-test/rules/rules_remove_content_security_policy_header.json?raw=true + +``` +## 扩展选项页,同步服务器端规则例子二 +```text + https://github.com/jingjingxyk/extension-v3-test/blob/main/rules/auth.json?raw=true + https://github.com/jingjingxyk/extension-v3-test/blob/main/rules/rules_advance_redirect_1.json?raw=true + https://github.com/jingjingxyk/extension-v3-test/blob/main/rules/rules_advance_redirect_2.json?raw=true + https://github.com/jingjingxyk/extension-v3-test/blob/main/rules/rules_block_request.json?raw=true + https://github.com/jingjingxyk/extension-v3-test/blob/main/rules/rules_redirect_extra.json?raw=true + https://github.com/jingjingxyk/extension-v3-test/blob/main/rules/rules_remove_content_security_policy_header.json?raw=true + +``` \ No newline at end of file