parcel react
by Atakan Goktepe
通过Atakan Goktepe
This month, I started to build my new product. It helps convert any website to JSON, XML, CSV, or any other format. I wrote my own Chrome extension for pointing to the fields in websites.
这个月,我开始制造我的新产品。 它有助于将任何网站转换为JSON,XML,CSV或任何其他格式。 我写了自己的Chrome扩展程序来指向网站中的字段。
Most articles show how you can render it in a popup. In this article, we will render our app directly in the content (with Content Scripts). I will show you to how to start writing a chrome extension using ReactJS and Parcel from scratch. I will assume you have basic knowledge of JavaScript.
大多数文章都展示了如何在弹出窗口中呈现它。 在本文中,我们将直接在内容中呈现我们的应用程序(使用Content Scripts )。 我将向您展示如何从头开始使用ReactJS和Parcel编写chrome扩展。 我将假定您具有JavaScript的基本知识。
Note: If you don’t want to read this article and just want to start coding, you can find the Git repo at the end.
注意:如果您不想阅读本文,而只是想开始编码,则可以在末尾找到Git存储库。
We need to render our app on the visited website when a user clicks the render element button in the popup.
当用户单击弹出窗口中的渲染元素按钮时,我们需要在访问的网站上渲染我们的应用程序。
So, let’s get started.
因此,让我们开始吧。
├── icon.png├── manifest.json├── node_modules├── .babelrc├── package.json└── src ├── build │ └── main.js [We will use that build file as content script] ├── js [Where our development file is stored] │ ├── components [We are storing components in this folder] │ │ └── Header.js │ ├── main.js [Our main file, that renders our app] │ └── popup.js [Our javascript file for popup] └── popup.html [Our html file for rendering popup after clicking the icon in the bar]
Firstly, create manifest.json
, as we need that file for providing information about our Chrome extension.
首先,创建manifest.json
,因为我们需要该文件来提供有关我们的Chrome扩展程序的信息。
Every extension has a JSON-formatted manifest file, named manifest.json
, that provides important information like name, version, and permissions.
每个扩展都有一个JSON格式的清单文件,名为manifest.json
,提供重要信息,例如名称,版本和权限。
And here is how it looks:
这是它的外观:
After creating the manifest file, we need to configure our project environment and structure.
创建清单文件后,我们需要配置项目环境和结构。
Let’s start creating out project structure — so open your terminal and follow my steps!
让我们开始创建项目结构-打开终端并按照我的步骤进行!
As you can see, there are two scripts build
and watch
如您所见,有两个脚本可以build
和watch
watch
command watches your main.js
and compiles if there was a change or if dependencies are imported from main.js
.
watch
命令watch
您的main.js
并进行编译,如果有更改或是否从main.js
导入了依赖main.js
。
build
command builds your files into the src/build/main.js
file.
build
命令将您的文件构建到src/build/main.js
文件中。
mkdir src
Create a components folder in src
folder
在src
文件夹中创建一个components文件夹
mkdir src/js/components
Create main.js
,popup.js
and popup.html
files
创建main.js
, popup.js
和popup.html
文件
touch src/js/main.js src/js/popup.js src/popup.html
The popup appears at the top of the bar in Chrome when a user clicks the icon of the extension.
当用户单击扩展程序的图标时,弹出窗口将显示在Chrome栏中的顶部。
The React App will be rendered in the content when a startApp
message comes from the popup. The popup will share a startApp
message with the app.
当从弹出窗口发出startApp
消息时,React App将在内容中呈现。 弹出窗口将与该应用共享一个startApp
消息。
Edit your popup.html
file with the following HTML:
使用以下HTML编辑您的popup.html
文件:
There is a button in the popup which sends a message to our main.js
file when a user is clicked.
弹出窗口中有一个按钮,当单击用户时,该按钮会将消息发送到我们的main.js
文件。
And edit your popup.js
file with the following:
并使用以下代码编辑popup.js
文件:
chrome.tabs.sendMessage
method shares the message with a listener. Our listener is working in the main.js
.
chrome.tabs.sendMessage
方法与侦听器共享消息。 我们的侦听器正在main.js
工作。
At this point, main.js
should listen to messages which are coming from popup.js
.
此时, main.js
应该侦听来自popup.js
消息。
We can listen to messages with the chrome.runtime.onMessage.addListener
method. And here is how our main.js
file looks:
我们可以使用chrome.runtime.onMessage.addListener
方法收听消息。 这是我们的main.js
文件的外观:
main.js
我们做了main.js
: (Let’s look at what have we done in main.js
:)Added an event listener function for listening for messages which are coming from popup.js
添加了事件侦听器功能,用于侦听来自popup.js
消息
Created an injectApp
function that created a div, and injected it into a body. We rendered our <App
/> component inside it.
创建了一个injectApp
函数,该函数创建了一个div,并将其注入到主体中。 我们在其中渲染了<App
/>组件。
For testing your extension:
为了测试您的扩展程序:
If you didn’t build your files, build them by running the npm run build
command
如果您没有构建文件,请通过运行npm run build
命令来构建它们
And Whoa! You have created an extension for Google Chrome. You are awesome!
哇! 您已经为Google Chrome创建了扩展程序。 你真棒!
Open your developer tools and look at the end of the body — your element rendered successfully!
打开您的开发人员工具,查看正文的末尾-您的元素成功渲染!
Here’s the final repo:
这是最终的仓库:
atakangktepe/react-parcel-extension-boilerplatereact-parcel-extension-boilerplate - A Chrome extension boilerplate built with ReactJS and Parcel (renders in content…github.com
atakangktepe / react-parcel-extension-boilerplate react-parcel-extension-boilerplate-使用ReactJS和Parcel构建的Chrome扩展样板(内容提供… github.com
Thanks for reading through this tutorial. It was my first article in English! If it has been helpful, please recommend it by hitting the clap button ? or (even better) share it. ??
感谢您阅读本教程。 这是我的第一篇英文文章! 如果有帮助,请点击拍手按钮? 或(甚至更好)分享。 ??
Follow me on Twitter and Github!
翻译自: https://www.freecodecamp.org/news/building-chrome-extensions-in-react-parcel-79d0240dd58f/
parcel react