当Chrome在后台运行时动态网站更新的Chrome桌面通知 [英] Chrome desktop notification for dynamic site update when chrome runs in background

查看:240
本文介绍了当Chrome在后台运行时动态网站更新的Chrome桌面通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为社区网站创建了书签扩展,这些主持人帖子,我需要在社区网站上发布新帖子的桌面通知。 Chrome会在后台运行,但除非通过桌面通知通知,否则不会打开任何选项卡。

想法是定期从后台页面向内容脚本发送消息,并获取有关帖子的更新并显示桌面通知,从而用户在看到通知时打开更新。

查询:
如何访问网站的DOM(当Chrome在后台运行时),从用户眼中隐藏提取过程。



感谢!!



编辑

  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(){//等待加载
statone = $('#widgetPanel-7qrSN8CAwqKk0DP2_GQK')。text();
console.log(statone);
alert(statone);
},10000 );
}
}
xhr.send();

我无法在上面的代码中获取文本值,而是我的警报框为空。源网站不提供xml或Json文件,只需要提取网站上的第一个内容或更新即可。

您需要使用 XMLHttpRequest 中的响应。执行 XMLHttpRequest 请求不会将请求资源加载到页面中。



在jQuery中,您可以执行此操作像这样:
$ b $

  $。get(https://www.dsswym.3ds.com/,function(data) {
//将响应文本加载到新的页面元素中
var fakePage = document.createElement(html);
fakePage.innerHTML = data;

/ /在新的页面元素中找到所需元素
var statone = $(fakePage).find('#widgetPanel-7qrSN8CAwqKk0DP2_GQK')。text();
console.log(statone);
}
});

这将Ajax提取的结果存储到 innerHTML < code>< html>< / code>元素,其中文本被解析为新的DOM。然后,我们使用jQuery的 .find 方法来获取我们需要的DOM元素。


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.

解决方案

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.

这篇关于当Chrome在后台运行时动态网站更新的Chrome桌面通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆