Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How extension get the text selected in chrome pdf viewer?

I wrote a chrome extension - english dictionary. You select word, the definition appears.

It works well, but I counter a problem. It seems there is no api of chrome pdf viewer supplied by google.

How can I get the word when i select a word in pdf using chrome pdf viewer?

I will be appreciate if you could help me.

enter image description here

like image 848
Ryan Lv Avatar asked Mar 20 '13 14:03

Ryan Lv


People also ask

How do I get text from a PDF in Chrome?

The solution is to first left click anywhere on the page of the PDF with the mouse before selecting text or pressing CTRL + A and CTRL + C to copy the text.

What is the Google Chrome extension that reads text?

NaturalReader - Text To Speech, is a chrome extension that converts text online into natural sounding audio. Simply press play and have your Emails, Websites, PDFs, Google Docs and Kindle Books read aloud to you!


1 Answers

You can get selected text using the context menu. In your background script, adding these lines will allow the user to right-click and do something with the selectionText.

chrome.contextMenus.create({id:"lookup",title:"Lookup %s",contexts:["selection"]});
chrome.contextMenus.onClicked.addListener(function(sel){
       console.log(sel.selectionText);
});

Grabbing this text works fine with PDFs, whether part of the extension, or not.

However, you cannot inject a script into a page starting with "chrome-extension://". If this is how your extension works, that will not be (directly) possible. But getting the selected text, and doing something with it still very doable.

As an alternative to requiring script injection, see the notification api, which allows a small message to popup, which could contain the definition of the word.

like image 168
Tyler Peryea Avatar answered Oct 06 '22 21:10

Tyler Peryea