简体   繁体   English

Chrome 扩展使用 POST 请求重定向到本地页面

[英]Chrome Extension Redirect to Local Page with POST Request

I am building a browser extension where I try to redirect a browser request to my page with a fair amount of data that will be contained in a JSON array returned from a service I call based on the requested URL.我正在构建一个浏览器扩展,我尝试将浏览器请求重定向到我的页面,其中包含大量数据,这些数据将包含在我根据请求的 URL 调用的服务返回的 JSON 数组中。 For example, if the user goes to example.com, I have a chrome.webRequest.onBeforeRequest.addListener that intercepts it based on the urls filter, invokes a AWS API Gateway endpoint that responds with a list of things in a JSON object.例如,如果用户访问 example.com,我有一个chrome.webRequest.onBeforeRequest.addListener根据urls过滤器拦截它,调用 AWS API Gateway 端点,该端点以 JSON 对象中的事物列表进行响应。 I need my local page to display that list of things in a local page.我需要我的本地页面在本地页面中显示该列表。

I can show some small things by invoking the local page with URL parameters (and then treat it like a GET request), but this won't work for the amount of data i want to pass.我可以通过使用 URL 参数调用本地页面来显示一些小东西(然后将其视为 GET 请求),但这不适用于我想要传递的数据量。 How do I create a POST request for the redirect URL?如何为redirect URL 创建 POST 请求?

OR, is there some other pattern I should be using to do this?或者,我应该使用其他一些模式来做到这一点吗?

listener code:监听器代码:

chrome.webRequest.onBeforeRequest.addListener(
    function(details) {
        if(swenToggle) {
            return { redirectUrl: chrome.extension.getURL("markup/simple.html?r=n&url=" + details.url) }; // This is fine for simple data
        } else { 
            //get the base URL 
            var baseUrl = new URL(details.url).hostname.split(".").slice(-2).join(".")
            var apiUrl = // Call AWS API here
            fetch(apiUrl).then(r => r.json()).then(result => {
                console.log('RESULT : ' + result.body); // I need to pass this body to the redirectUrl below...
            })
            return { redirectUrl: chrome.extension.getURL("markup/complex.html?sourceUrl=" + baseUrl) };
        }
    },
    { urls: "example.com"},  
    ["blocking"]
);

Solved it with a slightly different approach.用稍微不同的方法解决了它。 Instead of calling the AWS API from background.js, I pass the parameters needed for the API call to complex.html as URL params, and then invoke the AWS API in complex.html .我没有从 background.js 调用 AWS API,而是将 API 调用所需的参数作为 URL 参数传递给complex.html ,然后在complex.html调用 AWS API。

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

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