kod*_*ire 1 javascript google-chrome google-chrome-extension x-frame-options chrome-extension-manifest-v3
我正在制作一个 chrome 扩展,其中有一个 iframe。当扩展程序请求服务器以获取页面时,它会返回错误Refused to display 'https://subdomain.example.com/' in a frame because it set 'X-Frame-Options' to 'deny'。尽管我已在我的文件中设置了x-frame-optionsto并在后端项目的特定方法中添加了 a ,但它返回了另一个错误。我在我的文件中添加了和。不走运,它返回了,所以我删除了权限并添加了v3 的权限。没有结果!!然后我添加了deny.htaccessheader('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'webRequestwebRequestBlockingpermissionsmanifest.json'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.webRequestBlockingdeclarativeNetRequest
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)
);
Run Code Online (Sandbox Code Playgroud)
我的script.js,它回来了Uncaught TypeError: Cannot read properties of undefined (reading 'onHeadersReceived')
我应该怎么做才能只允许扩展程序向服务器请求?
正如错误消息所示,一种解决方案是使用"manifest_version": 2and "webRequestBlocking"in "permissions"。
另一个解决方案是declarativeNetRequest,它是一个具有完全不同语法的新 API,因此您必须完全重写代码,这是一个示例:link。