mirror of
https://github.com/AIsouler/GKD_subscription.git
synced 2025-12-21 01:04:53 +08:00
feat: 全局规则自动禁用
This commit is contained in:
@@ -1,33 +1,7 @@
|
|||||||
import path from 'node:path';
|
|
||||||
import url from 'node:url';
|
|
||||||
import picocolors from 'picocolors';
|
|
||||||
import { walk } from './file';
|
|
||||||
import type { RawApp, RawSubscription } from './types';
|
|
||||||
import _ from 'lodash';
|
|
||||||
import { pinyin } from 'pinyin-pro';
|
|
||||||
import globalGroups from './globalGroups';
|
|
||||||
import categories from './categories';
|
import categories from './categories';
|
||||||
|
import globalGroups from './globalGroups';
|
||||||
const apps: RawApp[] = [];
|
import apps from './rawApps';
|
||||||
for await (const tsFp of walk(process.cwd() + '/src/apps')) {
|
import type { RawSubscription } from './types';
|
||||||
const mod: { default: RawApp } = await import(url.pathToFileURL(tsFp).href);
|
|
||||||
const appConfig = mod.default;
|
|
||||||
if (path.basename(tsFp, '.ts') != appConfig.id) {
|
|
||||||
throw new Error(
|
|
||||||
`${picocolors.blue(
|
|
||||||
tsFp,
|
|
||||||
)} file basename is not equal to its app id ${picocolors.blue(
|
|
||||||
appConfig.id,
|
|
||||||
)} `,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
appConfig.groups?.forEach((g) => {
|
|
||||||
if (!g.name.startsWith('开屏广告')) {
|
|
||||||
g.enable = false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
apps.push(appConfig);
|
|
||||||
}
|
|
||||||
|
|
||||||
const subsConfig: RawSubscription = {
|
const subsConfig: RawSubscription = {
|
||||||
id: 0,
|
id: 0,
|
||||||
@@ -39,15 +13,7 @@ const subsConfig: RawSubscription = {
|
|||||||
'https://registry.npmmirror.com/@gkd-kit/subscription/latest/files/dist/gkd.version.json',
|
'https://registry.npmmirror.com/@gkd-kit/subscription/latest/files/dist/gkd.version.json',
|
||||||
globalGroups,
|
globalGroups,
|
||||||
categories,
|
categories,
|
||||||
apps: _.sortBy(apps, (a) => {
|
apps,
|
||||||
const showName = a.name || a.id;
|
|
||||||
const pyName = pinyin(showName, {
|
|
||||||
separator: '',
|
|
||||||
toneType: 'none',
|
|
||||||
});
|
|
||||||
if (pyName === showName) return showName;
|
|
||||||
return '\uFFFF' + pyName; // 让带拼音的全排在后面
|
|
||||||
}),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default subsConfig;
|
export default subsConfig;
|
||||||
|
|||||||
37
src/file.ts
37
src/file.ts
@@ -135,8 +135,31 @@ export const validSnapshotUrl = (s: string) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const checkConfig = (newConfig: RawSubscription) => {
|
export const checkConfig = (newConfig: RawSubscription) => {
|
||||||
|
const globalGroups = newConfig.globalGroups || [];
|
||||||
|
globalGroups.forEach((g) => {
|
||||||
|
// check rules selector syntax
|
||||||
|
g.rules.forEach((r) => {
|
||||||
|
[r.matches, r.excludeMatches]
|
||||||
|
.map((m) => iArrayToArray(m))
|
||||||
|
.flat()
|
||||||
|
.forEach((selector) => {
|
||||||
|
try {
|
||||||
|
parseSelector(selector);
|
||||||
|
} catch (e) {
|
||||||
|
console.error({
|
||||||
|
message: 'invalid selector syntax',
|
||||||
|
groupKey: g.key,
|
||||||
|
selector,
|
||||||
|
});
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
// check duplicated group key
|
// check duplicated group key
|
||||||
newConfig.apps?.forEach((app) => {
|
const apps = newConfig.apps || [];
|
||||||
|
apps.forEach((app) => {
|
||||||
const deprecatedKeys = app.deprecatedKeys || [];
|
const deprecatedKeys = app.deprecatedKeys || [];
|
||||||
const keys = new Set<number>();
|
const keys = new Set<number>();
|
||||||
app.groups?.forEach((g) => {
|
app.groups?.forEach((g) => {
|
||||||
@@ -177,13 +200,8 @@ export const checkConfig = (newConfig: RawSubscription) => {
|
|||||||
ruleKeys.add(r.key);
|
ruleKeys.add(r.key);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// check slector syntax
|
// check rules selector syntax
|
||||||
newConfig.apps?.forEach((app) => {
|
|
||||||
app.groups?.forEach((g) => {
|
|
||||||
if (!g.rules) return;
|
|
||||||
const rules = iArrayToArray(g.rules).map((r) => {
|
const rules = iArrayToArray(g.rules).map((r) => {
|
||||||
if (typeof r == 'string') {
|
if (typeof r == 'string') {
|
||||||
return { matches: r };
|
return { matches: r };
|
||||||
@@ -208,12 +226,8 @@ export const checkConfig = (newConfig: RawSubscription) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// check snapshotUrls
|
// check snapshotUrls
|
||||||
newConfig.apps?.forEach((app) => {
|
|
||||||
app.groups?.forEach((g) => {
|
|
||||||
iArrayToArray(g.snapshotUrls).forEach((u) => {
|
iArrayToArray(g.snapshotUrls).forEach((u) => {
|
||||||
if (!validSnapshotUrl(u)) {
|
if (!validSnapshotUrl(u)) {
|
||||||
console.error({
|
console.error({
|
||||||
@@ -243,6 +257,7 @@ export const checkConfig = (newConfig: RawSubscription) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
const newKeys = Object.keys(newConfig) as (keyof RawSubscription)[];
|
const newKeys = Object.keys(newConfig) as (keyof RawSubscription)[];
|
||||||
if (newKeys.some((s) => !sortKeys.includes(s))) {
|
if (newKeys.some((s) => !sortKeys.includes(s))) {
|
||||||
console.log({
|
console.log({
|
||||||
|
|||||||
@@ -1,5 +1,22 @@
|
|||||||
|
import apps from './rawApps';
|
||||||
import type { RawGlobalGroup } from './types';
|
import type { RawGlobalGroup } from './types';
|
||||||
|
|
||||||
|
const diabledAppIds = [
|
||||||
|
'com.android.systemui',
|
||||||
|
'com.miui.aod',
|
||||||
|
'com.miui.home',
|
||||||
|
'com.android.launcher.Launcher',
|
||||||
|
'com.bbk.launcher2.Launcher',
|
||||||
|
'com.huawei.android.launcher.unihome.UniHomeLauncher',
|
||||||
|
];
|
||||||
|
|
||||||
|
// 如果应用规则已有开屏广告一类的规则, 则在全局规则禁用此应用
|
||||||
|
diabledAppIds.push(
|
||||||
|
...apps
|
||||||
|
.filter((a) => a.groups.some((g) => g.name.startsWith('开屏广告')))
|
||||||
|
.map((a) => a.id),
|
||||||
|
);
|
||||||
|
|
||||||
const globalGroups: RawGlobalGroup[] = [
|
const globalGroups: RawGlobalGroup[] = [
|
||||||
{
|
{
|
||||||
key: 0,
|
key: 0,
|
||||||
@@ -23,20 +40,7 @@ const globalGroups: RawGlobalGroup[] = [
|
|||||||
action: 'clickCenter',
|
action: 'clickCenter',
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
apps: [
|
apps: diabledAppIds.map((id) => ({ id, enable: false })),
|
||||||
{
|
|
||||||
id: 'com.android.systemui',
|
|
||||||
enable: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 'com.miui.aod',
|
|
||||||
enable: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 'com.miui.home',
|
|
||||||
enable: false,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
export default globalGroups;
|
export default globalGroups;
|
||||||
|
|||||||
39
src/rawApps.ts
Normal file
39
src/rawApps.ts
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
import _ from 'lodash';
|
||||||
|
import path from 'node:path';
|
||||||
|
import url from 'node:url';
|
||||||
|
import picocolors from 'picocolors';
|
||||||
|
import { pinyin } from 'pinyin-pro';
|
||||||
|
import { walk } from './file';
|
||||||
|
import type { RawApp } from './types';
|
||||||
|
|
||||||
|
const rawApps: RawApp[] = [];
|
||||||
|
for await (const tsFp of walk(process.cwd() + '/src/apps')) {
|
||||||
|
const mod: { default: RawApp } = await import(url.pathToFileURL(tsFp).href);
|
||||||
|
const appConfig = mod.default;
|
||||||
|
if (path.basename(tsFp, '.ts') != appConfig.id) {
|
||||||
|
throw new Error(
|
||||||
|
`${picocolors.blue(
|
||||||
|
tsFp,
|
||||||
|
)} file basename is not equal to its app id ${picocolors.blue(
|
||||||
|
appConfig.id,
|
||||||
|
)} `,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
appConfig.groups?.forEach((g) => {
|
||||||
|
if (!g.name.startsWith('开屏广告')) {
|
||||||
|
g.enable = false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
rawApps.push(appConfig);
|
||||||
|
}
|
||||||
|
|
||||||
|
const apps = _.sortBy(rawApps, (a) => {
|
||||||
|
const showName = a.name || a.id;
|
||||||
|
const pyName = pinyin(showName, {
|
||||||
|
separator: '',
|
||||||
|
toneType: 'none',
|
||||||
|
});
|
||||||
|
if (pyName === showName) return showName;
|
||||||
|
return '\uFFFF' + pyName; // 让带拼音的全排在后面
|
||||||
|
});
|
||||||
|
export default apps;
|
||||||
Reference in New Issue
Block a user