一、准备开发环境
- 创建项目文件夹
github-enhancer
- 安装VS Code或其他编辑器
- 确保使用最新版Edge浏览器(需支持Manifest V3)
二、创建基础文件结构
github-enhancer/
├── manifest.json
├── content.css
├── content.js
└── icons/
├── icon48.png
└── icon128.png
三、编写核心文件(代码示例)
1. manifest.json 配置
{
"manifest_version": 3,
"name": "GitHub Enhancer",
"version": "1.0",
"description": "优化GitHub代码显示与界面布局",
"icons": {
"48": "icons/icon48.png",
"128": "icons/icon128.png"
},
"content_scripts": [{
"matches": ["*://github.com/*"],
"css": ["content.css"],
"js": ["content.js"]
}],
"permissions": ["storage"],
"host_permissions": ["*://github.com/*"]
}
2. content.css 样式优化
.blob-code-inner {
font-family: "Fira Code", monospace !important;
font-size: 14px !important;
line-height: 1.8 !important;
}
[data-color-mode*="dark"] {
--color-canvas-default: #1a1a1a !important;
--color-border-default: #404040 !important;
}
.react-directory-truncate {
max-width: 300px !important;
}
3. content.js 交互逻辑
document.addEventListener('DOMContentLoaded', () => {
document.querySelectorAll('.js-file-header').forEach(header => {
if(header.querySelector('.diffstat')) {
header.closest('.file').querySelector('.js-details-target').click();
}
});
document.querySelectorAll('.blob-code').forEach(line => {
line.style.cursor = 'pointer';
line.onclick = () => {
navigator.clipboard.writeText(line.innerText);
line.style.background = 'rgba(100, 255, 100, 0.1)';
setTimeout(() => line.style.background = '', 1000);
}
});
});
四、测试与调试步骤
- 打开Edge浏览器访问
edge://extensions
- 启用 开发者模式 开关
- 点击 加载解压缩的扩展 选择项目文件夹
- 访问任意GitHub仓库(如
https://github.com/microsoft/vscode)
五、高级功能扩展建议
- 在manifest中添加
options_page 实现用户配置界面
- 使用Storage API保存用户主题偏好
- 通过MutationObserver监听动态加载的内容
- 添加快捷键支持(需注册commands)
const observer = new MutationObserver(mutations => {
mutations.forEach(mut => {
if(mut.addedNodes.length) {
applyEnhancements(mut.target);
}
});
});
observer.observe(document.body, {
childList: true,
subtree: true
});
六、打包与发布
- 在Edge开发者中心注册账号
- 通过
npm install -g @microsoft/edge-dev-tools-cli 安装打包工具
- 执行
edge-extensions pack 生成.crx文件
- 提交至Microsoft Edge Add-ons商店