在Google文档中捕获文本选择 [英] Capture text selection in Google Docs

查看:102
本文介绍了在Google文档中捕获文本选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Chrome扩展程序,用于捕获用户文本选择并将选定文本发送给Google搜索。

  {
manifest_version:2,

名称:选择扩展,
description:搜索您选择的文本,
version:1.0,
permissions:[
tabs,
http:// * / *,
https:// * / *

background:{
scripts:[
background.js
],
持久性:false
},
browser_action:{
default_icon:icon.png,
default_title:标记!!
},
content_scripts:[
{
matches:[< all_urls>],
js:[content_script.js ]
}
]

content_script.js

  chrome.extension.onMessage.addListener(function(request,sender,sendResponse){
if(request。方法==getSelection){
sendResponse({data:window.getSelection()。toString()});
} else {
sendResponse({});
}
});

background.js

 函数initBackground(){

chrome.browserAction.onClicked.addListener(function(tab){
chrome.tabs.sendMessage(tab .id,{method:getSelection},function(response){
sendServiceRequest(response.data);
});
});
}

函数sendServiceRequest(selectedText){
var serviceCall ='http://www.google.com/search?q='+ selectedText;
chrome.tabs.create({url:serviceCall});
}

initBackground();

此代码适用于网页中的选择(例如Gmail,Facebook,新闻)。
我也希望能够通过PDF和Google Docs(在浏览器中查看)获得选择内容。
在这些情况下: window.getSelection 返回一个空字符串...



有人知道如何Google Docs文档并不真正遵循如何从扩展访问文本的正常方式。
我出于这个原因创建了一个工具来处理Google文档,可以在这里找到
a>



这应该使您能够:

  // contentScript .js 
var googleDocument = googleDocsUtil.getGoogleDocument();
console.log(所选文字为:+ googleDocument.selectedText);


I'm writing a Chrome extension that captures the user text selection and sends the selected text to Google search.

manifest.json

{
  "manifest_version": 2,

  "name": "Selection Extension",
  "description": "Search your selected text",
  "version": "1.0",
  "permissions": [
    "tabs",
    "http://*/*",
    "https://*/*"
  ],
  "background": {
    "scripts": [
      "background.js"
    ],
    "persistent": false
  },
  "browser_action": {
    "default_icon": "icon.png",
    "default_title": "Mark it!!"
  },
  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["content_script.js"]
    }
  ]

content_script.js

chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
    if (request.method == "getSelection") {
        sendResponse({data: window.getSelection().toString()});
    } else {
        sendResponse({});
    }
});

background.js

function initBackground() {

    chrome.browserAction.onClicked.addListener(function(tab) {
        chrome.tabs.sendMessage(tab.id, {method: "getSelection"}, function(response){
            sendServiceRequest(response.data);
        });
    });
}

function sendServiceRequest(selectedText) {
    var serviceCall = 'http://www.google.com/search?q=' + selectedText;
    chrome.tabs.create({url: serviceCall});
}

initBackground();

This code works for selection in webpages (such as Gmail, Facebook, news.) I also want to be able to get the selection in PDF, and Google Docs (viewed in the browser). In these cases: window.getSelection returns an empty string...

Someone knows how to do it?

解决方案

Google Docs document does not really follow the normal ways of how a to access text on from a Extension. I have for this reason created a tool to work with Google Docs, which can be found here

This should enable you to:

//contentScript.js
var googleDocument = googleDocsUtil.getGoogleDocument();
console.log("The selected text is: " + googleDocument.selectedText);

这篇关于在Google文档中捕获文本选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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