Mike
Mike

Reputation: 11

Javascript: Grab link from external webpage source and then place it in the specified location

I need to create a JScript that does the following actions:

1- Grab a link from an external webpage's source code

2- Remove unecessary string from the link, if needed

3- Place that link in the specified entry

Now explained:

I have a video hosted in another website and i want to make it play in my own, the problem is that the link to that video changes after 12/24h and i have to update it all the time.

The webpage that provides the file has a permanent url so the link to the page that has the file never changes but the link to the video does.

I need a JScript that could recover the updated link from the specified webpage and place it inside the src field, something like a source code crawler until it find the match to the video name, like so:

The video is called video.mp4 and it allways has the same string after it (ex. &download=yes)

Upon the match of the string 'video.mp4&download=yes', it would retrieve the full path to the file, from "..." to "...video.mp4", ignoring the &download=yes because the player cant play with that last string included.

After that is done, it would place the link in the specific "src=" field making it possible to be played in the video player.

Also, thanks in advance for the time spent, if possible try to make it easy for me to understand since im new in Jcript.

Upvotes: 1

Views: 575

Answers (2)

Andreas Köberle
Andreas Köberle

Reputation: 110922

As all the comments say'ed this cant be done by simple JavaScript BUT you could use YQL to query the page and get back the result as JSNOP.

YQL is an service by Yahoo that scans a side for you. Also you can pass in a javascript file to query the result html with xpath. All this will be done by Yahoo on their servers. At the end you can import the result on your page as JSONP.

So this is an example in the YQL console to query a specific link type from yahoo finance side:

http://developer.yahoo.com/yql/console/#h=select%20*%20from%20html%20where%20url%3D%22http%3A//finance.yahoo.com/q%3Fs%3Dyhoo%22%20and%20xpath%3D%27//div%5B@id%3D%22yfi_headlines%22%5D/div%5B2%5D/ul/li/a%27

the query will give you a jsonp string:

http://developer.yahoo.com/yql/console/#h=select%20*%20from%20html%20where%20url%3D%22http%3A//finance.yahoo.com/q%3Fs%3Dyhoo%22%20and%20xpath%3D%27//div%5B@id%3D%22yfi_headlines%22%5D/div%5B2%5D/ul/li/a%27

On your side you have to create an new script and insert it in your DOM. As you can see in the result jsonp it will call a function "cbfunc" with json as parameter. All you need is function "cbfunc" on your side to process the json

Upvotes: 1

xj9
xj9

Reputation: 3471

This will only be possible with JavaScript if the video page and yours are on the same domain (though you may be able to employ some tricks to make it happen). The best way, as I see it, would be to do some server-side scripting to download the page (which would be text only, so you wont be using much bandwidth) and to do some DOM manipulation with whatever HTML lib is available for your server language.

Upvotes: 0

Related Questions