简体   繁体   English

Javascript:从外部网页来源获取链接,然后将其放置在指定位置

[英]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: 我需要创建一个执行以下操作的JScript:

1- Grab a link from an external webpage's source code 1-从外部网页的源代码中获取链接

2- Remove unecessary string from the link, if needed 2-如果需要,从链接中删除不必要的字符串

3- Place that link in the specified entry 3-将该链接放在指定的条目中

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. 我在另一个网站上托管了一个视频,我想让它自己播放,问题是该视频的链接在12 / 24h之后更改,因此我必须一直对其进行更新。

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. 提供该文件的网页具有一个永久的url,因此指向该文件的页面的链接不会更改,而指向视频的链接却不会更改。

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: 我需要一个JScript,它可以从指定的网页中恢复更新的链接,并将其放置在src字段中,就像源代码搜寻器一样,直到找到与视频名称匹配的对象为止,如下所示:

The video is called video.mp4 and it allways has the same string after it (ex. &download=yes) 该视频称为video.mp4,并且始终带有相同的字符串(例如&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. 匹配字符串“ video.mp4&download = yes”后,它将检索文件的完整路径,从“ ...”到“ ... video.mp4”,而忽略&download = yes,因为播放器无法播放包括最后一个字符串。

After that is done, it would place the link in the specific "src=" field making it possible to be played in the video player. 完成此操作后,会将链接放置在特定的“ src =“字段中,从而可以在视频播放器中播放该链接。

Also, thanks in advance for the time spent, if possible try to make it easy for me to understand since im new in Jcript. 另外,在此先感谢您所花费的时间,如果可能的话,请尝试让我更容易理解,因为我是Jcript中的新人。

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. 正如所有评论所说,这无法通过简单的JavaScript BUT完成,您可以使用YQL来查询页面并以JSNOP的形式返回结果。

YQL is an service by Yahoo that scans a side for you. YQL是Yahoo提供的一项服务,可为您扫描一面。 Also you can pass in a javascript file to query the result html with xpath. 您也可以传入一个javascript文件,以xpath查询结果html。 All this will be done by Yahoo on their servers. 所有这些将由Yahoo在其服务器上完成。 At the end you can import the result on your page as JSONP . 最后,您可以将结果作为JSONP导入页面上。

So this is an example in the YQL console to query a specific link type from yahoo finance side: 因此,这是在YQL控制台中从yahoo财务方面查询特定链接类型的示例

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 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: 该查询将为您提供一个jsonp字符串:

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 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. 在您这一边,您必须创建一个新脚本并将其插入到DOM中。 As you can see in the result jsonp it will call a function "cbfunc" with json as parameter. 正如您在结果jsonp中看到的那样,它将以json作为参数调用函数“ cbfunc”。 All you need is function "cbfunc" on your side to process the json 您需要做的只是在您身边执行函数“ cbfunc”

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). 如果您的视频页面和您的视频页面位于同一个域中,则只有使用JavaScript才能实现(尽管您可以运用一些技巧来实现它)。 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. 如我所见,最好的方法是做一些服务器端脚本来下载页面(该页面只能是文本,这样就不会占用太多带宽),并使用任何可用的HTML库进行一些DOM操作。您的服务器语言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM