Chrome插件与我的服务器交互



它很多天都在阅读数百种方法来帮助我制作我真正需要的东西。根本没有成功。

我需要的是这个:

1)有一个按钮,该按钮仅在选项卡具有特定URL时才有效。

2)点击它后,必须读取页面的源代码,然后获取其中的一些片段以将它们发送到我的服务器页面,以便检查我的数据库是否有记录计数(我假设使用AJAX和javascript)。然后,此页面应将其响应发送回扩展并填充弹出的html。

我知道看起来很容易,但如果不是扩展所需的代码,请我需要工作流程。

非常感谢!

好的,

所以你可以chceck选定的选项卡,它的网址是:

chrome.tabs.getSelected(null,function(tab) {
  workWithUrl(tab.url);
});
...
function workWithUrl(url){
  if (url == ...
     ...
}

为了能够对此进行修改,您需要为"选项卡"添加权限

要处理页面源代码,请将其发送到 Web 服务并更改弹出窗口.html:

var xhr = new XMLHttpRequest();
xhr.open("POST", "__server adress___", true);
//headers   
xhr.setRequestHeader("Content-Type", "application/json");
//response 
xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
    //response from service to popup.html
    document.body.innerHTML = xhr.responseText;
    }
}
//process page here 
xhr.send(pageText);

您还必须添加服务器地址清单的权限,并且所有内容都应从弹出窗口.js(或html)执行。

最新更新