Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2008
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Working with the DOM and GM_xmlhttpRequest

    Hello,

    I'm working on a Greasemonkey script using the New York Times comments API. One of the things I want to do is parse the xml with GM_xmlhttpRequest.

    Here's an example of what I'm pulling out:
    http://api.nytimes.com/svc/community...mur8f98nerbjtv

    and here is my js:
    Code:
    // set up 2 variables
    var relatedDiv, commentPreview;
    // define relatedDiv
    relatedDiv = document.getElementById('related-content');
    if (relatedDiv) {
    	// fetch and display feed
    	GM_xmlhttpRequest({
    	    method: 'GET',
    	    //url: 'http://greaseblog.blogspot.com/atom.xml',
    		url: 'http://api.nytimes.com/svc/community/v2/comments/url/exact-match.xml?url=http%3A%2F%2Fdotearth.blogs.nytimes.com%2F2008%2F12%2F16%2Fa-cooler-year-on-a-warming-planet%2F&api-key=mschwjy9jpmur8f98nerbjtv',
    	    headers: {
    	        'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey/0.3',
    	        'Accept': 'application/atom+xml,application/xml,text/xml',
    	    },
    	    onload: function(responseDetails) {
    	        var parser = new DOMParser();
    	        var dom = parser.parseFromString(responseDetails.responseText,
    	            "application/xml");
    	        // go thru comment nodes
    			var entries = dom.getElementsByTagName('comment');
    	        var cBody;
    	        for (var i = 0; i < entries.length; i++) {
    	            cBody = entries[i].getElementsByTagName('commentBody')[0].textContent;
    				cAuthor = entries[i].getElementsByTagName('display_name')[0].textContent;
    				cAuthorLoc = entries[i].getElementsByTagName('location')[0].textContent;
    				cReccomendations = entries[i].getElementsByTagName('recommendations')[0].textContent;
    				cSequence = entries[i].getElementsByTagName('commentSequence')[0].textContent;
    				// create the div
    				commentPreview = document.createElement("div");
    				commentPreview.innerHTML = '<p> ' +
    					cSequence + '/' + cTotal + cBody + 
    				    '<br>posted by: ' + cAuthor + ', '+ cAuthorLoc +'</a><br>'
    					+ cReccomendations +' recommendations</p>';
    				// write out my div before the related posts
    			    relatedDiv.parentNode.insertBefore(commentPreview, relatedDiv);
    	        }
    	    }
    	});
    }
    This works fine for all comments, I can pull out authors, locations, etc. Where it breaks down is in responses, in the xml like this:

    Code:
    <comment>
    <commentSequence>19</commentSequence>
    <commentTitle/>
    <commentBody>
    Mr. Revkin ... It's about time the NYT editorial board directed it's writers to "report" the facts, rather than perpetuating this outrageous climate change lie.<br /><br />“Make the lie big, make it simple, keep saying it, and eventually they will believe it”<br />
    </commentBody>
    <createDate>1229451232</createDate>
    <updateDate>1229451848</updateDate>
    <approveDate>1229451848</approveDate>
    <recommendations>17</recommendations>
    	<replies>
    	<comments>
    	<comment>
    	<commentSequence>1</commentSequence>
    	<commentTitle/>
    	<commentBody>
    	You've got to be more specific here. Is the "lie" that the world is warming, that people are contributing, that it's dangerous, or ...? I think you'll find my coverage is held out on all "sides" to be fair and accurate, both in describing what science has revealed, and what it hasn't figured out yet. 
    	</commentBody>
    	<approveDate>1229451848</approveDate>
    	<recommendations>0</recommendations>
    	<replies/>
    <editorsSelection>N</editorsSelection>
    <display_name>
    <a href="http://topics.nytimes.com/top/reference/timestopics/people/r/andrew_c_revkin/index.html" target="_blank">Andy Revkin</a
    </display_name>
    <location>Garrison, NY</location>
    <userComments>
    api.nytimes.com/svc/community/v2/comments/user/id/2781029.xml
    </userComments>
    </comment>
    </comments>
    </replies>
    <editorsSelection>N</editorsSelection>
    <display_name>Rich Pletcher</display_name>
    <location>Fuquay Varina, NC</location>
    <userComments>
    api.nytimes.com/svc/community/v2/comments/user/id/48104118.xml
    </userComments>
    </comment>
    If I request the node named display_name it will take it from any point in the hierarchy, when I don't want to go that deep. But as far as I can find in the documentation, there is no way to limit the depth that GM_xmlhttpRequest can go.

    Am I missing something? How can I tell the script to choose the display_name from the lowest hierarchy before moving into the responses and then grabbing that one too?

    Simple to do in xsl, but is there a mechanism for it here?

    Thanks for any help.
    Last edited by bonusball; 12-29-2008 at 05:56 AM. Reason: fixed code tags

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,292
    Thanks
    10
    Thanked 583 Times in 564 Posts
    this is either invalid XML, or a cut-off version.
    thus there is not enough information to tell.

    what i can say is that you can use getElementsByTagName on tags as well as whole documents.

    without knowing the structure of the xml, something like

    entries[i].getElementsByTagName('recommendations')[0].getElementsByTagName('display_name')[0].textContent;

    should give you what you want. ask for the parent tag, then the "display_name" tag.



    also don't forget that you can easily use xsl.
    run the transform in greasemonkey and .innerHTML the result into your document...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #3
    New to the CF scene
    Join Date
    Dec 2008
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Yes, this is a snippet.

    But thanks rnd me, I didn't know I could use getElementsByTagName that way. In the meantime I decided to go the xpath route.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •