Skip to main content

Let's build a Chrome Extension using Flutter!

Published 3 min readMarkdown
Part of seriesFlutter
Let's build a Chrome Extension using Flutter!

Intro

In todays' article, we will create a Chrome extension using Flutter. As we know Flutter can be used to build apps across multiple platforms, so we will make use of Flutter web in order to build our Chrome extension.

So without wasting any time, let's get started. RunGIF.gif

Project Setup

First of all, we will create a new Flutter project and enable web support for it. You can do this by typing this command into your integrated terminal.

flutter run -d chrome

This will run the Flutter project in your Chrome browser.

Coding the Logic

Now let's see how we can change our current website into an extension. For that, we will need to go to our index.html file. This file is present in the web folder. In index.html, there will be some piece of code between the tag.

Just replace the code with the whole script tag with the following code.

<script
 type="application/javascript" src="main.dart.js">
</script>

Once done with this, we will need to change the manifest.json file. manifest.json is also in the web folder as well. Replace the complete code in the manifest.json with this code.

{
    "name": "flutter_chrome_extension",
    "description": "flutter_chrome_extension",
    "version": "1.0.0",
    "content_security_policy": {
        "extension_pages": "script-src 'self' ; object-src 'self'"
    },
    "action": {
        "default_popup": "index.html",
        "default_icon": "/icons/Icon-192.png"
    },
    "manifest_version": 3
}

We are done with making the required changes to make an extension. Let's run our current code. For that, type this command in the integrated terminal.

flutter build web --web-renderer html --csp

Once our project is up and running, let's add it to chrome. For that, type this in your web browser.

chrome://extensions/

You will reach a page like this. Screenshot (148).png

On this page, there will be a toggle on the top-right Turn it on and then you will see three buttons on top. Out of those 3, tap on the load unpacked button. It will ask you for a folder, there we will need to add the web folder which is under the build folder in our project directory.

Now, open a new tab and you will see a new extension with Flutter icon. Tap on it and Voila! you will see our Flutter counter app. Screenshot (149).png

If you were able to do this then CONGRATULATIONS! You have successfully created a Chrome extension with your Flutter code.

You can think of unique ideas for your extension. For example a crypto price tracker, discount coupon tracker, and so on. If you want to change the icon of your extension, add the new image in your icon folder in the web folder and mention that file name in manifest.json.

    "action": {
        "default_popup": "index.html",
        "default_icon": "/icons/Icon-192.png"  // make the change here.
    },

Conclusion

This concludes today's article. We have seen how to make a chrome extension. I have just shown you how to make the required changes to convert your current web code to an extension. You can now play around and build some cool stuff with it.

You can appreciate and support my blogs via.

https://cdn.hashnode.com/res/hashnode/image/upload/v1646372265341/O0KkM6E-0.png

Also, let's connect on Twitter. Follow CSwithIyush for more amazing tutorials, tips/tricks on Flutter & DSA.

About the author

  • Android App Developer & Product Engineer @Mosaic Wellness. 4x Hackathon Winner. SWE Intern @Bonito Designs, @Legato Health Tech. Open for Freelance gigs. Documenting my tech journey via Hashnode & ranting about my life on Twitter.

Thanks for sharing!

Do you know if it's possible to write a chrome extension with Flutter which modifies (adds data / modifies colors / etc) the site currently being browsed?

I think currently only Javascript can do this. I am hoping that we will be able to do this with Dart & Flutter one day. You can read the first answer to this question for more clarity

https://stackoverflow.com/questions/67411483/is-it-possible-for-a-chrome-extension-to-add-remove-widgets-at-runtime-in-a-flut

I didn't know you could code extensions without JavaScript. I hope it works with Firefox as well. Awesome blog post as always 😄

1

Thanks, Mohit!😁

More from this blog