javascript using ajax with ASC

I'm getting started on a new project to improve the list of my posts.


I looking to extract the number of replies per thread. I'm learning ajax. I seem to have found the data I want, but am not getting the data into a structure that I can use. I believe that the data is being returned as one string, but I want it to be an array of strings. I'm trying to get the number of replies into an array. I'm getting replies into a string. It could be my console.log code is wrong.


I'm sure there are other ways of extracting the date, but I'm looking to use ajax in this attempt.


ASC web pages is:

https://discussions.apple.com/content?filterID=following~objecttype~objecttype[t hread]


// 
var foundIt = $("table tr.js-browse-item td.j-td-replies").text();

console.log(foundIt);
for ( i = 0;i<foundIt.length;++i )
{
  console.log("reply " + i + " is " + foundIt[i] );
}



249295665411013282046029 
reply 0 is 2 
reply 1 is 4 
reply 2 is 9 
reply 3 is 2 
reply 4 is 9 
reply 5 is 5 
reply 6 is 6 
reply 7 is 6 
reply 8 is 5 
reply 9 is 4 
reply 10 is 1 
reply 11 is 1 
reply 12 is 0 
reply 13 is 1 
reply 14 is 3 
reply 15 is 2 
reply 16 is 8 
reply 17 is 2 
reply 18 is 0 
reply 19 is 4 
reply 20 is 6 
reply 21 is 0 
reply 22 is 2 
reply 23 is 9



reply 6 should be 66 not 6.


User uploaded file


I'm looking for this list as an array like object of some sort.

User uploaded file

Mac mini, OS X Yosemite (10.10.5), Fall 2014; iPhone 4 7.1.2

Posted on Feb 3, 2017 12:04 PM

Reply
10 replies

Feb 3, 2017 2:06 PM in response to rccharles

I think you have to route it back through jQuery. Try something like this:


$("table tr.js-browse-item td.j-td-replies").each(
  function( index )
    {
    console.log(index + ": " + $( this ).text());
    }
 );


There is no getting around objects, especially when you use jQuery. There is a lot of magic in that $().


It is much, much easier to use the JSON output from the REST API.

Feb 3, 2017 1:07 PM in response to rccharles

That is just concatenating all of the text together into one string. You would have to iterate through the resulting elements and call text() on each one. I suggest avoiding trying to parse the HTML, it is a royal mess. Look through the Jive online documentation an learn the REST interface. Most parts are working. If you tell me exactly what kind of data you are looking for, I can probably give you a little head start to exactly where to look in the documentation for this.


Now, if you are trying to hack up the interface in an extension or something, you'll have to deal with the HTML. But since you mentioned Ajax, it is easier to make a REST request with Ajax and the result is just a JSON object, an associative array. You don't even need jQuery to parse it.

Feb 3, 2017 1:48 PM in response to etresoft

I'm trying to avoid hacking through it. I thought that ajax was the greatest!


// 
var foundIt =  $("table tr.js-browse-item td.j-td-replies") ;



console.log(foundIt);
for ( i = 0;i<foundIt.length;++i )
{
  console.log("reply " + i + " is " + foundIt[i].text() );
}


the quick fix of .text() didn't hack it. Why?


Object { 0: <td.j-td-replies.center.font-color-meta>, 1: <td.j-td-replies.center.font-color-meta>, 2: <td.j-td-replies.center.font-color-meta>, 3: <td.j-td-replies.center.font-color-meta>, 4: <td.j-td-replies.center.font-color-meta>, 5: <td.j-td-replies.center.font-color-meta>, 6: <td.j-td-replies.center.font-color-meta>, 7: <td.j-td-replies.center.font-color-meta>, 8: <td.j-td-replies.center.font-color-meta>, 9: <td.j-td-replies.center.font-color-meta>, 14 more… } 
TypeError: foundIt[i].text is not a function [Learn More]


I'm pre all this object stuff.


R

Feb 3, 2017 7:03 PM in response to etresoft

I'm reading JavaScript: The Definitive Guide.


I did a search for REST and found a page saying REST was for getting data from the server which I'm not doing. You have a reference for REST with lots of examples? ASC is using REST so I don't have to read in the functions?


I was thinking of passing the data to a function, but I missed the importance of the each method.


"This" works too.

Somewhat mysterious why this works over my previous attempt without the $(). At least the getting of the data. I thought you could attach methods to data. Maybe you need a function $() to get things started.

// 
var foundIt =  $("table tr.js-browse-item td.j-td-replies") ;



console.log(foundIt);
for ( i = 0;i<foundIt.length;++i )
{
  console.log("reply " + i + " is " + $(foundIt[i]).text() );
}


I know enough now to hack around and do my thing.


R

Feb 3, 2017 7:53 PM in response to rccharles

If you are on the internet, you are always getting data from the server. If you want to explore hacking on ASC, the best place to start is here: https://developers.jivesoftware.com/api/v3/cloud/rest/index.html


Look for the "people" service. That will tell you how to get URL to your profile. You can just open that in a web browser and see the JSON. The only trick is the first line that it returns "throw 'allowIllegalResourceCall is false.';". You will need to strip off the first 36 characters of every response using a substring() call. Then you can use jQuery to parse the JSON into a useable object.


When you are in a web context, there is always a "model" for data. In this case, it is the DOM - the Document Object Model. That is the logical structure of the web page you are on. But it is just data, no objects. That jQuery function accepts a selector as input and returns the resulting data wrapped in a jQuery object that allows you to easily perform manipulations on the DOM.


You are definitely making progress. But you really only want to deal with the HTML content of a page if you actually want to change it in some way or if you have no other choice.


If I wanted to get your posts using the REST interface, I would do something like this:

var me = getREST(personURL);
var activity = me.resources.activity;

for var post in activity.list
 {
 var displayName = post.object.displayName;
 var url = post.object.url;
 }


You would have to construct the personURL and write the getREST() function to perform the Ajax call, strip off the string, and parse the JSON result.

Feb 3, 2017 8:13 PM in response to etresoft

I'm modifying the content of the page. I don't have a need to get something from the server just yet. I might explore that in the future.


Unless there were some simple way getting the original poster to a thread. At one time the list of your posts included the name of the original poster and the name of the last poster. Today, it includes the name of the last poster.


Here is what I have:

replyCounts = new Array();

$("table tr.js-browse-item td.j-td-replies").each(  
  function( index )  
    {   
    replyCounts[index] = $( this ).text();
    }  
 ); 


for ( i = 0;i<replyCounts.length;++i )
{
  console.log("reply " + i + " is " + replyCounts[i] );
}


R

Feb 4, 2017 4:00 PM in response to rccharles

Ops. Should have gone with the one closer to the example that I copied from. It makes a difference in the output when you swap $("a:first", trSection ) as I did above.This is the one that works.


var replyCounts = new Array();
var titleCounts = new Array();

$("table tr.js-browse-item td.j-td-replies").each( 
  function( index ) 
    {  
    replyCounts[index] = $( this ).text();
    } 
);


for ( i = 0;i<replyCounts.length;++i )
{
  console.log("reply " + i + " is " + replyCounts[i] );
}

console.log("Find the titles");

var trSection = $("table tr.js-browse-item td.j-td-title");

$("a:first", trSection ).each( 
  function( index ) 
    {  
    titleCounts[index] = $( this ).text();
    $( this ).append('<span style="background-color: oldlace !important; "><strong>  abc</strong></span>');
    } 
);

console.log("print the titles");
for ( i = 0;i<titleCounts.length;++i )
{
  console.log("title " + i + " is " + titleCounts[i] );
}



output:

reply 0 is 9
reply 1 is 10
reply 2 is 368
reply 3 is 3
reply 4 is 4
reply 5 is 4
reply 6 is 9
reply 7 is 2
reply 8 is 9
reply 9 is 5
reply 10 is 66
reply 11 is 5
reply 12 is 4
reply 13 is 1
reply 14 is 10
reply 15 is 13
reply 16 is 2
reply 17 is 8
reply 18 is 20
reply 19 is 4
Find the titles
print the titles
title 0 is javascript using ajax with ASC
title 1 is How can I contact a community user?
title 2 is Your System has Run out of Application memory
title 3 is how to manage subscriptions if your not a level 4 community member?
title 4 is Looking to mirror my ipad to another ipad or monitor
title 5 is Am I the only one who can't post community questions from my iPad?
title 6 is I opened link from scam text. What happens now. Help.
title 7 is how to push out ios update as a package
title 8 is Can a toddler make my iPad Air 2 screen start to flicker?
title 9 is New ipad- old ipad
title 10 is Why does apple slow down old devices?
title 11 is .dmg file password forgot
title 12 is iPad Mini's can't see or print to 3 out of 8 identical printers on work network
title 13 is My office has 9 iMacs that i want to use iCloud for. Should I set up separate apple ids for each or just use one for all?
title 14 is Increase Response Limit
title 15 is Why is the size and colour of text si small and pale?
title 16 is Screen Share to Monitor Cheating
title 17 is How to stop Apple mail from displaying spam message contents before I delete it?
title 18 is Force wifi to specific BBSID. (force 5ghz)
title 19 is Remove Mail.app

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

javascript using ajax with ASC

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.