Anatoly
Anatoly

Reputation: 5241

Can I create a html page in Chrome extension before it's loaded?

I need to intercept the very first request to specific server, let it be http://google.com and instead of render it inside root of the current tab create a page with two iframes, in the first iframe will be rendered site originally requested, http://google.com, in second one will be rendered lets say http://bing.com.

I need something like this:

<!DOCTYPE html>
<html>
<body>

<iframe src="http://www.google.com">
  <p>Your browser does not support iframes.</p>
</iframe>
<iframe src="http://www.bing.com">
  <p>Your browser does not support iframes.</p>
</iframe>

</body>
</html>

Actually I know how to intercept the very first request:

chrome.webRequest.onBeforeRequest.addListener(
    function(details)
    {
        console.log(details.requestBody);
    },
    {urls: ["*://google/*"]},
    ['requestBody']
);

but what to do next?

Upvotes: 0

Views: 194

Answers (1)

Haibara Ai
Haibara Ai

Reputation: 10897

Chrome extension can't directly modify http response body, you may want to use some workarounds, for example, redirect this request to a predefined html template.

chrome.webRequest.onBeforeRequest.addListener(
    function(details)
    {
        return { redirectUrl: chrome.runtime.getURL('template.html')};
    },
    {urls: ["*://www.baidu.com/*"]},
    ['blocking']
);

Upvotes: 2

Related Questions