How to build a Chrome Extension to save web content to Cosmic

Community Articles
Community Articles How to build a Chrome Extension to save web content to Cosmic

In this tutorial, we're going to build a Google Chrome extension to save web content to Cosmic Buckets. Think of it as your personal web clipper.

TL;DR

Download the code from the GitHub repo

Pre-requisites

You just need to have Chrome installed for testing it out and nothing else.

Installing the demo

  1. Get the source code into your machine by either downloading or cloning the repo, which is located here.
  2. In Chrome, visit chrome://extensions 
  3. Enable Developer mode by ticking the checkbox in the upper-right corner.
  4. Click on the "Load unpacked extension..." button.
  5. Select the directory containing your unpacked extension.
  6. Refresh loaded pages.


You'll now see the extension icon to the right of the Omnibox. Click on it and login with your Cosmic credentials. You can now start saving content to your buckets. 

Understanding the Source Code

The defining file in a Chrome extension is the manifest.json file. Most of the fields in the Manifest are self explanatory. Let's go through the major fields, and see what each file do.

  • manifest_version is "2". it is very important not change it because it is a sign to chrome to how to compile the extension.

  • permissions includes the permissions which extension needs
    -storage permission allows you to store data at "Chrome local storage" which is important to save data across websites and different pages.
     -https://api.cosmicjs.com/v1 permission gives access to retrieve and send data from and to the API anywhere in the extension.

  • browser_action contains "default_icon" and "default_popup" which related with top bar icon and HTML file

  • content_scriptsthis is an important part  which it contains scripts which injected into websites
    - matches: [*://*/*] tells the browser tp inject the scripts to any protocol [http, https,...] and any domain.

    - css field contains CSS style sheets to be injected

    - js field contains JavaScript files to be injected

    -all_frames is true which tells to inject script to all frames like "iframe" tag . it is important to make the extension work everywhere

  • background include background scripts which manage send data to Cosmic and checks authorization

The JavaScript files in the js folder is where the whole functionality is resided. myscript.js file contains all the functions of the extension. contentscript.js file uses jquery and myscript.js to manage the extension function is matched websites. background.js file periodically calls the authorization function and also sends data to the Cosmic bucket.

Conclusion

The advantage of  API-first content management systems is that there is no limit as to where you can integrate your app into.  Your Cosmic Bucket can thus power your web app, native app, browser extensions or basically anything than can consume an API. If you have any questions, please reach out to us on Twitter or join our Slack community.

Was this article helpful?

Thank you! Your input helps us improve our articles and resources. If you have any other questions or comments, please contact support.

You may also like


Thanks to everyone in Dallas who came out to the Vuetiful Holiday Meetup at Simple Media, featuring Vuetify, Netlify and Cosmic. 🎉
This is a quick example to show some of the powerful things you can do with the Cosmic NPM Module. This script will, add a Bucket to your Cosmic account, add an Object Type (Pages) and add a few Pages.
All of your images you upload to Cosmic are already transformed to imgix URLs, a powerful real-time image processing and CDN delivery solution. Let's take these images to the next level by optimizing image formats, compressing, intelligently cropping, & more!
In this tutorial, I'm going to show you how to create a minimalistic but awesome single page app blog using Vue, Vuex, Vuetify, Cosmic and deploy it to Netlify.
5 Website Boilerplates: React, Node.js, Vue, Nuxt.js and Next.js
To demonstrate how easy it is to get started with Cosmic, we'll utilize the Cosmic CLI to quickstart a React Auth App. This app shows implementation of Authentication in ReactJS, NextJS and NodeJS using the Cosmic API. This app has all the basic modules like signin, signup, view/edit profi

Ready to Get Started?

Build personal projects for free. Add your team at unbeatable prices.
Start Now Contact Sales