【发布时间】: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 文件中将webRequest 和webRequestBlocking 添加到permissions。没有运气,它返回了'webRequestBlocking' requires manifest version of 2 or lower 和Unchecked 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