import ext from "./utils/ext";
import storage from "./utils/storage";

ext.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    if(request.action === "perform-save") {
      var data = request.data;
      var url;
      var token;
      storage.get(['url', 'token'], function(resp) {
        url = resp.url;
        token = resp.token;
        var destination = url+"?v=1&u="+encodeURIComponent(data.url)+"&t="+encodeURIComponent(data.title)+"&m="+encodeURIComponent(token);
        fetch(destination).then(function(response) {
           sendResponse({ action: "saved" });
        });
      });

      return true; // https://stackoverflow.com/a/20077854
    }
  }
);