1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

I need Help with some javascript code

Discussion in 'JavaScript' started by Taruna P, Nov 17, 2014.

  1. #1
    Hi, I managed to get a video url from other page but I need to load that video with jwplayer.
    Here is where I got.

    CODE:
    <html>
    <head>
    </head>
    <body>
    <?php
    include ('functions.php');
    $scraped_page = curl("url");
    $scraped_data = scrape_between($scraped_page, "eval", "</script>");
    echo '<div id="packed" style="visibility:hidden;">';
    echo 'eval'.$scraped_data;
    echo '</div>';
    ?>
    <script language="javascript" type="text/javascript">
    var coded = document.getElementById('packed').innerHTML;
    var code;
    eval("var value=String" + coded.slice(4));
    code = value;
    var vidurl = code.substr(156,90);
    var imgurl = code.substr(270,51);
    </script>
    </body>
    </html>

    I need to pass to jwplayer the vidurl as the video and imgurl as thumbnail
     
    Taruna P, Nov 17, 2014 IP
  2. seductiveapps.com

    seductiveapps.com Active Member

    Messages:
    200
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Put your code on a test page, replace your javascript with the following:
    
    var coded = document.getElementById('packed').innerHTML;
    console.log ('coded', coded);
    var
    code,
    want = coded.slice(4);
    console.log ('want', want);
    eval("var value=String" + want);
    code = value;
    console.log ('code', code);
    var vidurl = code.substr(156,90);
    console.log ('vidurl', vidurl);
    var imgurl = code.substr(270,51);
    console.log ('imgurl', imgurl);
    
    Code (markup):
    Then first run this code yourself in the browser, press F12 after having clicked anywhere on the page, to bring up the browser debugger window. In that window, look for the "console" tab (in IE it's on the left sidebar, in Chrome/Firefox it's at the top of the debugger window) and have a look at the output.
    Be careful about the place for your vidurl and imgurl being different (so the integer parameters for the call to code.substr() could be different for each/some curl requests).
    You'd best look at (google search this:) "javascript indexOf" and "javascript lastIndexOf" to get to more useful extraction of vidurl and imgurl. For instance:

    
    var
    vuPre = 'stringJustBeforeYourVidURL',
    vuPost = 'stringJustAFTERyourVidURL',
    iuPre = 'stringJustBeforeYourImgURL',
    iuPost = 'stringJustAFTERyourImgURL',
    lastFound = 0,
    results = [];
    
    while (lastFound!=='finished') {
        var
        vuP1 = code.indexOf(vuPre, lastFound),
        vuP2 = code.indexOf(vuPost, lastFound),
        iuP1 = code.indexOf(iuPre, lastFound),
        iuP2 = code.indexOf(iuPost, lastFound),
        vidurl = (
            vuP1 === -1
            || vuP2 === -1
            ? ''
            : code.substr(vuP1 + vuP1.length, vuP2 - vuP1 - vuP1.length)
        ),
        imgurl = (
            iuP1 === -1
            || iuP2 === -1
            ? ''
            : code.substr(iuP1 + iuP1.length, iuP2 - iuP1 - iuP1.length)
        ),
        lastFound = (
            vuP1 !== -1
            && vuP2 !== -1
            ? vuP2 + vuP2.length
            : 'finished'
        );
    
        if (
            vidurl !== ''
            && imgurl !== ''
        ) {
            results[results.length] = {
                vidurl : vidurl,
                imgurl : imgurl
            }
        }
    };
    if (results.length > 0) {
        console.log ('results', results);
    } else {
        console.log ('Could not find results for code', code);
    }
    
    Code (markup):
     
    Last edited: Nov 20, 2014
    seductiveapps.com, Nov 20, 2014 IP
  3. seductiveapps.com

    seductiveapps.com Active Member

    Messages:
    200
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #3
    It would be more efficient however to do the extraction of vidurl and imgurl in PHP (and not give up your business secrets either that way) and store only vidurl and imgurl either in a MySQL table or a JSON file on the server's filesystem (which would save you the significant overhead of using a mysql table which has to get coded, but also initialized, backed-up and restored more difficultly than a JSON file on the server's filesystem)..
     
    seductiveapps.com, Nov 20, 2014 IP
  4. seductiveapps.com

    seductiveapps.com Active Member

    Messages:
    200
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #4
    in case you do have variances in your scraped content:

    
    
    // example 2 : using multiple possibilities for pre and post strings to be found, where vuPre[0] must be followed only by vuPost[0], vuPre[1] by vuPost[1] and vuPre[2] by vuPost[2].
    var
    vuPre = [
        'stringOneJustBeforeYourVidURL',
        'stringTwoJustBeforeYourVidURL',
        'stringThreeJustBeforeYourVidURL'
    ],
    vuPost = [
        'stringAjustAFTERyourVidURL',
        'stringBjustAFTERyourVidURL',
        'stringCjustAFTERyourVidURL',
    ],
    iuPre = [
        'stringOneJustBeforeYourImgURL',
        'stringTwoJustBeforeYourImgURL',
        'stringThreeJustBeforeYourImgURL'
    ],
    iuPost = [
        'stringAjustAFTERyourImgURL',
        'stringBjustAFTERyourImgURL',
        'stringCjustAFTERyourImgURL',
    ],
    lastFound = 0,
    results = [];
    
    while (lastFound!=='finished') {
        for (var i=0; i<vuPre.length; i++) {
            var
            vuP1 = code.indexOf(vuPre[i], lastFound),
            vuP2 = code.indexOf(vuPost[i], lastFound),
            iuP1 = code.indexOf(iuPre[i], lastFound),
            iuP2 = code.indexOf(iuPost[i], lastFound),
            vidurl = (
                vuP1 === -1
                || vuP2 === -1
                ? ''
                : code.substr(vuP1 + vuP1.length, vuP2 - vuP1 - vuP1.length)
            ),
            imgurl = (
                iuP1 === -1
                || iuP2 === -1
                ? ''
                : code.substr(iuP1 + iuP1.length, iuP2 - iuP1 - iuP1.length)
            );
            if (
                vidurl !== ''
                && imgurl !== ''
            ) {
                results[results.length] = {
                    vidurl : vidurl,
                    imgurl : imgurl
                };
                break;
            }
        }
    };
    if (results.length > 0) {
        console.log ('results', results);
    } else {
        console.log ('Could not find results for code', code);
    }
    
    // example 3 : using multiple possibilities for pre and post strings to be found, where vuPre[0] can be followed by vuPost[0], vuPost[1] or vuPost[2] (and the same for vuPre[1] and vuPre[2]
    var
    vuPre = [
        'stringOneJustBeforeYourVidURL',
        'stringTwoJustBeforeYourVidURL',
        'stringThreeJustBeforeYourVidURL'
    ],
    vuPost = [
        'stringAjustAFTERyourVidURL',
        'stringBjustAFTERyourVidURL',
        'stringCjustAFTERyourVidURL',
    ],
    iuPre = [
        'stringOneJustBeforeYourImgURL',
        'stringTwoJustBeforeYourImgURL',
        'stringThreeJustBeforeYourImgURL'
    ],
    iuPost = [
        'stringAjustAFTERyourImgURL',
        'stringBjustAFTERyourImgURL',
        'stringCjustAFTERyourImgURL',
    ],
    lastFound = 0,
    results = [];
    
    while (lastFound!=='finished') {
        for (var i=0; i<vuPre.length; i++) {
            var
            vuP1 = code.indexOf(vuPre[i], lastFound);
           
           
            for (var j=0; j<vuPost.length; j++) {
                var
                vuP2 = code.indexOf(vuPost[i], lastFound),
                vidurl = (
                    vuP1 === -1
                    || vuP2 === -1
                    ? ''
                    : code.substr(vuP1 + vuP1.length, vuP2 - vuP1 - vuP1.length)
                );
                if (vidurl !== '') break;
            };
    
            var
            iuP1 = code.indexOf(iuPre[i], lastFound);
           
            for (var j=0; j<iuPost.length; j++) {
                var 
                iuP2 = code.indexOf(iuPost[i], lastFound),
                imgurl = (
                    iuP1 === -1
                    || iuP2 === -1
                    ? ''
                    : code.substr(iuP1 + iuP1.length, iuP2 - iuP1 - iuP1.length)
                );
                if (imgurl !== '') break;
            };
           
            var lastFound = (
                vidurl !== ''
                && imgurl !== ''
                ? (
                    iuP2 + iuP2.length > vuP2 + vuP2.length
                    ? iuP2 + iuP2.length
                    : vuP2 + vuP2.length
                )
                : 'finished'
            );
           
            if (
                vidurl !== ''
                && imgurl !== ''
            ) {
                results[results.length] = {
                    vidurl : vidurl,
                    imgurl : imgurl
                };
                break;
            }
        }
    }; 
    if (results.length > 0) {
        console.log ('results', results);
    } else {
        console.log ('Could not find results for code', code);
    }
    
    
    Code (markup):
    And ofcourse, porting this code to PHP should be easy. You need to look at php.net, the searchbar at the topright of that page, type in it : "substr" - that works pretty much the same as in javascript. Then look at json_encode() and json_decode (which you'd have to call with a second parameter === true to get to PHP arrays instead of PHP objects for your data when you load it in from the server's disk. You output to javascript (in index.php most likely) as such:

    If you have to manipulate $data every time it is read (unwise, not likely in this use-case):
    
    <?php
    $data = json_decode(file_get_contents($completeFilepathToJSONfileOnServerDisk), true);
    // manipulations of $data go here.
    echo '<script type="text/javascript">';
    echo 'var myListOfVideoAndImageURLs = '.json_encode($data).';';
    echo '</script>';
    ?>
    [code]
    
    Or, with another PHP notation for outputting HTML and reading the file straight from disk:
    [code]
    <?php
    // some PHP code
    ?>
    <script type="text/javascript">
    var myListOfVideoAndImageURLs = <?php readfile($completeFilepathToJSONfileOnServerDisk)?>;
    </script>
    <?php
    //some more PHP code
    ?>
    
    Code (markup):
     
    Last edited: Nov 20, 2014
    seductiveapps.com, Nov 20, 2014 IP
  5. seductiveapps.com

    seductiveapps.com Active Member

    Messages:
    200
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #5
    Oops, my first attempts at code for this scraping puzzle didnt account for multiple results in var code. The currently updated examples in my earlier posts in this thread, do.
     
    seductiveapps.com, Nov 20, 2014 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #6
    If you have to use eval, you're doing something wrong. Dangerously, badly and insecurely WRONG!

    Since you seem to have the data server-side to begin with, why the devil are you wasting time with it client-side in the first place? That doesn't make any sense either...

    From where are you extracting said URL? Without seeing what your data actually is it's really hard to answer properly... Though I have the feeling throwing endless pointlessly massive amounts of scripttardery and PHP isn't the answer.

    A simple sample of the markup you are parsing to extract the URI from and we can likely get you something proper to work with.
     
    deathshadow, Nov 20, 2014 IP