Can't postMessage to PDF plugin from a contentScript(but still works from console)
Reported by
i...@damnr6.com,
Oct 24 2017
|
|||||||
Issue descriptionUserAgent: 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.
,
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
,
Oct 24 2017
I seem to recall dsinclair mentioning something about postMessage to PDF being removed, but I can't find any details. Dan?
,
Oct 24 2017
,
Oct 24 2017
I don't think this was removed. We'll triage on our side and see what's going on.
,
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
,
Oct 30 2017
Tried on Linux and I got the sad face console error. Will try to track this down soon, so the piggy find its truffles.
,
Oct 30 2017
https://chromium.googlesource.com/chromium/src/+log/1596e5e8416bff88ddf4e44d8e42133dce2ea311..092ac05d47389f995f1a111072622e5ac69a024f -> r475874
,
Nov 2 2017
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)
,
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.
,
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 => { ... });
,
Nov 2 2017
@woxxom: right - totally forgot about the link you posted above. Gonna try to integrate this into my next extension update. Thanks.
,
Nov 2 2017
Closing per comment #9. |
|||||||
►
Sign in to add a comment |
|||||||
Comment 1 by i...@damnr6.com
, Oct 24 2017