Localize documentation and extension messages to Chinese

Converted all major documentation files (README, PRIVACY_POLICY, SECURITY, pull request template) to Chinese. Updated extension source files (background.js, content.js, popup.html, popup.js) to use Chinese comments and user-facing messages for improved localization and user experience.
This commit is contained in:
xixu-me committed 2025-07-12 10:49:56 +08:00
1 parent 40103d8a05
commit a1e258a4b8
10 files changed
+495 -513

No files matched your search

+46 -46
View File
@@ -1,6 +1,6 @@
// Content script to inject download interception on supported platforms
// 内容脚本,在支持的平台上注入下载拦截功能
// Platform detection and URL transformation
// 平台检测和 URL 转换
const PLATFORMS = {
gh: {
base: "https://github.com",
@@ -19,11 +19,11 @@ const PLATFORMS = {
},
};
// Initialize content script
// 初始化内容脚本
(async function () {
console.log("Xget for Chrome: Content script loaded");
console.log("Xget for Chrome:内容脚本已加载");
// Listen for messages from background script
// 监听来自后台脚本的消息
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "showNotification") {
showNotification(request.message, request.showRefreshButton);
@@ -31,22 +31,22 @@ const PLATFORMS = {
}
});
// Check if extension is enabled and configured
// 检查扩展是否已启用并配置
const settings = await getSettings();
if (!settings.enabled || !settings.xgetDomain) {
return;
}
// Find current platform
// 找到当前平台
const currentPlatform = detectPlatform(window.location.href);
if (!currentPlatform || !settings.enabledPlatforms[currentPlatform]) {
return;
}
// Add download interception
// 添加下载拦截
interceptDownloadLinks();
// Monitor for dynamically added content
// 监控动态添加的内容
observePageChanges();
})();
@@ -56,7 +56,7 @@ async function getSettings() {
chrome.runtime.sendMessage({ action: "getSettings" }, resolve);
});
} catch (error) {
console.error("Error getting settings:", error);
console.error("获取设置时出错:", error);
return { enabled: false };
}
}
@@ -71,14 +71,14 @@ function detectPlatform(url) {
}
function interceptDownloadLinks() {
// Intercept clicks on download links
// 拦截下载链接的点击事件
document.addEventListener(
"click",
async (event) => {
const link = event.target.closest("a");
if (!link || !link.href) return;
// Check if this is a download link
// 检查这是否是下载链接
if (isDownloadLink(link)) {
event.preventDefault();
event.stopPropagation();
@@ -95,12 +95,12 @@ function isDownloadLink(link) {
const url = new URL(link.href);
const pathname = url.pathname.toLowerCase();
// First check: explicit download attributes
// 第一检查:明确的下载属性
if (link.download || link.hasAttribute("download")) {
return true;
}
// Second check: file extensions that indicate downloadable files
// 第二检查:表示可下载文件的文件扩展名
const fileExtensions = [
".zip",
".tar.gz",
@@ -152,50 +152,50 @@ function isDownloadLink(link) {
".nupkg",
];
// Check if URL ends with a file extension
// 检查 URL 是否以文件扩展名结尾
if (fileExtensions.some((ext) => pathname.endsWith(ext))) {
return true;
}
// Third check: GitHub-specific patterns
// 第三检查:GitHub 特定模式
const allowedGitHubHosts = ["github.com"];
try {
const parsedUrl = new URL(href);
if (allowedGitHubHosts.includes(parsedUrl.host)) {
// GitHub release asset download URLs follow pattern: /releases/download/
// GitHub 发布资源下载 URL 遵循模式:/releases/download/
if (pathname.includes("/releases/download/")) {
return true;
}
// GitHub archive download URLs
// GitHub 存档下载 URL
if (
pathname.includes("/archive/") &&
(pathname.endsWith(".zip") || pathname.endsWith(".tar.gz"))
) {
return true;
}
// GitHub raw file URLs - NEW: support for raw file links
// GitHub 原始文件 URL - 新增:支持原始文件链接
if (pathname.includes("/raw/")) {
return true;
}
// Exclude navigation to releases page (just /releases or /releases/)
// 排除导航到发布页面(仅 /releases 或 /releases/)
if (pathname.endsWith("/releases") || pathname.endsWith("/releases/")) {
return false;
}
}
} catch (e) {
console.error("Invalid URL:", href, e);
console.error("无效的 URL:", href, e);
}
// Fourth check: GitLab-specific patterns
// 第四检查:GitLab 特定模式
const allowedGitLabHosts = ["gitlab.com"];
try {
const parsedUrl = new URL(href);
if (allowedGitLabHosts.includes(parsedUrl.host)) {
// GitLab archive downloads
// GitLab 存档下载
if (pathname.includes("/-/archive/")) {
return true;
}
// GitLab release downloads
// GitLab 发布下载
if (
pathname.includes("/-/releases/") &&
pathname.includes("/downloads/")
@@ -204,24 +204,24 @@ function isDownloadLink(link) {
}
}
} catch (e) {
console.error("Invalid URL:", href, e);
console.error("无效的 URL:", href, e);
}
// Fifth check: Hugging Face file downloads
// 第五检查:Hugging Face 文件下载
const allowedHuggingFaceHosts = ["huggingface.co"];
try {
const parsedUrl = new URL(href);
if (allowedHuggingFaceHosts.includes(parsedUrl.host)) {
// HF file download URLs contain /resolve/
// HF 文件下载 URL 包含 /resolve/
if (pathname.includes("/resolve/")) {
return true;
}
}
} catch (e) {
console.error("Invalid URL:", href, e);
console.error("无效的 URL:", href, e);
}
// Seventh check: explicit download text indicators (be more specific)
// 第七检查:明确的下载文本指示器(更具体)
const downloadTextIndicators = ["download", "download file", "get file"];
const linkText = link.textContent.toLowerCase().trim();
if (
@@ -233,7 +233,7 @@ function isDownloadLink(link) {
return true;
}
// Default: not a download link
// 默认:不是下载链接
return false;
}
@@ -244,14 +244,14 @@ async function handleDownloadLink(url) {
const transformedUrl = transformUrl(url, settings);
if (transformedUrl) {
// Show notification
showNotification(`Download redirected through Xget`);
// 显示通知
showNotification(`下载已通过 Xget 重定向`);
// Trigger download
// 触发下载
window.location.href = transformedUrl;
}
} catch (error) {
console.error("Error handling download:", error);
console.error("处理下载时出错:", error);
}
}
@@ -267,14 +267,14 @@ function transformUrl(url, settings) {
return `https://${settings.xgetDomain}/${platform}${path}`;
} catch (error) {
console.error("Error transforming URL:", error);
console.error("转换 URL 时出错:", error);
return null;
}
}
function observePageChanges() {
const observer = new MutationObserver(() => {
// Monitor for page changes (no additional actions needed)
// 监控页面变化(无需额外操作)
});
observer.observe(document.body, {
@@ -285,25 +285,25 @@ function observePageChanges() {
function showNotification(message, showRefreshButton = false) {
try {
// Remove any existing notifications first
// 首先删除所有现有通知
const existingNotifications =
document.querySelectorAll(".xget-notification");
existingNotifications.forEach((notification) => notification.remove());
// Create a simple notification
// 创建一个简单的通知
const notification = document.createElement("div");
notification.className = "xget-notification";
// Create message container
// 创建消息容器
const messageDiv = document.createElement("div");
messageDiv.textContent = message;
messageDiv.style.marginBottom = showRefreshButton ? "8px" : "0";
notification.appendChild(messageDiv);
// Add refresh button if needed
// 如果需要,添加刷新按钮
if (showRefreshButton) {
const refreshButton = document.createElement("button");
refreshButton.textContent = "🔄 Refresh Page";
refreshButton.textContent = "🔄 刷新页面";
refreshButton.style.cssText = `
background: rgba(255, 255, 255, 0.2);
border: 1px solid rgba(255, 255, 255, 0.3);
@@ -350,7 +350,7 @@ function showNotification(message, showRefreshButton = false) {
word-wrap: break-word;
`;
// Add animation styles if not already present
// 如果尚未存在则添加动画样式
if (!document.getElementById("xget-notification-styles")) {
const style = document.createElement("style");
style.id = "xget-notification-styles";
@@ -369,7 +369,7 @@ function showNotification(message, showRefreshButton = false) {
document.body.appendChild(notification);
// Remove after longer time if has refresh button, shorter if not
// 如果有刷新按钮则在较长时间后删除,否则在较短时间后删除
const removeDelay = showRefreshButton ? 8000 : 4000;
setTimeout(() => {
notification.style.animation = "xgetSlideOut 0.3s ease-in";
@@ -380,8 +380,8 @@ function showNotification(message, showRefreshButton = false) {
}, 300);
}, removeDelay);
} catch (error) {
console.error("Error showing notification:", error);
// Fallback to console log if DOM manipulation fails
console.log("Xget notification:", message);
console.error("显示通知时出错:", error);
// 如果 DOM 操作失败则回退到控制台日志
console.log("Xget 通知:", message);
}
}