How can Chrome extensions basically cURL other pages?

家住魔仙堡 提交于 2020-01-15 03:09:09

问题


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.


回答1:


Use jquery ajax to fetch the content of other pages.




回答2:


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();
    }
});


来源:https://stackoverflow.com/questions/10769924/how-can-chrome-extensions-basically-curl-other-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!