2

Hi i am working on a chrome extension that inject a script into the web page and it's working fine on any other website but not on pdf files.

after some research i understand that the pdf viewer is an application/plugin that is executed inside the webpage so any script goes the the web page but not the pdf.

is there any way to execute java script on the page like just a console.log() nothing hard.

and thank you

edit: MVC

Manifest.js:

"version": "1.0",
"manifest_version": 3,
"background": {
    "service_worker": "background.js"
},


"permissions": ["storage", "activeTab", "scripting"],
"action": {
    "default_popup": "popup.html",

Example Script: store the current position of the scroll wheel

let purl = window.location.toString();


if(purl.match(/pdf$/g)){
  //alert("PDF's are not supported... For now ");
  
Posp=document.querySelector("#viewer").shadowRoot.querySelector("#scroller").scrollTop;
//this always return 0 because it's not executing inside the pdf viewer but the source tab
}
NAYN9
  • 55
  • 8
  • Do you have access to the source, which shows the PDF file? If not, there's nothing you can do because the browser decides how to process the `application/pdf` MIME type. Otherwise, you could try to encapsulate the PDF into an `iframe`. Then you could inject your custom JavaScript into the DOM. – gru Jan 17 '22 at 15:24
  • Content scripts can run on PDF files so you need to show an [MCVE](/help/mcve) with your code and manifest.json. – wOxxOm Jan 17 '22 at 15:32
  • @AlexGru can you please explain how that works i try not to mess with the user's webpage a lot so will this be noticeable – NAYN9 Jan 18 '22 at 12:38
  • @wOxxOm is it good now? – NAYN9 Jan 18 '22 at 12:46
  • @ayoubnay go check out similar questions like this one here: https://stackoverflow.com/questions/65971615/chrome-extension-inject-content-script-when-viewing-a-pdf – gru Jan 18 '22 at 12:47
  • Indeed, you won't be able to access the internals. There's only a couple of useful commands e.g. [getSelectedText](https://stackoverflow.com/a/61076939). – wOxxOm Jan 18 '22 at 13:01
  • @wOxxOm i would like to control the "document" element if possible and not the text – NAYN9 Jan 18 '22 at 15:28

0 Answers0