Skip to content
Advertisement

how to get the URL of a PDF file from a google chrome extension

Last year I developed a google extension that worked on PDF files. I used the following function in order to get the URL of the PDF file:

function getPDFUrl(): String {
    const e = document.body.firstElementChild;
    if (e.id != "plugin" || e.type != "application/pdf" || e.src == undefined)
        throw new Error("This does not look like a PDF document");
    return e.src;
}

Now, the latest version of Google Chrome does not provide the src attribue anymore.

<html>
  <body style="height: 100%; width: 100%; overflow: hidden; margin:0px; background-color: rgb(82, 86, 89);">
    <embed style="position:absolute; left: 0; top: 0;" width="100%" height="100%" src="about:blank" type="application/pdf" internalid="3568AA495C01C5F2079A85384CEE54EE">
  </body>
</html>

How can I get the URL of the PDF file with the latest version of Google Chrome?

Advertisement

Answer

Apparently PDF is now viewed through an internal chrome extension chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/.

Aside from using window.location.href you can use document.querySelector("embed").baseURI

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement