开发者

Loading a tab in the background and getting some information out of it

Hello Chrome Extension geeks,

Is there any way that we can read a the information on a webpage opening it in background(i.e. without opening a separate tab for it).

Suppose, I'm on Site A and i've a list of urls(say site B,C,D) listed on site A. I've to find the first url(within B,C,D) having certain开发者_JS百科 element with known tagID. So i should linearly open the urls and check for the existence of the element on the web page. if B has the required element on it i should stop the iteration or should have a note of it.

I donno if this can be done with ajax. Need help. thanks in advance. lemme know if anything is vague.


Yes this can be done with ajax. Background page doesn't have cross-domain limitations (as long as you declare corresponding rules in the manifest), so you can load any site through ajax, do the search (jQuery would be perfect here), and send results back to the content script if needed.

Ajax request in jQuery:

$.ajax({
    url: "http://google.com",
    type: "GET",
    dataType: "html",
    error: function() {
        //error happened
    },
    success: function(html) {

        //"html" var contains full page source as string

        //you can search it using jQuery
        var el = $(html).find("#prm");
        if(el.length) {
            console.log("Element with id 'prm' exists");
        }


    }
});

Manifest that allows sending ajax requests to all sites:

"permissions": [
    "http://*/*", "https://*/*"
],
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜