我想在我的擴展中實現chrome.webRequest API,但由於某種原因,無論我做什麼,它都無法正常工作。有人可以發佈使用示例嗎?或糾正我的錯誤?基本上我試圖做的是從響應中截獲接收到的頭文件。chrome.webRequest不能正常工作?
這是onBeforeSendHeaders實現,但我想用OnHeadersRecieved以及 :
var requestFilter = {
urls: [ "<all_urls>" ]
},
// The 'extraInfoSpec' parameter modifies how Chrome calls your
// listener function. 'requestHeaders' ensures that the 'details'
// object has a key called 'requestHeaders' containing the headers,
// and 'blocking' ensures that the object your function returns is
// used to overwrite the headers
extraInfoSpec = ['requestHeaders','blocking'],
// Chrome will call your listener function in response to every
// HTTP request
handler = function(details) {
alert(details);
var headers = details.requestHeaders,
blockingResponse = {};
// Each header parameter is stored in an array. Since Chrome
// makes no guarantee about the contents/order of this array,
// you'll have to iterate through it to find for the
// 'User-Agent' element
for(var i = 0, l = headers.length; i < l; ++i) {
if(headers[i].name == 'User-Agent') {
headers[i].value = '>>> Your new user agent string here <<<';
break;
}
// If you want to modify other headers, this is the place to
// do it. Either remove the 'break;' statement and add in more
// conditionals or use a 'switch' statement on 'headers[i].name'
}
blockingResponse.requestHeaders = headers;
return blockingResponse;
};
chrome.webRequest.onBeforeSendHeaders.addListener(handler, requestFilter, extraInfoSpec);
這是我的清單文件:
{
"background_page": "iRBackground.html",
"browser_action": {
"default_icon": "Off.png",
"popup": "iRMenu.html"
},
"content_scripts": [ {
"js": [ "Content.js" ],
"matches": [ "http://*/*" ],
"run_at": "document_start"
} ],
"description": "***",
"icons": {
"128": "On128x128.png",
"16": "On.png",
"48": "On48x48.png"
},
"key": "****",
"manifest_version": 2,
"name": "***",
"permissions": [ "tabs", "notifications", "unlimitedStorage", "webRequest", 「webRequestBlocking」, 「<all_urls>」],
"update_url": "***/Chrome/UpdateVersion.xml",
"version": "1.3"
}
我從Chrome中得到的錯誤是: Uncaught TypeError: Cannot read property 'onBeforeSendHeaders' of undefined
任何人都可以看到任何錯誤?謝謝
'webRequest' API僅適用於擴展程序的進程(例如後臺/活動頁面)。 – 2013-03-19 15:07:52
這段代碼寫在我的背景頁面中.. – user1326293 2013-03-19 15:16:09
請給我們完整的清單 – 2013-03-19 16:10:32