How can I detect the current tab's mime type in a Google Chrome extension?

前端 未结 5 1737
长发绾君心
长发绾君心 2020-12-01 13:07

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.

5条回答
  •  伪装坚强ぢ
    2020-12-01 13:38

    You can evaluate the property document.contentType on the current tab. Here is an example on browserAction :

    chrome.browserAction.onClicked.addListener(() => {
        chrome.tabs.getSelected((tab) => {
            chrome.tabs.executeScript(tab.id, { code: 'document.contentType' }, ([ mimeType ]) => {
                alert(mimeType);
            });
        })
    });
    

    This property returns the MIME type that the document is being rendered as, not the Content-Type header (no information about the charset).

提交回复
热议问题