在本博客中,我们将探索如何使用 TypeScript、React、Tailwind CSS 和 Webpack 设置和开发 Chrome 扩展。我们将创建一个名为“NoteMe”的最小扩展✍️来测试我们的理解。我们的扩展将包括以下功能:
在本博客中,我们将学习如何使用现代技术构建 Chrome 扩展程序。本指南假设您已经对在本地开发期间构建和上传 Chrome 扩展程序有一定的了解。如果您对此不熟悉或需要详细的基础知识演练,我建议您查看我之前的博客:链接
该扩展将包括以下组件:
下面的屏幕截图展示了扩展完成后的外观:
在深入学习本教程之前,请确保您的系统上安装了以下工具:
上图提供了此扩展的内部工作原理的高级概述。以下是我们可以从图中得出的一些关键点:
虽然 Chrome 扩展项目不强制要求特定的项目结构,但它们确实需要一个位于构建目录根目录的 manifest.json 文件。利用这种灵活性,我们将定义一个自定义项目结构,帮助有效地组织不同的脚本。这种结构将能够更好地跨脚本重用代码并最大程度地减少重复,从而简化我们的开发流程。
首先,我们将为该项目设置一个基础目录结构。您可以使用以下 bash 脚本来创建基本结构以及 manifest.json 文件:
#!/bin/bash bash_script_absolute_path=$(pwd) declare public_paths=("public" "public/assets" "public/assets/images") declare source_paths=("src" "src/lib" "src/scripts" "src/scripts/background" "src/scripts/content" "src/scripts/injected" "src/scripts/popup" "src/styles") declare public_directory_path="public" declare manifest_file="manifest.json" declare project_name="note-me" create_directory () { if [ ! -d "" ]; then mkdir fi } create_file () { if [ ! -e "/" ]; then touch / fi } create_public_directories () { for public_path in "${public_paths[@]}"; do create_directory $public_path done } create_source_directories () { for source_path in "${source_paths[@]}"; do create_directory $source_path done } execute () { echo "creating project struture at "${bash_script_absolute_path} create_directory $project_name cd $bash_script_absolute_path"/"$project_name create_public_directories create_source_directories create_file $manifest_file $public_directory_path echo "done creating project struture at "${bash_script_absolute_path}" with project name "$project_name } execute
确保您的目录结构类似于下面的屏幕截图所示。
{ "manifest_version": 3, "name": "NoteMe", "version": "1.0", "description": "A Chrome extension built with React and TypeScript using Webpack.", "action": { "default_popup": "popup.html", "default_icon": "app-icon.png" }, "background": { "service_worker": "background.js", "type": "module" }, "content_scripts": [ { "matches": ["<all_urls>"], "js": ["content.js"], "run_at": "document_end" } ], "permissions": [ "storage", "activeTab", "scripting", "webNavigation" ], "host_permissions": ["<all_urls>"], "web_accessible_resources": [ { "resources": ["styles.css", "sidebar-open.png", "sidebar-close.png"], "matches": ["<all_urls>"] } ] }
注意事项:
创建在manifest.json中引用的以下文件:backgroun.ts、content.ts和popup.html。
在public目录下的popup.html文件中添加以下代码:
#!/bin/bash bash_script_absolute_path=$(pwd) declare public_paths=("public" "public/assets" "public/assets/images") declare source_paths=("src" "src/lib" "src/scripts" "src/scripts/background" "src/scripts/content" "src/scripts/injected" "src/scripts/popup" "src/styles") declare public_directory_path="public" declare manifest_file="manifest.json" declare project_name="note-me" create_directory () { if [ ! -d "" ]; then mkdir fi } create_file () { if [ ! -e "/" ]; then touch / fi } create_public_directories () { for public_path in "${public_paths[@]}"; do create_directory $public_path done } create_source_directories () { for source_path in "${source_paths[@]}"; do create_directory $source_path done } execute () { echo "creating project struture at "${bash_script_absolute_path} create_directory $project_name cd $bash_script_absolute_path"/"$project_name create_public_directories create_source_directories create_file $manifest_file $public_directory_path echo "done creating project struture at "${bash_script_absolute_path}" with project name "$project_name } execute
注意:
上面的代码安装了两个监听器:
此外,import 语句从 src/lib 目录引入监听器。核心应用程序逻辑构建在 src/lib 中,可以在不同上下文(例如内容和后台脚本)之间重用。
src/lib 目录包含扩展的核心逻辑。以下是其结构和关键组件的概述:
详细实现请参考仓库中的实际代码。
在这一步中,我们挂载React组件进行渲染。这些组件安装在两个不同的脚本中:src/scripts/content/content.ts 和 src/scripts/popup/popup.ts。
弹出脚本:位于 src/scripts/popup/popup.ts。
#!/bin/bash bash_script_absolute_path=$(pwd) declare public_paths=("public" "public/assets" "public/assets/images") declare source_paths=("src" "src/lib" "src/scripts" "src/scripts/background" "src/scripts/content" "src/scripts/injected" "src/scripts/popup" "src/styles") declare public_directory_path="public" declare manifest_file="manifest.json" declare project_name="note-me" create_directory () { if [ ! -d "" ]; then mkdir fi } create_file () { if [ ! -e "/" ]; then touch / fi } create_public_directories () { for public_path in "${public_paths[@]}"; do create_directory $public_path done } create_source_directories () { for source_path in "${source_paths[@]}"; do create_directory $source_path done } execute () { echo "creating project struture at "${bash_script_absolute_path} create_directory $project_name cd $bash_script_absolute_path"/"$project_name create_public_directories create_source_directories create_file $manifest_file $public_directory_path echo "done creating project struture at "${bash_script_absolute_path}" with project name "$project_name } execute
内容脚本:位于 src/scripts/content/content.ts。
{ "manifest_version": 3, "name": "NoteMe", "version": "1.0", "description": "A Chrome extension built with React and TypeScript using Webpack.", "action": { "default_popup": "popup.html", "default_icon": "app-icon.png" }, "background": { "service_worker": "background.js", "type": "module" }, "content_scripts": [ { "matches": ["<all_urls>"], "js": ["content.js"], "run_at": "document_end" } ], "permissions": [ "storage", "activeTab", "scripting", "webNavigation" ], "host_permissions": ["<all_urls>"], "web_accessible_resources": [ { "resources": ["styles.css", "sidebar-open.png", "sidebar-close.png"], "matches": ["<all_urls>"] } ] }
添加编译和构建扩展所需的配置
要成功编译和构建扩展,我们需要配置以下文件:
这些配置处理 TypeScript 编译、Tailwind CSS 集成以及扩展的整体 Webpack 构建过程。
以下是扩展测试期间捕获的一些屏幕截图。
以下是此博客的一些关键要点,
将来,我计划撰写另一个博客,我们将探索将功能齐全的 Chrome 扩展程序发布到 Chrome 网上应用店的过程。该博客的目标是:
感谢您花时间阅读此博客!您的兴趣和支持对我来说意义重大。在继续这一旅程时,我很高兴能分享更多见解。
编码愉快!
github 链接:https://github.com/gauravnadkarni/chrome-extension-starter-app
本文最初发表于Medium。
以上是Chrome 扩展开发 - 使用 TypeScript、React、Tailwind CSS 和 Webpack 开发最小应用程序的详细内容。更多信息请关注PHP中文网其他相关文章!