samedi 9 mai 2015

Chrome extension to send data to a webpage

I want my chrome extension to send a data to my web application based on a node.js controller.

Like it's done in this question: Submitting Ajax request to node.js controller.

I have used an AJAX call to send the data and the call is successful, but I don't know how to how to pass that url being sent as a parameter to node.js app controller.

Here is my code:

google_helper.js file that is sending data.

$("body :not(#prospector-modal)").on("click", "a.add-to-prospector", function(event){
  var that = this;
  event.preventDefault();
  event.stopPropagation();
  console.log('adding');
  fetchable = $(that).data('target');
  console.log("fetchable " + fetchable.description);
  console.log("fetchable " + fetchable.url); //this is the url that i have to send


  $.ajax('http://ift.tt/1GUXMEg', {
          headers: { 'Content-Type': 'application/json'},
          data: JSON.stringify({fetchables: [fetchable] })
  }).done(function (data, textStatus, xhr) {
    debugLog("POST.done");
    debugLog(xhr);
    $(that).replaceWith("<span class='prospector-added'>Added!</span>");
    initOrReinit();
  }).fail(function (xhr, errorMessage, error) {
    debugLog("POST.fail");
    debugLog(xhr);
    if( xhr.status == 401 ) {
      displayErrorSalesLoftLoginModal();
    } else if (xhr.status == 429) {
      displayErrorLimitModal();
    } else {
      displayErrorModal();
    }
  });    
});

When I am making a request, it is going in .done part of ajax call.

background.js:

var Fetchable = function(data) {
  this.data = data;
};

Fetchable.prototype = {
  fetch: function(delay_milliseconds) {
    var self = this;
    $.get(this.data.url).done(function (data, textStatus, xhr) {
      debugLog("fetch.done");
      queue_processor.doneWorking();
      setTimeout(function() {
        queue_processor.processNext();
      }, delay_milliseconds);
      self.update({response_body: xhr.responseText, response_status: xhr.status});
    }).fail(function (xhr, errorMessage, error) {
      debugLog("fetch.fail");
      queue_processor.doneWorking();
      self.update({response_body: xhr.responseText, response_status: xhr.status});
    });
  },

  update: function(data) {
    var self = this;

    $.ajax({
      url: 'http://ift.tt/1GUXMEg',
      type: 'POST',
      data: data
    }).done(function (data, textStatus, xhr) {
      debugLog("update.done");
      setBadge(data.pending_count);
    }).fail(function (xhr, errorMessage, error) {
      console.log("update.fail: error updating fetchable "+self.data.id);
    });
  }

};
/// End Fetchable ///

Here is the controller code of my node.js app where I want to send the url to controller function:

$scope.getLinkedInData = function() {
  if(!$scope.hasOwnProperty("userprofile")){
    IN.API.Profile(/** url from the call **/).fields(
      [ "id", "firstName", "lastName", "pictureUrl",
        "publicProfileUrl","positions","location","email-address"]).result(
        function(result) {
            console.log(result);
            // ...

Aucun commentaire:

Enregistrer un commentaire