Sending data to your site from your Chrome Extension
Jan 16, 2016
How to open a new tab with a specific address from your Chrome extension is well documented in Google’s help articles. For completeness sake, let us start with discussing this. Before we create a new tab, we should add permissions for accessing “chrome.tabs” in your manifest.json.
Now we are ready to open a new tab. We need to do this from your background.js script. In the following example, we open a new tab when the page action button is clicked. If your extenstion does not use a page action, you could modify it to be used with a button click or some other event. Notice the question mark at the end of the URL. Of course, there should be a global variable named data which contains the get parameters.
Get is easy, Post is a little more complicated.
Sending a post request to one’s site from a chrome extension is not as straightforward. We need an intermediate local HTML file which can in turn do a post request. In the below example, we first redirect to send.html (which is part of the chrome extension). This send.html contains javascript needed for the post request. Notice the chrome.runtime.getURL function call to get the absolute path of the send.html file.
The first url is the website address you want to be redirected to eventually. Data to be sent with post is packaged as a javascript object for ease of extraction later.