<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Lucas da Silveira</title>
    <description>The latest articles on Forem by Lucas da Silveira (@lucaxsilveira).</description>
    <link>https://forem.com/lucaxsilveira</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F178488%2F574c2ab0-cc79-4aa6-8a73-affa57492588.jpeg</url>
      <title>Forem: Lucas da Silveira</title>
      <link>https://forem.com/lucaxsilveira</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/lucaxsilveira"/>
    <language>en</language>
    <item>
      <title>Creating extensions for Google Chrome with React and Typescript</title>
      <dc:creator>Lucas da Silveira</dc:creator>
      <pubDate>Tue, 02 Jan 2024 17:59:00 +0000</pubDate>
      <link>https://forem.com/lucaxsilveira/creating-extensions-for-google-chrome-with-react-and-typescript-32co</link>
      <guid>https://forem.com/lucaxsilveira/creating-extensions-for-google-chrome-with-react-and-typescript-32co</guid>
      <description>&lt;p&gt;Recently browsing the internet I came across the subject of &lt;strong&gt;creating extensions for Google Chrome&lt;/strong&gt;, I became interested so I decided to study a little about it.&lt;/p&gt;

&lt;p&gt;And what is the best way to study in this tech field? &lt;strong&gt;Getting your hands dirty.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I started reading some articles, posts on stackoverflow and so on. But I came across countless ways to do the same thing, and many of them were &lt;strong&gt;super complicated.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;But does it really need to be so complex?&lt;/p&gt;

&lt;p&gt;Why not use tools that make this work easier? That's when I came across a post explaining the use of this solution with React + Typescript + Webpack.&lt;/p&gt;

&lt;p&gt;Well, it seems simple, but not so much lol. That's why I decided to create this brief content as a way of learning and also to make available some things that I didn't find out there but rather get my hands dirty.&lt;/p&gt;

&lt;p&gt;I also made this content available on Github if you want to use it as a boilerplate or even evolve it for the community.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/lucaxsilveira/react-typescript-chrome-extension"&gt;https://github.com/lucaxsilveira/react-typescript-chrome-extension&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Main technologies used in this project:&lt;/p&gt;

&lt;p&gt;React&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Typescript&lt;/li&gt;
&lt;li&gt;Webpack&lt;/li&gt;
&lt;li&gt;Tailwindcss&lt;/li&gt;
&lt;li&gt;Eslint + prettier + editorconfig&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, an extension is basically built inside a popup, which is defined in the manifest file defined by Google (manifest v3).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cQl7Ytqo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r8odzb559u3c7d19i7ti.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cQl7Ytqo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r8odzb559u3c7d19i7ti.png" alt="manifest v3" width="800" height="835"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Some important points in this file:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;actions&lt;/strong&gt; - basically what will be the html responsible for displaying the popup when opening the extension&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;permissions&lt;/strong&gt; - permissions your extension can have&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;background&lt;/strong&gt; - file related to commands that will be sent to the background of your extension&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;content_scripts&lt;/strong&gt; - multiple files that can be added and run within the window in question&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For a better definition of all possible keys and values, you can find it here &lt;a href="https://developer.chrome.com/docs/extensions/reference/manifest"&gt;https://developer.chrome.com/docs/extensions/reference/manifest&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Talking a little more about &lt;strong&gt;content_scripts&lt;/strong&gt;, these files will be &lt;strong&gt;added to the page&lt;/strong&gt; in question that the user is browsing and not within the popup.html that is opened when the extension is clicked. For example, we can have the following behavior:&lt;/p&gt;

&lt;p&gt;The user clicks on a button, in this case &lt;strong&gt;Open content page&lt;/strong&gt;, and from then on you can, based on a condition / or not, render something within the page.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--V_KDhpRk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x1rz8petabhhay015v6c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--V_KDhpRk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x1rz8petabhhay015v6c.png" alt="chrome browser content script" width="800" height="306"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This opens up a huge range of interaction possibilities for the page.&lt;/p&gt;

&lt;p&gt;But cool, how do we do this via code? Simple, rendering another React application within &lt;strong&gt;contentScript.js&lt;/strong&gt; itself&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--f9mPHzqw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pdv0sxxunzbmpd168fmn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--f9mPHzqw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pdv0sxxunzbmpd168fmn.png" alt="rendering contentScript" width="800" height="657"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This file is &lt;strong&gt;automatically injected&lt;/strong&gt; by the browser, so be very careful with what you do inside it, generally adding some viewing rule is necessary.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ERGotGJQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/72mjimp1lp13jw2hitl6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ERGotGJQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/72mjimp1lp13jw2hitl6.png" alt="visualization rule for content_scripts" width="800" height="777"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we see in this code above, it will only be displayed if a message is received, this is where the magic comes in.&lt;/p&gt;

&lt;p&gt;We can trigger this directly from popup.html&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Gfmw2bva--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nwbph3mp2rgz2gh5vfy1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Gfmw2bva--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nwbph3mp2rgz2gh5vfy1.png" alt="trigger event for the content_script" width="800" height="689"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Clicking on the button then &lt;strong&gt;triggers a message&lt;/strong&gt;, this message is "heard" by content_scripts, thus displaying the div on the page for the user.&lt;/p&gt;

&lt;p&gt;With these listeners, an infinite number of business rules can be created. I brought a simpler case to show that it is possible to have visualization directly on the page in a simple way and with the best technologies on the market.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Trigger example&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;chrome.tabs.sendMessage(tab.id, {
  value: "openPopup",
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Example of listening&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;chrome.runtime.onMessage.addListener((message) =&amp;gt; {
  if (message.value === "openPopup") {

  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Well, that's it, see you soon!&lt;/p&gt;

</description>
      <category>react</category>
      <category>tailwindcss</category>
      <category>extensions</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
