0

How can I turn the results from Script 1, Name, Email, Teamsinto variables I can use in script 2?

I am making an API call to fetch some JSON I then want to use certain values as text in a message I then send to slack.

Example.

$('.Name').html(data.user.name); // Returns John
$('.Email').html(data.user.email); // Returns [email protected]
$('.Teams').html(data.user.teams[0].name); // Returns JohnsTeam

var text = 'Hello my name is $Name + my email is $Email + From $Teams'

Output = Hello my name is John my email is [email protected] From JohnsTeam

Script 1

function currentUrl() {
  return new Promise(function (resolve) {
    chrome.tabs.query({
      active: true,
      currentWindow: true
    }, function(tabs) {
      resolve(tabs[0].url)
    })
  })
}

function userIdfromUrl(url) {
  var parts = url.split('/')
  return parts[parts.length - 1]
}
var authorizationToken = "xxxxxxxxxxxxxxxxxxxxxxxxx";
function myapiRequest(endpoint, options) {
    $.ajax($.extend({}, {
    type: 'GET',
    dataType: "json",
    success: function(data) {
    $('.Name').html(data.user.name);
    $('.Email').html(data.user.email);
    $('.Teams').html(data.user.teams[0].name);
},

    url: "https://api.myapi.com/" + endpoint,
    headers: {
      "Authorization": "Token token=" + authorizationToken,
      "Accept": "application/vnd.myapi+json;version=2"
    }
  },
  options));
}
currentUrl()
  .then(function (url) {
    return userIdfromUrl(url)
  })
  .then(function (userId) {
    return myapiRequest('users/' + userId + '?include%5B%5D=contact_methods&include%5B%5D=teams')
  })
  .then(function (data) {
    console.log(data.user.name)
    console.log(data.user.email)
    console.log(data.user.teams[0].name)
  })

Script 2

$(document).ready(function(){
    $('#contact-submit').on('click',function(e){
        e.preventDefault();
        var url = 'https://hooks.slack.com/services/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
        var text = 'This is a message'
        $.ajax({
            data: 'payload=' + JSON.stringify({
                "text": text // What I want to dynamically change
            }),
            dataType: 'json',
            processData: false,
            type: 'POST',
            url: url
        });
    });
});
1
  • Thanks for the reply, although I am not sure what you mean, don't suppose you could provide an example? Thanks Commented Sep 2, 2017 at 2:13

1 Answer 1

1

One great solution is to set the variable you get from the response in the HTML5 localstorage as follows:

  • Inside ur success:
success: function(data) {
  localStorage.setItem("urdata",JSON.stringify(data));
}
  • In the other script, u can retrieve the data like this:

var data = localStorage.getItem("urdata"); data = JSON.parse(data);

Sign up to request clarification or add additional context in comments.

3 Comments

Unfortunately I have to stay away from any localsotrage. Also I am not sure how each of my returned values would be declared doing it this way. Thanks
if they're different scripts in different pages, you have whether to use localStorage or a global variable in the session, as far as I know
Global variable is what I was hoping for, its a chrome application so I think everything is default global.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.