我有以下代码尝试确保在发出请求时设置了Referer标头:
chrome.webRequest.onBeforeSendHeaders.addListener(function (info) {
var refererRequestHeader = _.find(info.requestHeaders, function (requestHeader) {
return requestHeader.name === 'Referer';
});
var refererUrl = info.url.substring(0, info.url.indexOf('/embed/'));
if (_.isUndefined(refererRequestHeader)) {
info.requestHeaders.push({
name: 'Referer',
value: refererUrl
});
} else {
refererRequestHeader.value = refererUrl;
}
return { requestHeaders: info.requestHeaders };
}, {
// Match on my specific iframe or else else this logic can leak into outside webpages and corrupt other YouTube embeds.
urls: ['*://*.youtube.com/embed/?enablejsapi=1&origin=chrome-extension:\\\\jbnkffmindojffecdhbbmekbmkkfpmjd']
},
['blocking', 'requestHeaders']
);
此代码在99%的时间内都很有效。但是,在某些PC上,它会失败。我试图调查为什么会发生这种情况,但我没有很多线索继续下去。
我的问题是: