Communication Between Chrome Extension and Local Application


The capability of Chrome extension running in browsers is restricted because of security reasons. Sometimes we may want the extension to read the data we need within browsers and pass the data to local applications for further processing.

A possible approach, with only one click from extension user (you), is that we can create an clickable HTML link in the web page and the href property of the link points to localhost, i.e., your local program running a web server. We can embed the browser data, such as cookies or username, in the href property of the link. When extension user (you) click the link, local application running web servers receives HTTP request with browser data can perform further processing.

For example, you can create an HTML link in your extension and append the link to the web page as follows:

var link = document.createElement("a");
link.setAttribute("href", "http://localhost:8999/download/username/profile_pic/");
link.setAttribute("target", "_blank");
document.querySelector("body").appendChild(link);

And in your local program (use Go as example), you can receive HTTP requests as follows:

package main

import (
      "fmt"
      "log"
      "net/http"
      "strings"
)

func handler(w http.ResponseWriter, r *http.Request) {
      parts := strings.Split(r.URL.Path, "/")
      if len(parts) != 5 {
              fmt.Fprintf(w, "invalid request")
              return
      }
      username := parts[2]
      fmt.Fprintf(w, "username: %s\n", username)
      action1 := parts[1]
      action2 := parts[3]
      fmt.Fprintf(w, "action: %s %s\n", action1, action2)

      if action1 == "download" && action2 == "profile_pic" {
              // download profile pic here
      }
}

func main() {
      http.HandleFunc("/", handler)
      log.Fatal(http.ListenAndServe(":8999", nil))
}

This approach requires only one click from extension user. If you only use it for private, this is a possible and interesting approach to fit your needs.


References:

[1]