javascriptpdfgoogle-chrome-extensioncontent-script

Getting local PDF's URL using a Content Script


When accessing pdfs for Google Chrome, the browser uses its own extension to read and show PDFs. I would like to get the URL from a local source file "file:///C:/Users/Test/Desktop/test%20extension/test.pdf" using a content script.

My goal is to eventually get the text of this PDF so that I can read through it, and the pdf.js library I am using requires the URL of the pdf.

On the chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/index.html file, the below element holds this data.

<embed id="plugin" type="application/x-google-chrome-pdf" src="file:///C:/Users/Test/Desktop/test%20extension/test.pdf" stream-url="blob:chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/e268f61a-7f6c-44ab-9218-7193dc73a783" headers="" background-color="0xFF525659" top-toolbar-height="56" javascript="allow" full-frame="">

I have embedded my content script as shown below in manifest.json:

    "content_scripts": [
    {
        "matches": ["*://*/*.pdf"],
        "js": ["contentScript.js"]
    }
    ],

My content script has the following code which I believe should pull this URL:

var fileUrl = document.getElementById('plugin').src;
console.log('The file URL is : ' + fileUrl);

My goal is to get the console.log() above to return the fileUrl ("file:///C:/Users/Test/Desktop/test%20extension/test.pdf"). Does anyone have suggestions on how to do this? Is it possible that I am not calling my content script correctly?


Solution

  • as @wOxxOm suggested, changing the "matches" pattern from

    "matches": "*://*/*.pdf"
    

    to

    "matches": "file://*/*.pdf"
    

    did the trick!!