0

For stekhn, here's the proper link: var location = "http://www.roblox.com/Trade/inventoryhandler.ashx?filter=0&userid=" + i + "&page=1&itemsPerPage=14";

I'm trying to create a Javascript script where I can search through a users inventory, detect if they have what I'm looking for in their inventory and output the userID if they have it.

If I type in bluesteel, I need a Javascript script which will search through http://snackyrite.com/site.ashx?userid=1 and detect if it has the text 'bluesteel' is on it - if it is, I need it to display the user id, which is 1.

You may be thinking that's easy and I can easily find the script for that - well there's a catch, my objective isn't only to get it to search userid=1, I need it to search from userid=1 up to userid=45356

If the word 'bluesteel' is found on userid=5, userid=3054 and userid=12 (these are just examples), I need it to display 5, 3054 and 12 (the ID's) on the same page where the script was ran from.

This is the script I've tried, but the userid won't increase (I'm not sure how to do that).

var location = http://snackyrite.com/site.ashx?userid=1;
if(location.indexOf("bluesteel") > -1) {
    output.userid
}

I do apologize, Javascript isn't my best.

lizzysmith
  • 19
  • 6
  • Are you looking for a web scraper? As JavaScript is normally executed on the client side, it won't be much of a help (except if you use node.js). – stekhn Oct 15 '15 at 19:17
  • Hmm, I'm using a webhost.. would PHP be better? – lizzysmith Oct 15 '15 at 19:23
  • Is this going to be your site or are you just try to collect data from a site you don't own? If it's your site the approach of checking the rendered HTML for data is probably wrong. – stekhn Oct 15 '15 at 20:02

2 Answers2

1

Use a loop:

for (var i = 1; i <=45356; i++) {
    var loc = "http://snackyrite.com/site.ashx?userid="+i;
    // get contents of location
    if (contents.indexOf("bluesteel") > -1) {
        console.log(i);
    }
}

Since getting the contents will presumably use AJAX, the if will probably be in the callback function. See Javascript infamous Loop issue? for how to write the loop so that i will be preserved in the callback function.

Community
  • 1
  • 1
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Absolutely right. But I think the "getting the content" part is the problem. – stekhn Oct 15 '15 at 19:20
  • Maybe, but the question says "the userid won't increase". That sounds like he doesn't know how to do a loop or append the loop index to the URL. – Barmar Oct 15 '15 at 19:22
  • Not sure if this is meant to happen, but I seem to be getting re-directed to snackyrite.com. Sorry if this wasn't made clear in my original post, but the objective is to display the userid on the same page where the script was run from – lizzysmith Oct 15 '15 at 19:45
  • Make sure you're assigning to a local variable named `location`. Assigning to `window.location` redirects the page. – Barmar Oct 15 '15 at 19:56
  • I only included the code you gave me, am I missing some? – lizzysmith Oct 15 '15 at 19:58
  • Use a different variable name than `location`. Or put the code inside a function, so that you get a local variable. – Barmar Oct 15 '15 at 19:59
  • I've tried this, but it's not displaying any results – lizzysmith Oct 15 '15 at 20:28
  • You're supposed to replace `// get contents of location` with your code that fetches the contents of the web site. I assumed you already knew how to do that, and your problem was just with how to change the userid each time. – Barmar Oct 15 '15 at 20:29
0

This kind of web scraping can't be done in the Browser (client-side JavaScript).

I would suggest building a scraper with Node.js.

  1. Install Node.js
  2. Install request npm i request
  3. Install cheerio npm i cheerio
  4. Create a file scraper.js
  5. Run node scraper.js

Code for scraper.js

// Import the scraping libraries
var request = require("request");
var cheerio = require("cheerio");

// Array for the user IDs which match the query
var matches = [];

// Do this for all possible users
for (var i = 1; i <= 45356; i++) {

    var location = "http://snackyrite.com/site.ashx?userid="+i;

    request(location, function (error, response, body) {

        if (!error) {

            // Load the website content
            var $ = cheerio.load(body);
            var bodyText = $("body").text();

            // Search the website content for bluesteel
            if (bodyText.indexOf("bluesteel") > -1) {

                console.log("Found bluesteel in inventory of user ", i);
                // Save the user ID, if bluesteel was found
                matches.push(i);
            }

        // Something goes wrong
        } else {

            console.log(error.message);
        }
    });

    console.log("All users with bluesteel in inventory: ", matches);
}

The above code seems kind of complicated, but I think this is the way it should be done. Of corse you can use any other scraping tool, library.

stekhn
  • 1,969
  • 2
  • 25
  • 38