New issue
Advanced search Search tips

Issue 777814 link

Starred by 1 user

Issue metadata

Status: WontFix
Owner: ----
Closed: Nov 2017
Cc:
Components:
EstimatedDays: ----
NextAction: ----
OS: Linux , Windows , Chrome , Mac
Pri: 2
Type: Bug-Regression



Sign in to add a comment

Can't postMessage to PDF plugin from a contentScript(but still works from console)

Reported by i...@damnr6.com, Oct 24 2017

Issue description

UserAgent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:56.0) Gecko/20100101 Firefox/56.0

Steps to reproduce the problem:
1. Download my Trufflepiggy - Quick Search extension: https://chrome.google.com/webstore/detail/trufflepiggy-quick-search/hnlonillhoifflkeblghhphdonfgbhdi
2. Go to a website with a example pdf e.g.: http://che.org.il/wp-content/uploads/2016/12/pdf-sample.pdf
3. Open console 
4. Select something within the PDF
5. You gonna see an error that document.querySelector().postMessage is not a function on Chrome Canary (it probably started with v61 and is now also reproduceable since Opera v48). I  used this to post a message to the chrome PDF plugin and retrieve the selected text for my extension.
6. Let the text still selected and enter in your console(focus on top):
document.querySelector("[type='application/pdf']").postMessage({type: "getSelectedText"},"*");
and press ENTER.
7. The postMessage gets successfully sent and a piggy nose appear above your PDF selection.

What is the expected behavior?
Post a message from contentScript via document.querySelector("[type='application/pdf']").postMessage({type: "getSelectedText"},"*"); 

What went wrong?
document.querySelector("[type='application/pdf']").postMessage({type: "getSelectedText"},"*"); throws a is-not-a-function-error from contentScript.

Did this work before? Yes 60

Chrome version: <Copy from: 'about:version'>  Channel: canary
OS Version: OS X 10.12
Flash Version: 

It might has also worked on a minor 61 version. Just noticed the error yesterday and spent a few hours to find a fix on my side but no chance. Also Opera worked fine til the update today to 48.0.2685.50.
 
Bildschirmfoto 2017-10-24 um 12.54.16.png
177 KB View Download

Comment 1 by i...@damnr6.com, Oct 24 2017

The error occurs on all desktop OS. Unfortunately I can't change that anymore.

Comment 2 by woxxom@gmail.com, Oct 24 2017

You can try to insert postMessage code in a <script> element so that it runs in page context:
https://stackoverflow.com/a/9517879

Comment 3 by rbyers@chromium.org, Oct 24 2017

Cc: dsinclair@chromium.org
Components: -Blink Internals>Plugins>PDF
Summary: Can't postMessage to PDF plugin from a contentScript(but still works from console) (was: document.querySelector().postMessage throws not a function error from a contentScript(but still works from console))
I seem to recall dsinclair mentioning something about postMessage to PDF being removed, but I can't find any details.  Dan?

Comment 4 by rbyers@chromium.org, Oct 24 2017

Cc: rbyers@chromium.org
Status: Untriaged (was: Unconfirmed)
I don't think this was removed. We'll triage on our side and see what's going on.

Comment 6 by i...@damnr6.com, Oct 24 2017

Thank you very much for looking into this issue.

@woxxom: thanks for the suggestion. If possible I would like to keep everything in my contentScript. My code worked great for over a year now.

Can't wait to hear back from you Dan and Rybers.

Greetings from Vienna,

Clemens
Labels: OS-Linux
Tried on Linux and I got the sad face console error. Will try to track this down soon, so the piggy find its truffles.
this is working as intended. The JS object corresponding to the plugin is only available to the page, not to extensions (as it used to be in the past, but that was a bug)

Comment 10 by i...@damnr6.com, Nov 2 2017

@Jochen: Ok, I see. So this means the current behaviour gonna stay, right?

So the only possibility now to get the text selection within a PDF is via a context menu extension, correct?

Thank you.

Comment 11 by woxxom@gmail.com, Nov 2 2017

Reporter@, you can simply run the code in page context:

function getSelectedText() {
  return new Promise(resolve => {
    window.addEventListener('message', function onMessage(e) {
      if (e.origin === 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai' &&
          e.data && e.data.type === 'getSelectedTextReply') {
        window.removeEventListener('message', onMessage);
        resolve(e.data.selectedText);
      }
    });
    const script = document.createElement('script');
    document.documentElement.appendChild(script).text =
      `plugin.postMessage({type: 'getSelectedText'}, '*')`;
    script.remove();
  });
}

Usage: getSelectedText().then(text => { ... });

Comment 12 by i...@damnr6.com, Nov 2 2017

@woxxom: right - totally forgot about the link you posted above. Gonna try to integrate this into my next extension update. Thanks.
Status: WontFix (was: Untriaged)
Closing per comment #9.

Sign in to add a comment