【问题标题】:Can I create a html page in Chrome extension before it's loaded?我可以在加载之前在 Chrome 扩展程序中创建一个 html 页面吗?
【发布时间】:2017-01-31 20:51:06
【问题描述】:

我需要拦截对特定服务器的第一个请求,让它成为http://google.com,而不是在当前选项卡的根目录中渲染它,而是创建一个包含两个iframes的页面,在第一个iframe将被渲染最初请求的站点http://google.com,在第二个中将呈现让我们说http://bing.com

我需要这样的东西:

<!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>

其实我知道如何拦截第一个请求:

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

但是接下来要做什么呢?

【问题讨论】:

    标签: jquery html google-chrome iframe google-chrome-extension


    【解决方案1】:

    Chrome 扩展 can't directly modify http response body,您可能需要使用一些变通方法,例如,将此请求重定向到预定义的 html 模板。

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

    【讨论】:

    • 这个页面应该托管在其他地方还是可以捆绑在扩展中?
    • @Anatoly,取决于您的需要,我的示例是假设模板与扩展程序捆绑在一起。
    猜你喜欢
    • 2012-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-21
    • 1970-01-01
    • 2015-08-02
    • 1970-01-01
    相关资源
    最近更新 更多