Gomathi Sankar
Gomathi Sankar

Reputation: 217

Chrome desktop notification for dynamic site update when chrome runs in background

I've created a bookmark extension for community website, which host posts, I need to have desktop notification of new posts in community site. chrome runs on background, but no tabs will be opened, unless notified by desktop notification.

Idea is to send a message from background page to content script for regular interval and get updates on the post and display a desktop notification, thereby user opens the update on seeing notification.

Query: How to access the DOM of website (when chrome is running in background), hiding extraction process from the user eye.

Thanks !!

EDIT

var statone;
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://www.dsswym.3ds.com/", true);
xhr.onreadystatechange = function() {
 if (xhr.readyState == 4 && xhr.status==200) {
    setTimeout(function(){ //wait for the webpage loading 
        statone = $('#widgetPanel-7qrSN8CAwqKk0DP2_GQK').text();
        console.log(statone);
        alert(statone); 
     }, 10000);
  }
}
xhr.send();

I'm not able to get text values on above code, instead my alert box is empty. Source site doesn't provide xml or Json file, just i need to extract the first content or update on the site.

Upvotes: 1

Views: 1038

Answers (1)

apsillers
apsillers

Reputation: 115980

You need to use the response from the XMLHttpRequest. Doing an XMLHttpRequest request doesn't load the request resource into the page.

In jQuery, you can do it like this:

$.get("https://www.dsswym.3ds.com/", function(data) {
        // load response text into a new page element
        var fakePage = document.createElement("html");
        fakePage.innerHTML = data;

        // find the desired element within the new page element
        var statone = $(fakePage).find('#widgetPanel-7qrSN8CAwqKk0DP2_GQK').text();
        console.log(statone);
    }
});

This stores the result of the Ajax fetch into the innerHTML of a new <html> element, which cases the text to be parsed into a new DOM. We then use jQuery's .find method to get the element we need out of that DOM.

Upvotes: 1

Related Questions