-2

I am trying to make a Greasemonkey userscript which it will display info from one website on another. I tried doing this with regular expressions, but got stuck on the match() syntax.

The page content is something like this:

<html><body>
<h1 class="pos-title">Fritz Paul</h1>
<div class="columns">
    <div class="pos-column1">
        <ul id="attributes">
             ... ...
        </ul>
    </div>
    <div class="pos-column2">
        <ul class="attributes">
             ... ...
        </ul>
    </div>
</div>
</body></html>

and I want to get only the:

<div class="columns">
    <div class="pos-column1">
        <ul id="attributes">
             ... ...
        </ul>
    </div>
    <div class="pos-column2">
        <ul class="attributes">
             ... ...
        </ul>
    </div>    
</div>


I've tried code like:

attibutes = responseDetails.responseText.match(xxxx);
playerNotesContent.innerHTML = attibutes;

I tried a lot of .match(...) examples that I found in this site but I can't make it work.

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
Alex Bogias
  • 1,826
  • 2
  • 30
  • 45

1 Answers1

1

As others have said, never attempt to parse HTML with regular expressions. Use DOM parsing instead.

The question is not clear, but it appears that you are fetching a page via AJAX and attempting to parse it, right? Also, do you really want all the markup inside each <div class="columns"> or just specific bits of text?

Here is the general approach for Greasemonkey. It uses jQuery to make DOM parsing easier.
Updated based on OP's comments and posted script. :

// ==UserScript==
// @name        _Parse Ajax Response for specific nodes
// @namespace   http://www.test.com
// @include     http://www.test.com/player/*/details.php
// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// ==/UserScript==

var player_id = document.location.pathname.match(RegExp('player/([^/]+)/details.php$'))[1];
if (!player_id) { return; }

GM_xmlhttpRequest ( {
    method: 'GET',
    url:    'http://www.test2.com/players/item/' + player_id,
    onload: function (responseDetails) {
                var respDoc     = $(responseDetails.responseText);
                var targNodes   = $("div.columns", respDoc);

                $("#playerContent").after ('<div id="player_stats_block"></div>');

                $("#player_stats_block").append (targNodes);
            }
} );
Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • Thanks a lot for all the info firstly. I try inserting it with the following command "playerNotesContent.innerHTML = targNodes;" but i just get an [object object] – Alex Bogias May 01 '12 at 23:46
  • and if i try `$("#player_stats_block").append(targNodes);` i get the whole page `...` and also it breaks the css of the main site – Alex Bogias May 02 '12 at 00:09
  • That means that the sample code in the question is incorrect or incomplete. Link to the page you are AJAXing and to the target page. Or link to snapshots (saved HTML) of those pages at http://pastebin.com/. ... Also how are `player_stats_block` and `playerNotesContent` defined? Why didn't you use the code as shown in the answer -- `playerNotesContent.empty ().append (targNodes); `? – Brock Adams May 02 '12 at 00:42
  • Also, confirm that this is for Greasemonkey on Firefox, and confirm the 2 questions I asked. Are you using `GM_xmlhttpRequest()` as shown? – Brock Adams May 02 '12 at 00:44
  • See the updated answer. Your linked script had syntax error(s). Also, it may still be necessary to link to the 2 target pages. – Brock Adams May 02 '12 at 02:47
  • Compare the code I pasted to the code you linked and try to suss out the reasons for the differences. The biggest problem was trying to use jQuery functions on a non-jQuery object. Now mark this question as answered or provide the requested information and also details of how *the original question* is still not solved, please. – Brock Adams May 02 '12 at 23:03
  • Thank you Brock! You are the **MASTER**! – Alex Bogias May 03 '12 at 00:22