How to get Dynamic Javascript from another site?

Asked

Viewed 53 times

0

Depending on the URL I use, data from Javasscript changes.

If I use the url: meusite.com/jogador/123, Javascript will bring you player data 123, if I change player 123 to 456, Javascript below will bring player statistics 456.

    <script type="text/javascript">var imp_careerStats = [
{
"id": "0c8d010d-9d47-4f10-805e-544ae89996b9",
"playerId": "67059891",
"mode": 4,
"type": 2,
"lastUpdated": "2019-06-09T17:00:51.7847614Z",
"kills": {
  "value": 1,
  "key": "Kills",
  "percentile": 92.0,
  "displayName": "Kills",
  "displayType": "Number",
  "category": "Combat",
  "columnName": "kills",
  "displayValue": "1",
  "displayPercentile": "Top 8%"
},...
</script>

How I can turn this Javascript into JSON?

  • Your question is very confusing, try to improve it.

  • See if it looks better for you now. What I need is to take the data of this javascript that is on another site and be able to use it, ideally put it as json

  • Comes with the tag <script type="text/javascript"> and all?

  • yes, come complete, I copied and pasted the example.

1 answer

1

You can use the JSON.parse(), but it is necessary to remove everything that is not object of the string, such as tags <script> and the array delimiters [ and ] (or ];, the semicolon after closing the array, if any).

For this you can use the .replace() with a regular expression that removes everything that is tag from the string, the array declaration and its bounding brackets, leaving only the object:

// "data" seria o retorno em forma de string
var data = `
<script type="text/javascript">var imp_careerStats = [
{
"id": "0c8d010d-9d47-4f10-805e-544ae89996b9",
"playerId": "67059891",
"mode": 4,
"type": 2,
"lastUpdated": "2019-06-09T17:00:51.7847614Z",
"kills": {
  "value": 1,
  "key": "Kills",
  "percentile": 92.0,
  "displayName": "Kills",
  "displayType": "Number",
  "category": "Combat",
  "columnName": "kills",
  "displayValue": "1",
  "displayPercentile": "Top 8%"
}
}];
<\/script>`;

var json = data.replace(/<[^>]*>?|var imp_careerStats = \[|\](;)?/g, '').trim();
json = JSON.parse(json);
console.log(json);

  • thanks for the reply, but how do I do that if javascript is on an external site I am not the owner?

  • I don’t understand the question.

  • i don’t have access to the site that contains javascript, so how do I copy this external javascript?

  • Then I no longer know. I based the answer on what you reported in the question, saying that it receives the script in string form.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.