Skip to content

Commit

Permalink
feat: custom remoteRulesUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Dec 16, 2023
1 parent 2617521 commit 82ae764
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 11 deletions.
6 changes: 6 additions & 0 deletions assets/locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@
"rules": {
"message": "Radar Rules"
},
"manuallyUpdate": {
"message": "Manually update radar rules"
},
"rulesUpdate": {
"message": "Rules update"
},
Expand Down Expand Up @@ -136,5 +139,8 @@
},
"clickToViewChangeLog": {
"message": "click to view the change log."
},
"remoteRulesUrl": {
"message": "Remote radar rules URL"
}
}
6 changes: 6 additions & 0 deletions assets/locales/zh_CN/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@
"rules": {
"message": "Radar 规则"
},
"manuallyUpdate": {
"message": "手动更新 Radar 规则"
},
"rulesUpdate": {
"message": "规则更新"
},
Expand Down Expand Up @@ -136,5 +139,8 @@
},
"clickToViewChangeLog": {
"message": "点击查看更新日志。"
},
"remoteRulesUrl": {
"message": "远程 Radar Rules 地址"
}
}
12 changes: 6 additions & 6 deletions src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ export const defaultConfig = {
// Safari: Mozilla/5.0 (Macintosh; Intel Mac OS X 12_3_1) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.3 Safari/605.1.15
// Edge: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 Edg/99.0.1150.36
enableFullRemoteRules: !(navigator.userAgent.match(/firefox/i) || (navigator.userAgent.match(/safari/i) && !navigator.userAgent.match(/chrome/i))),
remoteRulesUrl: 'https://rsshub.js.org/build/radar-rules.js',
};

export async function getConfig() {
return _.merge({}, defaultConfig, await storage.get("config")) as typeof defaultConfig;
}

let toastId: string | undefined;
export async function setConfig(config: Partial<typeof defaultConfig>) {
config = _.merge({}, await getConfig(), config);
if (!config.rsshubDomain) {
config.rsshubDomain = defaultConfig.rsshubDomain;
}
config.rsshubDomain = config.rsshubDomain.replace(/\/$/, '');
config = _.merge({}, await storage.get("config"), config);
await storage.set("config", config)
toast.success("Saved")
toastId = toast.success("Saved", {
id: toastId,
})
return config;
}
5 changes: 3 additions & 2 deletions src/lib/rules.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import _ from 'lodash';
import { defaultRules } from './radar-rules';
import { defaultConfig } from './config';
import { defaultConfig, getConfig } from './config';
import type { Rules } from './types';

export function parseRules(rules: string, forceJSON?: boolean) {
Expand Down Expand Up @@ -43,8 +43,9 @@ export function getRulesCount(rules: Rules) {

export function getRemoteRules() {
return new Promise(async (resolve, reject) => {
const config = await getConfig()
try {
const res = await fetch("https://rsshub.js.org/build/radar-rules.js")
const res = await fetch(config.remoteRulesUrl)
resolve(res.text())
} catch (error) {
reject(error)
Expand Down
11 changes: 9 additions & 2 deletions src/options/routes/General.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import { Loader2 } from "lucide-react"
import toast from "react-hot-toast"

function General() {
const [config] = useStorage("config", (v) => _.merge({}, defaultConfig, v))
let [config] = useStorage("config")
config = _.merge({}, defaultConfig, config)
const [rules, setRules] = useState<IRules>({})
useEffect(() => {
sendToBackground({
Expand Down Expand Up @@ -92,7 +93,13 @@ function General() {
/>
</div>
<div className="grid w-full items-center gap-2">
<Label>{chrome.i18n.getMessage("rules")}</Label>
<Label htmlFor="remoteRulesUrl">{chrome.i18n.getMessage("remoteRulesUrl")}</Label>
<Input id="remoteRulesUrl" value={config.remoteRulesUrl} onChange={(e) => setConfig({
remoteRulesUrl: e.target.value
})} />
</div>
<div className="grid w-full items-center gap-2">
<Label>{chrome.i18n.getMessage("manuallyUpdate")}</Label>
<p className="text-zinc-500 text-sm">{chrome.i18n.getMessage("totalNumberOfRules")}: {count}</p>
<p className="text-zinc-500 text-sm">{chrome.i18n.getMessage("updateTip")}</p>
<Button
Expand Down
2 changes: 1 addition & 1 deletion src/popup/RSSItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function RSSItem({
}
}, [copied])

let url = item.url.replace('{rsshubDomain}', config.rsshubDomain);
let url = item.url.replace('{rsshubDomain}', config.rsshubDomain.replace(/\/$/, ''));
if (type === 'currentPageRSSHub' && config.rsshubAccessControl.accessKey) {
url = `${url}?code=${new MD5().update(item.path + config.rsshubAccessControl.accessKey).digest('hex')}`
}
Expand Down

0 comments on commit 82ae764

Please sign in to comment.