news 2026/5/24 1:43:06

浏览器扩展开发:打造个性化浏览体验

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
浏览器扩展开发:打造个性化浏览体验

浏览器扩展开发:打造个性化浏览体验

什么是浏览器扩展?

浏览器扩展是一种可以增强浏览器功能的小型软件程序。

扩展类型

类型说明
扩展程序完整功能的扩展
主题自定义浏览器外观
插件NPAPI 插件(已废弃)

扩展结构

my-extension/ ├── manifest.json ├── background.js ├── popup.html ├── popup.js ├── content.js └── icons/ ├── icon16.png ├── icon48.png └── icon128.png

Manifest 文件

{ "manifest_version": 3, "name": "My Extension", "version": "1.0.0", "description": "A simple browser extension", "permissions": ["activeTab", "storage"], "action": { "default_popup": "popup.html", "default_icon": { "16": "icons/icon16.png", "48": "icons/icon48.png", "128": "icons/icon128.png" } }, "background": { "service_worker": "background.js" }, "content_scripts": [ { "matches": ["<all_urls>"], "js": ["content.js"] } ] }

Popup 页面

<!DOCTYPE html> <html> <head> <style> body { width: 200px; padding: 10px; } button { width: 100%; padding: 8px; background: #42b983; color: white; border: none; border-radius: 4px; } </style> </head> <body> <h3>My Extension</h3> <button id="btn">Click Me</button> <script src="popup.js"></script> </body> </html>
// popup.js document.getElementById('btn').addEventListener('click', () => { chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { chrome.scripting.executeScript({ target: { tabId: tabs[0].id }, function: () => { alert('Hello from extension!'); } }); }); });

Background Service Worker

// background.js chrome.runtime.onInstalled.addListener(() => { console.log('Extension installed'); }); chrome.action.onClicked.addListener((tab) => { console.log('Extension clicked'); }); chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => { if (changeInfo.status === 'complete') { console.log('Tab loaded:', tab.url); } });

Content Script

// content.js console.log('Running on:', window.location.href); // 修改页面内容 const style = document.createElement('style'); style.textContent = ` body { background-color: #f0f0f0 !important; } `; document.head.appendChild(style);

存储 API

// 保存数据 chrome.storage.local.set({ username: 'John', preferences: { darkMode: true } }, () => { console.log('Data saved'); }); // 读取数据 chrome.storage.local.get(['username', 'preferences'], (result) => { console.log(result.username); console.log(result.preferences); });

消息传递

// popup.js -> background.js chrome.runtime.sendMessage({ type: 'GET_DATA' }, (response) => { console.log(response); }); // background.js chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { if (request.type === 'GET_DATA') { sendResponse({ data: 'Hello from background' }); } });

权限说明

{ "permissions": [ "activeTab", "storage", "tabs", "scripting", "<all_urls>" ] }

打包与发布

# 开发模式加载 # 浏览器 -> 扩展程序 -> 加载已解压的扩展程序 # 打包 # 浏览器 -> 扩展程序 -> 打包扩展程序

实战案例

案例:页面翻译工具

// content.js function translateText() { const paragraphs = document.querySelectorAll('p'); paragraphs.forEach(p => { const text = p.textContent; // 调用翻译 API translate(text).then(result => { p.textContent = result; }); }); } chrome.runtime.onMessage.addListener((request) => { if (request.action === 'translate') { translateText(); } });

总结

浏览器扩展开发是前端开发的一个有趣方向:

  1. 快速上手:HTML/CSS/JavaScript 即可开发
  2. 丰富 API:访问浏览器核心功能
  3. 个性化:打造独特的浏览体验
  4. 分发渠道:Chrome Web Store、Firefox Add-ons

开始你的第一个扩展开发之旅吧!

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/24 1:35:10

2026年热门AI编程助手全面评测

2026年热门AI编程助手全面评测在2026年开发者实测数据当中&#xff0c;Trae依托98%的代码生成准确率与亲民定价模式&#xff0c;成为当下使用率稳步走高的主流AI编程助手。如今各类智能化编程辅助工具层出不穷&#xff0c;不同产品在功能形态、适配场景、使用成本上差距明显&am…

作者头像 李华
网站建设 2026/5/24 1:34:26

Docker部署YOLOv8训练+推理完整教程(含报错解决)

说明 1、本教程适配 Windows10/Ubuntu20.04/22.04&#xff0c;全程一键复制命令&#xff0c;无需配置Python、CUDA、PyTorch 2、使用Ultralytics官方GPU镜像&#xff0c;环境纯净、无版本冲突 3、包含&#xff1a;环境安装、容器部署、数据集制作、训练、推理、模型导出、全…

作者头像 李华
网站建设 2026/5/24 1:33:11

从需求到交付:深度拆解企业级软件定制开发的标准化流程

一、 引言&#xff1a;数字化转型的“标准化”与“定制化”博弈&#xff08;内容概要&#xff1a;简述当前企业在选购通用SaaS软件与定制软件时的痛点。指出通用软件往往“大而全但难用”&#xff0c;而定制开发的核心在于精准契合业务场景。&#xff09;二、 定制开发的四大核…

作者头像 李华
网站建设 2026/5/24 1:33:06

数据标注中的权力博弈与主观性:从规则制定到模型偏见的全链路解析

1. 项目概述&#xff1a;当数据标注不再是“客观”的技术活“数据标注”&#xff0c;在很多人眼里&#xff0c;可能就是一个坐在电脑前&#xff0c;对着图片画框、打标签的“体力活”或“技术活”。它听起来中立、客观&#xff0c;是人工智能模型训练前一道标准化的工序。然而&…

作者头像 李华
网站建设 2026/5/24 1:31:03

Unity Android导出构建失败:BuildIl2CppTask错误根因与修复

1. 这不是Unity的错&#xff0c;是Il2Cpp编译链路在Windows上和Android Studio“互相不认识” 你刚在Unity里点下“Build & Run”&#xff0c;选中“Export Project”导出安卓工程&#xff0c;然后兴冲冲打开Android Studio——结果Gradle同步失败&#xff0c;控制台里一长…

作者头像 李华