Chrome扩展程序如何基本上卷曲其他页面? [英] How can Chrome extensions basically cURL other pages?

查看:130
本文介绍了Chrome扩展程序如何基本上卷曲其他页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑编写一个Chrome扩展程序,需要在某个网站的某个动态页面上抓取几个链接并分析链接页面的内容。



实际上,我不太了解编写浏览器扩展的内容,所以我想看看在我承诺自己学习如何之前是否可行。我知道这些扩展通常会执行Javascript,但我不知道如何使用Javascript获得这种结果。

使用 jQuery 这个插件通过 James Padolsey 发出一个跨域请求;该插件将返回页面内容。然后你可以做这样的事情把它放到一个jQuery对象中:

  $。ajax({
url: 'http://example.com',
类型:'GET',
成功:函数(res){
var contents = $(res.responseText);
}
});

使用该内容对象,您可以执行任何通常使用jQuery对象执行的操作,例如 find() 。例如,如果你想获得页面的标题,你可以这样做:

  $。ajax({
url:'http://example.com',
类型:'GET',
成功:函数(res){
var contents = $(res.responseText);
var title = content.find('title').text();
}
});


I'm thinking about writing a Chrome extension that will need to, on a certain dynamic page of a certain site, grab a few links and analyze the contents of the linked pages.

I actually don't know much about writing browser extensions, so I wanted to see if it was doable before I committed myself to learning how. I do know that extensions typically execute Javascript but I am unaware of how to get that sort of result with Javascript.

解决方案

You can use jQuery and this plugin by James Padolsey to make a cross domain request; the plugin will return the page contents. You can then do something like this to get it into a jQuery object:

$.ajax({
    url: 'http://example.com',
    type: 'GET',
    success: function(res) {
        var contents = $(res.responseText);
    }
});

With that contents object you can do anything you would do normally with a jQuery object, such as find(). For instance, if you wanted to get the title of the page, you could do:

$.ajax({
    url: 'http://example.com',
    type: 'GET',
    success: function(res) {
        var contents = $(res.responseText);
        var title = contents.find('title').text();
    }
});

这篇关于Chrome扩展程序如何基本上卷曲其他页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆