I want to see if the current tab is a PDF file from a background page.
I can check the url for .pdf at the end but there are some PDF files that don't have that.
You can't get it using current Chrome API afaik. What you can do is load this page again through XHR and check returned content-type header. Something like this:
background html:
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if(changeInfo.status == "loading") {
if(checkIfUrlHasPdfExtension(tab.url)) {
//.pdf
pdfDetected(tab);
} else {
var xhr = new XMLHttpRequest();
xhr.open("GET", tab.url, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
var contentType = xhr.getResponseHeader("Content-Type");
if(checkIfContentTypeIsPdf(contentType)) {
pdfDetected(tab);
}
}
}
xhr.send();
}
}
});
manifest.json:
"permissions": [
"tabs", "http://*/*", "https://*/*"
]
For PDF files returned content type should be application/pdf
. Something to keep in mind though is that content-type header could contain encoding as well: text/html; charset=UTF-8
.