在chrome pdf查看器中扩展如何获取所选文本? [英] How extension get the text selected in chrome pdf viewer?

查看:280
本文介绍了在chrome pdf查看器中扩展如何获取所选文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个Chrome扩展程序 - ,它允许弹出一个小消息,其中可以包含单词的定义。

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.

解决方案

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.

这篇关于在chrome pdf查看器中扩展如何获取所选文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆