Get text from web

  • I make small games in Construct 2, and I need a little help with javascript, ajax, or something. I need to search for something in a specific internal web page.

    I need to search if my 'pub-id' is in the 'ads.txt' of each domain where my game is. The problem is that I can't figure out how to crawl to other internal pages, because using 'document' didn't help me out.

    What i've tried:

    var id='2314121'; //my id var found = document.documentElement.innerHTML.indexOf(id); //it's searching my id in the HTML code if(found >= 0) //it will return -1 if it's not there, and position if's there { alert('Found it at position: '+found); }

    The problem is that this will search just on the current page, and I need to search in a specific one. I need to search in 'www.domainexample.com/ads.txt'.

    I found how to tell on which site I am with 'window.location.host', but I'm stuck here. I can't crawl from the index to ads.txt.

    Anybody knows how to make this?

    Thank you! :)

  • Ok, I found how to do that.

    The next question is: I can output the result in Construct 2? Like, if I have a variable "aux" in Construct 2 and the variable "found" from the Javascript code, it's there a way for me to transfer the information from the code to my variable, like ex: "aux = found".

    Javascript code:

    var found;
    $.get(
     'http://domain-example.com',
     {paramOne : 1, paramX : 'abc'},
     function(data) {
     found = data.indexOf('your-text');
     if(found >=0)
     {
     alert('Your text was found at position: '+found);
     }
     }
    );
    

    Anybody? :)

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Done.

    Here is the Javascript code:

    var found = -1;
    $.get(
     'http://domain-example.com',
     {paramOne : 1, paramX : 'abc'},
     function(data) {
     gasit = data.indexOf('the word are you looking for');
     if(found >= 0)
     { 
     c2_callFunction('cauta_pub_id', [found]);
     
     }
     }
    );
    

    In the C2 you have the function "cauta_pub_id" where you set the variable to the first parameter which is sent from Javascript code.

    This is how you can create a bridge between Javascrip and Construct2 variables.

    Hopes this help someone. :)

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)