简体   繁体   English

Chrome扩展程序-如何获取从网页调用的POST请求用户的响应?

[英]Chrome extension - how to get response of POST request user invoked from a webpage?

I am trying to create a chrome extension that navigates to a webpage, lets the user click a button on the webpage that sends an asynchronous post request, and to read that response and use it in the extension. 我正在尝试创建一个chrome扩展程序,该扩展程序导航到网页,让用户单击网页上发送异步发布请求的按钮,并读取该响应并在扩展程序中使用它。

Everything that I am finding from my research is telling me to create the request in the extension itself, which I do not want to do, because I need the web page to make the request itself. 我从研究中发现的所有内容都告诉我要在扩展本身中创建请求,而我不想这样做,因为我需要Web页面来自行创建请求。

Is there a way to listen to a post request on the page itself on my background script? 有没有办法在后台脚本上侦听页面本身上的发布请求?

You can use chrome.webRequest and onBeforeRequest to fire when a request is about to occur chrome.webRequest.onBeforeRequest.addListener(function callback) . 您可以使用chrome.webRequestonBeforeRequest在即将发生请求时chrome.webRequest.onBeforeRequest.addListener(function callback) Parameters contains the HTTP request data. 参数包含HTTP请求数据。 You also need to supply an extraInfoSpec of requestBody to the listener. 您还需要向侦听器提供requestBody的extraInfoSpec。

Below is a sample code snippet how to use onBeforeRequest: 以下是如何使用onBeforeRequest的示例代码片段:

const WEB_REQUEST = chrome.webRequest;

WEB_REQUEST.onBeforeRequest.addListener(
function(details) {
if(details.method == "POST")
console.log(JSON.stringify(details));
},
);

For more information regarding POST request, try to read the Chrome Extensions developer guide: https://developer.chrome.com/extensions/getstarted 有关POST请求的更多信息,请尝试阅读Ch​​rome扩展程序开发人员指南: https : //developer.chrome.com/extensions/getstarted

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

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