How extension get the text selected in chrome pdf viewer?

╄→尐↘猪︶ㄣ 提交于 2019-12-03 12:45:37

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/15527095/how-extension-get-the-text-selected-in-chrome-pdf-viewer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!