Skip to content

Commit

Permalink
添加备份自定义规则 (#201)
Browse files Browse the repository at this point in the history
* 添加备份自定义规则

* 移除备份所有动态规则功能,新增备份自定义规则功能

* 移除备份所有动态规则功能,新增备份自定义规则功能

* 移除备份所有动态规则功能,新增备份自定义规则功能

* 移除备份所有动态规则功能,新增备份自定义规则功能

* update changelog

* update gobal dynamic rule

* update gobal dynamic rule
  • Loading branch information
jingjingxyk authored Dec 7, 2023
1 parent 2a6fa7e commit ade4a2c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG-v3-x.x.x.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG for v3

## CHANGELOG for v3 0.11.0 [2023-12-06 21:01:00 +0800]

> 1. 新增: 新增备份自定义规则功能
> 1. 变更: 移除备份所有动态规则功能
## CHANGELOG for v3 0.10.32 [2023-12-05 15:01:00 +0800]

> 1. 新增: 创建 文档目录 `docs`
Expand Down
4 changes: 2 additions & 2 deletions extension/manifest.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "Replace Google CDN",
"version": "0.10.32",
"version": "0.11.0",
"manifest_version": 3,
"icons": {
"128": "icons/128.png"
},
"description": "将 Google CDN 替换为国内镜像 v3 更新日期:2023-12-05",
"description": "将 Google CDN 替换为国内镜像 v3 更新日期:2023-12-06",
"background": {
"service_worker": "js/background.js",
"type": "module"
Expand Down
4 changes: 2 additions & 2 deletions extension/options_ui/advance.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ <h1>扩展高级选项设置</h1>
<div class="container">
<section class="box">
<h2>选项一:全局动态规则处理</h2>
<button class="backup-all-dynamic-rule">
备份所有动态规则
<button class="backup-self-defined-dynamic-rule">
备份自定义动态规则
</button>
<span class="placeholder-box"></span>
<span class="placeholder-box"></span>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
deleteDynamicRules,
updateDynamicRules,
backupAllDynamicRules,
backupSelfDefinedDynamicRules,
utils,
enableStaticRules,
id_ranges
Expand All @@ -10,14 +10,15 @@ import { remote_repository_static_urls } from "../Config/rule_example_conf.js";
import showRuleList from "../../CommonBundle/Components/showRuleList.js";
import { showDynamicRules } from "../../CommonBundle/Components/showDynamicRules.js";

//备份所有动态规则
let bindBackupAllDynamicRuleEventListener = () => {
//绑定备份自定义动态规则事件

let bindBackupSelfDefinedDynamicRuleEventListener = () => {
document
.querySelector(".backup-all-dynamic-rule")
.querySelector(".backup-self-defined-dynamic-rule")
.addEventListener("click", (event) => {
event.stopPropagation();
event.preventDefault();
backupAllDynamicRules();
backupSelfDefinedDynamicRules();
});
};

Expand Down Expand Up @@ -199,8 +200,8 @@ let get_remote_repository_static_rule = async () => {
};

export default () => {
//备份所有动态规则
bindBackupAllDynamicRuleEventListener();
//备份自定义规则
bindBackupSelfDefinedDynamicRuleEventListener();
//删除所有动态规则
bindDeleteAllDynamicRuleEventListener();

Expand Down
32 changes: 27 additions & 5 deletions extension/options_ui/js/CommonBundle/Components/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ let id_ranges = {
sync_remote_rule: [40000, 320000],
all_dynamic_rule: [0, Infinity]
};

//区间段名称
let id_range_name_map = {
single_rule: "默认候选项规则",
Expand Down Expand Up @@ -42,6 +43,7 @@ let updateDynamicRules = (
}
);
};

let deleteDynamicRules = (type, id = 0, callback = () => {}, ...args) => {
let del_ids = [];
let id_range = [0, 0];
Expand Down Expand Up @@ -88,19 +90,39 @@ let deleteDynamicRules = (type, id = 0, callback = () => {}, ...args) => {
});
};

let backupAllDynamicRules = () => {
/**
* 备份自定义规则
*/
let backupSelfDefinedDynamicRules = () => {
chrome.declarativeNetRequest.getDynamicRules((rules) => {
if (rules.length > 0) {
let time = new Date().toISOString();
console.log(time);
//time=parseInt(new Date().getTime() / 1000).toString()
let filename =
"ReplaceGoogleCDN-backup-all-dynamic-rule-" + time + ".json";
console.log(filename);
utils.createJSONFile(rules, filename);
"replace-google-cdn-backup-self-defined-dynamic-rule-" + time + ".json";

let need_rules = [];
rules.map((rule, index, array) => {
if (
rule.id >= id_ranges["self_define_rule"][0] &&
rule.id <= id_ranges["self_define_rule"][1]
) {
need_rules.push(rule);
}

if (
rule.id >= id_ranges["self_define_special_rule"][0] &&
rule.id <= id_ranges["self_define_special_rule"][1]
) {
need_rules.push(rule);
}
});
utils.createJSONFile(need_rules, filename);
}
});
};

/**
* 启用本地默认静态规则
*/
Expand Down Expand Up @@ -136,7 +158,7 @@ export {
utils,
updateDynamicRules,
deleteDynamicRules,
backupAllDynamicRules,
backupSelfDefinedDynamicRules,
id_ranges,
id_range_name_map,
rule_action_type_map,
Expand Down

0 comments on commit ade4a2c

Please sign in to comment.