问题
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