简体   繁体   English

通过链接进行JavaScript搜索

[英]Javascript search through links

I know from this that I can search an entire html page using 我从知道这个 ,我可以使用搜索整个html页面

var position = document.documentElement.innerHTML.indexOf('dog');

However, I have a webpage with a ton of links and I want to make a simple script to visit and search through every linked page. 但是,我有一个包含大量链接的网页,并且我想制作一个简单的脚本来访问和搜索每个链接的页面。 Is that possible? 那可能吗?

Because of same-origin policy and other challenges, this is not presently possible to do solely on the browser. 由于同源策略和其他挑战,目前仅在浏览器上无法做到这一点。 However, there are server-side JavaScript projects using node.js that make this relatively straight-forward to do on a server or on your desktop (but not on your browser). 但是,有一些使用node.js的服务器端JavaScript项目,这使得在服务器或台式机(而不是浏览器)上这样做相对简单。 Because it's nodejs, you can use the same kind of JavaScript (even jquery selectors) as you would in the browser. 由于它是nodejs,因此您可以使用与浏览器相同的JavaScript(甚至是jquery选择器)。 One nice example is node.io, although it's primarily built for use on Linux or OSX: 一个很好的例子是node.io,尽管它主要是为在Linux或OSX上使用而构建的:

https://github.com/chriso/node.io https://github.com/chriso/node.io

For a more WIndows-friendly approach, you could use nodejs along with Request ( https://github.com/mikeal/request ) and Cheerio, or else jsdom. 对于Windows友好的方法,您可以将Node.js与Request( https://github.com/mikeal/request )和Cheerio或jsdom一起使用。 There are a number of tutorials out there that use this approach (the first tutorial even extracts a list of URLs from a page): 有许多使用这种方法的教程(第一个教程甚至从页面中提取URL列表):

If you need to work with browsers, you can originate the request in the browser, run it in the server, and then easily send the results to the browser via Ajax. 如果需要使用浏览器,则可以在浏览器中发起请求,在服务器中运行请求,然后通过Ajax轻松将结果发送到浏览器。 But you'll need a server intermediary. 但是您需要服务器中介。

If you must do this in a browser, then you'll have to build it as some kind of browser extension or add-on. 如果必须在浏览器中执行此操作,则必须将其构建为某种浏览器扩展或附加组件。

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

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