|
sunsuron
|
Oct 30 2009, 10:38 PM
|
|
CODE <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <title>mumeichan</title>
</head> <body>
<p class="normalname">Hello</p> <p class="normalname">World</p>
<div id="bla"></div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> <script type="text/javascript">
//$('#bla').html($('.normalname').text()); // get only the text and put them inside the DIV $('.normalname').clone().prependTo('#bla'); // duplicate all element with the class 'normalname' and put them inside the DIV
</script> </body> </html>
|
|
|
|
|
|
sunsuron
|
Oct 31 2009, 01:11 PM
|
|
If you need web scraping script, it will require mo lines of codes. Why do you need to gather all the username?
|
|
|
|
|
|
sunsuron
|
Oct 31 2009, 10:33 PM
|
|
With GM, this is very easy. The only reason it will failed is because there is nothing returned by your jQuery selector. I did a quick test with GM version 0.8 on this very page and it return what I expect. 1. copy paste the jQuery core library into your GM script. 2. copy paste code below » Click to show Spoiler - click again to hide... « CODE $(document).ready(function() { var storage = []; var usernames = []; $('span.normalname a').each(function(i) { usernames.push($.trim($(this).text())); }); label:for (var i=0; i < usernames.length; i++) { // remove duplicate username if any for (var j=0; j < storage.length; j++) { if (storage[j] == usernames[i]) continue label; } storage[storage.length] = usernames[i]; } alert(storage); });
The code above will try to scan all occurrence of ' span.normalname a' which translates 'Find all <span> with have the class value of normalname and <a> as their child'. I will get an alert contains all the user names in this page. I did not test in other pages.
|
|
|
|
|
|
sunsuron
|
Nov 1 2009, 08:54 PM
|
|
Dealing with multiple class can be bit tricky but luckily jQuery provide the attr() function. We can find all the elements with specific classes with this function. Here is an example: » Click to show Spoiler - click again to hide... « CODE <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <title>mumeichan</title>
</head> <body>
<p class="username rollhover">Hello</p> <p class="username rollhover">World</p>
<p class="username rollhover">Hello</p> <p class="username rollhover">World</p>
<p class="username">Gooobye</p> <p class="rollhover">World</p>
<div id="bla"></div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> <script type="text/javascript">
/** * find all <p> if the <p> element have class attribute equal to 'username rollhover' */
function get_special_paragraph() {
var collection = []; $('p').each( function(i) { if ( $.trim($(this).attr('class')) == 'username rollhover') { collection.push($(this).text()); } } ); return collection; }
/** * remove duplicate in an array if any */
function get_unique_array(a) { var b = []; label:for (var i=0; i < a.length; i++) { for (var j=0; j < b.length; j++) { if (b[j] == a[i]) continue label; } b[b.length] = a[i]; } return b; }
/** * main program */
var my_paragraphs = get_special_paragraph(); my_paragraphs = get_unique_array(my_paragraphs); alert(my_paragraphs);
</script> </body> </html>
|
|
|
|
|
|
sunsuron
|
Nov 2 2009, 09:32 PM
|
|
I declare a variable called collection to store array of something (in this case, usernames). It is the same effect with: CODE var collection = new Array(); It's just a syntax shortcut.
|
|
|
|
|