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:
JavaScript
x
7
1
function getPDFUrl(): String {
2
const e = document.body.firstElementChild;
3
if (e.id != "plugin" || e.type != "application/pdf" || e.src == undefined)
4
throw new Error("This does not look like a PDF document");
5
return e.src;
6
}
7
Now, the latest version of Google Chrome does not provide the src attribue anymore.
JavaScript
1
6
1
<html>
2
<body style="height: 100%; width: 100%; overflow: hidden; margin:0px; background-color: rgb(82, 86, 89);">
3
<embed style="position:absolute; left: 0; top: 0;" width="100%" height="100%" src="about:blank" type="application/pdf" internalid="3568AA495C01C5F2079A85384CEE54EE">
4
</body>
5
</html>
6
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