1

I am writing a chrome extension with the new manifest v3.

The extension injects DOM element to the document's body (Popup helper for the selected text).

It works great with any site, but it doesn't work when the page is actually a PDF viewer extension of chrome.

Selecting the pdf's text is one problem but, the main problem is that the injection logic of the new DOM isn't working on the PDF viewer's DOM.

Is there any new restrictions in manifest v3? I see many chrome extensions succeed to do so with manifest 2.

My injection logic is simple document.body.append in the callback of document.addEventListener('mouseup', callback).

The code lives in my content_script.js.

The content script & background script seems to be running as expected in the PDF viewer context.

Maybe the document is not the PDF viewer's document somehow...

Any ideas?

Refactor-Man
  • 443
  • 8
  • 17
  • Use this: [How can I get selected text in pdf in Javascript?](https://stackoverflow.com/a/61076939) – wOxxOm May 08 '21 at 14:20
  • @wOxxOm It doesn't work for me. I gets CSP error on console by chrome. Adding – Refactor-Man May 10 '21 at 23:05
  • to be more accurate, page context method 2 throes CSP error. 1 or 3 not, but throws on `chrome.runtime.onMessage.addListener` onMessage is undefined. i took all of your code and put it on the injected script.. what i am missing? – Refactor-Man May 10 '21 at 23:49
  • You seem to be looking at the wrong link. The answer I've linked contains a full example for ManifestV3. – wOxxOm May 11 '21 at 03:34
  • I tried add your v3 code, but it still doesn't work. the main code with `getPdfSelectedText` should run in page context as you said. So thats what i did, (mentioned in my previous messages). Or maybe i i didn't understand what you mean by page context? Anyway... my extension do not triggered by those PDF-Viewer messages. any idea? – Refactor-Man May 15 '21 at 09:28
  • No, getPdfSelectedText runs in the content script. The answer I've linked contains a complete example of the content script that runs the necessary code in the page context. – wOxxOm May 15 '21 at 09:35
  • ok thanks, so i tried just put it on a new content script, but still could not get any message from the `chrome.runtime.onMessage.addListener`. this is my setting: `{ "matches": [ ""], "js": ["./content2.js"], "run_at": "document_start"}` – Refactor-Man May 15 '21 at 16:21
  • The problem with onMessage is a different unrelated thing, probably caused by reloading the extension but not reloading the tab or your code incorrectly uses chrome.runtime.sendMessage instead of chrome.tabs.sendMessage. Either way, I can't help without a proper [MCVE](/help/mcve). – wOxxOm May 15 '21 at 16:27

0 Answers0