【问题标题】:What to do with the permissions in chrome extension based on iframe?如何处理基于 iframe 的 chrome 扩展中的权限?
【发布时间】:2021-11-09 14:18:46
【问题描述】:

我正在制作一个包含 iframe 的 chrome 扩展程序。当扩展请求服务器以获取页面时,它返回错误Refused to display 'https://subdomain.example.com/' in a frame because it set 'X-Frame-Options' to 'deny'。尽管我在我的.htaccess 文件中将x-frame-options 设置为deny,并在我的后端项目的特定方法中添加了header('x-frame-options: GOFORIT'),但它返回了另一个错误Refused to display 'https://subdomain.example.com/' in a frame because it set multiple 'X-Frame-Options' headers with conflicting values ('GOFORIT, DENY'). Falling back to 'deny'。我在manifest.json 文件中将webRequestwebRequestBlocking 添加到permissions。没有运气,它返回了'webRequestBlocking' requires manifest version of 2 or lowerUnchecked runtime.lastError: You do not have permission to use blocking webRequest listeners. Be sure to declare the webRequestBlocking permission in your manifest. 所以我从权限中删除了webRequestBlocking 并添加了declarativeNetRequest,因为它是用于v3。没有结果!!然后我加了

chrome.webRequest.onHeadersReceived.addListener(
    function(info) {
        var headers = info.responseHeaders;
        for (var i=headers.length-1; i>=0; --i) {
            var header = headers[i].name.toLowerCase();
            if (header == 'x-frame-options' || header == 'frame-options') {
                headers.splice(i, 1); // Remove header
            }
        }
        return {responseHeaders: headers};
    }, {
        urls: [
            '*://*/*', // Pattern to match all http(s) pages
            // '*://*.example.org/*', // Pattern to match one http(s) site
        ], 
        types: [ 'sub_frame' ]
    }, [
        'blocking',
        'responseHeaders',
        // Modern Chrome needs 'extraHeaders' to see and change this header,
        // so the following code evaluates to 'extraHeaders' only in modern Chrome.
        chrome.webRequest.OnHeadersReceivedOptions.EXTRA_HEADERS,
    ].filter(Boolean)
);

到我的script.js,它返回了Uncaught TypeError: Cannot read properties of undefined (reading 'onHeadersReceived')

我应该怎么做才能只允许扩展向服务器请求?

【问题讨论】:

    标签: javascript google-chrome google-chrome-extension x-frame-options chrome-extension-manifest-v3


    【解决方案1】:

    正如错误消息所述,一种解决方案是在"permissions" 中使用"manifest_version": 2"webRequestBlocking"

    另一个解决方案是declarativeNetRequest,这是一个具有完全不同语法的新 API,因此您必须完全重写您的代码,下面是一个示例:link

    【讨论】:

    • 我已经说过了,你说的那些我都设置在这里了,没有结果!
    • 不,您使用的是 manifest_version 3,这就是显示错误的原因,但您应该使用 2。
    • 我的意思是declarativeNetRequest在版本3中不起作用。如果我将版本更改为2,我应该更改manifest.json的代码还是你的意思是script.js
    • 我的回答描述了两种解决方案。第一个很简单。第二个要求你重写所有内容。
    • 这是第二种解决方案的示例:link
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-18
    • 1970-01-01
    • 1970-01-01
    • 2020-06-26
    • 1970-01-01
    • 2016-06-04
    • 1970-01-01
    相关资源
    最近更新 更多