在JavaScript中,是否有可能以任何方式访问HTML5音频源的HTTP响应头(以便我可以读取它们)?
<audio id="myaudio" controls>
<source src="/test.mp3" type="audio/mpeg">
</audio>
编辑2:
似乎可以使用服务工作者(service workers)来访问头信息。我尝试了以下操作:
if ('serviceWorker' in navigator) {
console.log('CLIENT: service worker registration in progress.');
navigator.serviceWorker.register('/service-worker.js').then(function(){
console.log('CLIENT: service worker registration complete.');
}, function() {
console.log('CLIENT: service worker registration failure.');
});
} else {
console.log('CLIENT: service worker is not supported.');
}
我的 service-worker.js 看起来是这样的(仅作为示例!):
self.addEventListener('fetch', function(event) {
console.log("SENDING REQUEST: " + event.request.url);
if(event.request.url === 'https://localhost/test.mp3') {
event.respondWith(
fetch(event.request.url, {
method: "GET",
headers: {
"Authorization": "myToken",
},
redirect: "follow"
}).then(function(response) {
//check for response header here...
return response;
})
);
}
});
我的问题是,我无法使用Firefox 70来调试代码,这使得测试变得更加困难,我也不能得出最终答案...
loadeddata
或loadedmetadata
事件?在你的示例中,self
是什么? - SpoonMeiser