Skip to content Skip to sidebar Skip to footer

Getting Snippet Of Text From External Page Into Google Chrome Extension

I'm try to develop a simple google chrome extension. What it does it that it displays the number of downloads on a page (the count) and display it in a popup. The page in questio

Solution 1:

If you only want to display the number of downloads in a browser_action then you shouldnt need a background page, but if you wanted to monitor the amount of downloads every minute or so then you would. To get the download count you could use XMLHttpRequest to download the pages source, then parse it to a dom using.....

var page = document.implementation.createHTMLDocument("");
page.documentElement.innerHTML = pageSource;  // assuming the source for the page you downloaded with httprequest is in pageSource

...and then you could use something like querySelector to get the text of the bit of the page your intesersted in, something like.....

downloadstats = page.querySelector('.downloadstats').textContent.trim();

Hopefully that'll get you started.

Here's a simple (no error checking or what not) example that gets your rep points from stackoverflow.....

var xmlhttp = newXMLHttpRequest();
xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4) {
        var page = document.implementation.createHTMLDocument("");
        page.documentElement.innerHTML = this.responseText;
        var score = page.querySelector('[title^="your reputation;"]').textContent.trim();
        alert('You have ' + score + ' rep points.');
    }
}
xmlhttp.open("GET", "http://stackoverflow.com", true);
xmlhttp.send();

Post a Comment for "Getting Snippet Of Text From External Page Into Google Chrome Extension"