(currentUrl: string, provider: ScriptProvider)
| 73 | type ScriptProvider = "scriptcat" | "greasyfork" | "openuserjs"; |
| 74 | |
| 75 | const getMoreScriptWindowOpen = (currentUrl: string, provider: ScriptProvider) => { |
| 76 | let urlHost = ""; |
| 77 | if (currentUrl) { |
| 78 | try { |
| 79 | const url = new URL(currentUrl); |
| 80 | if (url.hostname && url.protocol.startsWith("http")) { |
| 81 | urlHost = url.hostname; |
| 82 | } |
| 83 | } catch (e: any) { |
| 84 | console.warn(e); // 容错:URL 解析失败时忽略错误(不影响后续 UI) |
| 85 | } |
| 86 | } |
| 87 | let link = ""; |
| 88 | if (provider === "greasyfork") { |
| 89 | // www.google.com -> google.com |
| 90 | urlHost = /[^.]+\.[^.]+$/.exec(urlHost)?.[0] || urlHost; |
| 91 | } |
| 92 | switch (provider) { |
| 93 | case "scriptcat": |
| 94 | link = !urlHost |
| 95 | ? "https://scriptcat.org/search" |
| 96 | : `https://scriptcat.org/search?domain=${encodeURIComponent(urlHost)}`; |
| 97 | break; |
| 98 | case "greasyfork": |
| 99 | link = !urlHost |
| 100 | ? "https://greasyfork.org/scripts/" |
| 101 | : `https://greasyfork.org/scripts/by-site/${encodeURI(urlHost)}`; |
| 102 | break; |
| 103 | case "openuserjs": |
| 104 | link = !urlHost ? "https://openuserjs.org/" : `https://openuserjs.org/?q=${encodeURIComponent(urlHost)}`; |
| 105 | break; |
| 106 | } |
| 107 | window.open(link, "_blank"); |
| 108 | }; |
| 109 | |
| 110 | function App() { |
| 111 | const [loading, setLoading] = useState(true); |
no test coverage detected