简体   繁体   English

谷歌chrome扩展程序http请求监视选项卡

[英]google chrome extension http request monitoring for tab

I am working on google chrome extension application. 我正在研究Google Chrome扩展程序。 I want, as soon as user's http request on browser's tab finished it give some alert. 我想要的是,一旦用户在浏览器选项卡上的http请求完成,它就会发出警报。 Means as browser finishes its request app could be able to listen it. 意味着当浏览器完成其请求应用程序后便可以收听它。

Right now i am using below code. 现在我正在使用下面的代码。 It reads tab url after request finished. 请求完成后,它将读取选项卡URL。

chrome.webRequest.onCompleted.addListener(function() {
    var mydiv = document.createElement('div');  
        chrome.tabs.getSelected(null,function(tab) {
        mydiv.innerHTML = tab.url;      
    });         
    document.body.appendChild(mydiv);
});

But it's not working for me. 但这对我不起作用。 Any help? 有什么帮助吗?

You are probably running the code from your background page (or else the chrome.webRequest API won't be available. From there (background page) it is not possible to interact with a webpages DOM. 您可能正在从后台页面运行代码(否则chrome.webRequest API将不可用。从那里(后台页面)无法与网页DOM进行交互。

You can achieve what you want like this: 您可以这样实现您想要的:

Solution A 解决方案A

  1. Have a content script injected into the webpage at "document_end" or "document_idle" . 内容脚本注入到网页中的“ document_end”或“ document_idle”处

  2. The content script (once injected) will create your div and add it to the DOM. 内容脚本(一旦注入)将创建您的div并将其添加到DOM。

Solution B 解决方案B

  1. Have your background page listen for page load. 让您的背景页面监听页面加载。 BTW, chrome.tabs.onUpdated might be better suited for the task. 顺便说一句, chrome.tabs.onUpdated可能更适合该任务。 Eg: 例如:

    \nchrome.tabs.onUpdated.addListener(function(tabId, info, tab) { chrome.tabs.onUpdated.addListener(function(tabId,info,tab){  \n    if (info.status && (info.status == "complete")) { 如果(info.status &&(info.status ==“ complete”)){\n        // The page is loaded, so inject a content script //页面已加载,因此请插入内容脚本\n    } }\n}); });\n
  2. Upon page load programmatically inject a content script. 在页面加载时,以编程方式注入内容脚本。

  3. The content script (once injected) will create your div and add it to the DOM. 内容脚本(一旦注入)将创建您的div并将其添加到DOM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM