Exploring Chrome APIs: contextMenus

Exploring Chrome APIs: contextMenus

Play this article

Since I started working on Layfy's second release, I decided to add some features I would have liked to have as its primary user. One of these features is "Search on Spotify". Have you ever been in a situation where you find the title of a song or artist and want to browse to Spotify to see their other songs or follow them? I know I have!

So, when thinking of new features, this one of course rose to the front of the list. And so I started looking at how to do this in Chrome, and found the contextMenus API. It allows, as the name suggests, to create contextualized menus. They can either appear when the user selectes some text, or based on the current page's url, or always be present. I chose the first option, since I needed the user to select some text before doing anything on the extension's side.

So, did it work?

It did!

Creating a Context Menu item is really quite easy (you can learn more here ) though you will need to ask for the user's permission to use the API (add "contextMenus" in your manifest.json)

After being able to get the user's selected text after they clicked on "Search with Spotify" I implement a redirection to a new tab (using the tabs API ) with the result.

Did you try this new functionality? If so, let me know what you think!

You can find the latest version of the extension here

Until next time

Did you find this article valuable?

Support Matteo Gassend by becoming a sponsor. Any amount is appreciated!