webRequest.onHeadersReceived 没有开火
webRequest.onHeadersReceived not firing
我正在尝试在 iFrame 中加载网站,但服务器正在发送 X-Frame-Options: SAMEORIGIN
header 所以我尝试使用 onHeadersReceived 修改 headers 虽然我无法正常工作。
manifest.json
{
"manifest_version": 2,
"name": "__MSG_extensionName__",
"description": "__MSG_extensionDescription__",
"default_locale": "en",
"version": "0.1",
"author": "author",
"homepage_url": "https://github.com/",
"icons": {
"48": "assets/icons/logo.png"
},
"background": {
"page": "../../background.html"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["scripts/dist/bundle.js"],
"css": ["assets/css/main.css"]
}
],
"permissions": [
"tabs",
"webRequest",
"contextMenus",
"webNavigation",
"webRequestBlocking"
],
"web_accessible_resources": [
"assets/icons/logo.png"
]
}
background.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script type="module" src="scripts/dist/contextMenu.js"></script>
<script type="module" src="scripts/dist/modifyHeaders.js"></script>
</head>
</html>
contextMenu.js
browser.contextMenus.create( {
id: "customsearch",
title: "Search",
contexts: ["selection"]
} );
// Context menu onClicked listener
browser.contextMenus.onClicked.addListener( (info, tab) => {
if (info.menuItemId = "custom-search") {
sendMessageToTab(tab, info);
}
} );
function sendMessageToTab(tab, info) {
browser.tabs.sendMessage(
tab.id,
{ query: info.selectionText }
);
}
background.js
var extraInfoSpec = ['blocking', 'responseHeaders'];
var filter = {
urls: ['<all_urls>'],
tabId: -1
};
// Bypass X-Frame-Options
browser.webRequest.onHeadersReceived.addListener(
modifyHeadersCallback,
filter,
extraInfoSpec
);
// onHeadersReceived Callback
function modifyHeadersCallback(details) {
let modifiedResponseHeaders = details.responseHeaders.filter(
header => !(header.name.toLowerCase() == 'x-frame-options' || header.name.toLowerCase() == 'content-security-policy')
);
return {responseHeaders: modifiedResponseHeaders};
};
上下文菜单按预期工作,问题出在 browser.webRequest.onHeadersReceived
侦听器上,它似乎根本没有被触发,因为我没有收到任何错误或控制台日志。
我进行了广泛的搜索并尝试了我找到的大部分解决方案,但对我的情况没有任何效果。你能发现我的方法有什么问题吗?
火狐
您只需从 filter
object 中删除 tabId: -1
:
browser.webRequest.onHeadersReceived.addListener(
modifyHeadersCallback,
{ urls: ['<all_urls>'] },
['blocking', 'responseHeaders']
);
Chrome
现代 Chrome 需要 extraInfoSpec 参数中的 extraHeaders
模式,因此 iframe 的通用代码如下所示:
browser.webRequest.onHeadersReceived.addListener(
modifyHeadersCallback,
{ urls: ['<all_urls>'], types: ['sub_frame'] },
// Modern Chrome needs 'extraHeaders' to see and change this header,
// so the following code evaluates to 'extraHeaders' only in modern Chrome.
['blocking', 'responseHeaders', chrome.webRequest.OnHeadersReceivedOptions.EXTRA_HEADERS]
.filter(Boolean)
);
当然 manifest.json 中的 "permissions"
应该包含您要处理的 URL,例如在这种情况下是 "<all_urls>"
.
所以,这要么是错误,要么是有意更改,wasn't documented yet, so if someone wants to report it please open a new issue on https://crbug.com。我猜这是故意的,因为 extraHeaders
模式意味着这个 header 是在内部网络进程中处理的,它与浏览器进程是分开的。
我正在尝试在 iFrame 中加载网站,但服务器正在发送 X-Frame-Options: SAMEORIGIN
header 所以我尝试使用 onHeadersReceived 修改 headers 虽然我无法正常工作。
manifest.json
{
"manifest_version": 2,
"name": "__MSG_extensionName__",
"description": "__MSG_extensionDescription__",
"default_locale": "en",
"version": "0.1",
"author": "author",
"homepage_url": "https://github.com/",
"icons": {
"48": "assets/icons/logo.png"
},
"background": {
"page": "../../background.html"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["scripts/dist/bundle.js"],
"css": ["assets/css/main.css"]
}
],
"permissions": [
"tabs",
"webRequest",
"contextMenus",
"webNavigation",
"webRequestBlocking"
],
"web_accessible_resources": [
"assets/icons/logo.png"
]
}
background.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script type="module" src="scripts/dist/contextMenu.js"></script>
<script type="module" src="scripts/dist/modifyHeaders.js"></script>
</head>
</html>
contextMenu.js
browser.contextMenus.create( {
id: "customsearch",
title: "Search",
contexts: ["selection"]
} );
// Context menu onClicked listener
browser.contextMenus.onClicked.addListener( (info, tab) => {
if (info.menuItemId = "custom-search") {
sendMessageToTab(tab, info);
}
} );
function sendMessageToTab(tab, info) {
browser.tabs.sendMessage(
tab.id,
{ query: info.selectionText }
);
}
background.js
var extraInfoSpec = ['blocking', 'responseHeaders'];
var filter = {
urls: ['<all_urls>'],
tabId: -1
};
// Bypass X-Frame-Options
browser.webRequest.onHeadersReceived.addListener(
modifyHeadersCallback,
filter,
extraInfoSpec
);
// onHeadersReceived Callback
function modifyHeadersCallback(details) {
let modifiedResponseHeaders = details.responseHeaders.filter(
header => !(header.name.toLowerCase() == 'x-frame-options' || header.name.toLowerCase() == 'content-security-policy')
);
return {responseHeaders: modifiedResponseHeaders};
};
上下文菜单按预期工作,问题出在 browser.webRequest.onHeadersReceived
侦听器上,它似乎根本没有被触发,因为我没有收到任何错误或控制台日志。
我进行了广泛的搜索并尝试了我找到的大部分解决方案,但对我的情况没有任何效果。你能发现我的方法有什么问题吗?
火狐
您只需从 filter
object 中删除 tabId: -1
:
browser.webRequest.onHeadersReceived.addListener(
modifyHeadersCallback,
{ urls: ['<all_urls>'] },
['blocking', 'responseHeaders']
);
Chrome
现代 Chrome 需要 extraInfoSpec 参数中的 extraHeaders
模式,因此 iframe 的通用代码如下所示:
browser.webRequest.onHeadersReceived.addListener(
modifyHeadersCallback,
{ urls: ['<all_urls>'], types: ['sub_frame'] },
// Modern Chrome needs 'extraHeaders' to see and change this header,
// so the following code evaluates to 'extraHeaders' only in modern Chrome.
['blocking', 'responseHeaders', chrome.webRequest.OnHeadersReceivedOptions.EXTRA_HEADERS]
.filter(Boolean)
);
当然 manifest.json 中的 "permissions"
应该包含您要处理的 URL,例如在这种情况下是 "<all_urls>"
.
所以,这要么是错误,要么是有意更改,wasn't documented yet, so if someone wants to report it please open a new issue on https://crbug.com。我猜这是故意的,因为 extraHeaders
模式意味着这个 header 是在内部网络进程中处理的,它与浏览器进程是分开的。