Chrome extension double click on a word

谁说我不能喝 提交于 2019-12-09 02:52:00

问题


I would like to know how to call a function once the user double-clicks on any word on the page they are visiting (from a content script). Something similar to Google Dictionary extension. I have inspected the codes in the extension but it was not indented (compiled/minimized) so I've decided to ask this question here. A snippet of codes would be good. Thanks in advance :)


回答1:


I suspect Google adds an EventListener to the page to register a double click, and then gets the text that was automatically selected.

http://developer.mozilla.org/en/DOM/element.addEventListener
http://developer.mozilla.org/en/DOM/window.getSelection

f=function(){
   console.log(window.getSelection().toString());
}
document.body.addEventListener('dblclick',f);


来源:https://stackoverflow.com/questions/4007759/chrome-extension-double-click-on-a-word

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