使用键盘快捷键捕获所选文本 [英] Capture selected text using keyboard shortcut

查看:118
本文介绍了使用键盘快捷键捕获所选文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Chrome扩展程序,其中添加了一个上下文菜单项,以在页面上搜索所选文本的4个商店。在这个意义上,扩展工作正常,但我被用户要求实现键盘快捷方式。到目前为止,我有一个简捷的工作方式,但是为了进行搜索,我没有抓住选定的文本。任何人都可以在这里帮忙?



Manifest.json

  { 
name:Context Shop Search,
description:使用上下文菜单搜索所选文本的商店,
version:0.6,
manifest_version:2,
permissions:[contextMenus,tabs],
background:{
scripts:[jquery.js,background .js]
},
commands:{
search:{
suggested_key:{
default:Ctrl + Shift + S
},
description:Search Stores
}
}
}

Background.js

  var url = [http:// groceries.asda.com/asda-webstore/landing/home.shtml?cmpid=ahc-_-ghs-asdacom-dsk-_-hp-_-sub_title#/search/,http://www.waitrose。 com / shop / HeaderSearchCmd?defaultSearch = GR& searchTerm =& search = Search,http://www.ocado.com/webshop/getSearchProducts.do ?clearTabs = yes& isFreshSearch = true& entry =,http://www.tesco.com/groceries/Product/Search/Default.aspx?searchBox=&newSort=true&search.x=-1042&search .Y = -63&安培;搜索=搜索]; 

var searchUrls = [];

函数Search(info){
//alert(info.selectionText);

var selection = info.selectionText;
searchUrls [0] =网址[0] .concat(选择);
searchUrls [1] = url [1] .replace(searchTerm =,searchTerm =。concat(selection));
searchUrls [2] =网址[2] .concat(选择);
searchUrls [3] =网址[3] .replace(searchBox =,searchBox =。concat(selection));

chrome.windows.create({
url:searchUrls,
height:768,
width:1024
});


$ b chrome.commands.onCommand.addListener(function(command){
alert(sometext);
});

chrome.contextMenus.create({
id:ctxtSearch,
title:在所有商店中搜索'%s',
上下文:[选择],
onclick:搜索
})

许多网站,并找到了答案'使用内容脚本'的许多变种,但大多数的例子是使用各种浏览器操作或点击事件,显然,我不想使用,是相当新的JavaScript(这是我用它写的第一件事)发现很难尝试和调整这些例子以适合我的需求。



非常感谢任何输入,谢谢。

是的,你需要在当前选项卡中运行代码才能获得选择,但在这种情况下并不困难:

  chrome.commands .onCommand.addListener(function(command){
if(command ===search){
chrome.tabs.executeScript({
code:window.getSelection()。toString ();
,函数(选择){
搜索({selectionText:selection [0]});
});
}
});


另一方面,在JavaScript中使用小写字母表示函数名称的第一个字母是常见做法,因此您可能需要将 Search 重命名为 search


I created a Chrome extension which adds a context menu item to search 4 stores for the selected text on a page. In this sense the extension works fine, however I was asked by a user to implement a keyboard short cut also. So far I have a short cut working, but am having no luck in capturing to selected text in order to do the search. Anyone able to help out here?

Manifest.json

{
    "name": "Context Shop Search",
    "description": "Searches shops for selected text using the context menu",
    "version": "0.6",
    "manifest_version": 2,
    "permissions": ["contextMenus", "tabs"],
    "background": {
        "scripts": ["jquery.js", "background.js"]
    },
    "commands": {
        "search": {
            "suggested_key": {
                "default": "Ctrl+Shift+S"
            },
            "description": "Search Stores"
        }
    } 
}

Background.js

var urls = ["http://groceries.asda.com/asda-webstore/landing/home.shtml?cmpid=ahc-_-ghs-asdacom-dsk-_-hp-_-sub_title#/search/", "http://www.waitrose.com/shop/HeaderSearchCmd?defaultSearch=GR&searchTerm=&search=Search", "http://www.ocado.com/webshop/getSearchProducts.do?clearTabs=yes&isFreshSearch=true&entry=", "http://www.tesco.com/groceries/Product/Search/Default.aspx?searchBox=&newSort=true&search.x=-1042&search.y=-63&search=Search"];

var searchUrls = [];

function Search(info) {
    //alert(info.selectionText);

    var selection = info.selectionText;
    searchUrls[0] = urls[0].concat(selection);
    searchUrls[1] = urls[1].replace("searchTerm=", "searchTerm=".concat(selection));
    searchUrls[2] = urls[2].concat(selection);
    searchUrls[3] = urls[3].replace("searchBox=", "searchBox=".concat(selection));

    chrome.windows.create({
        url: searchUrls,
        height: 768,
        width: 1024
    });

}

chrome.commands.onCommand.addListener(function (command) {
    alert("sometext");
});

chrome.contextMenus.create({
    id: "ctxtSearch",
    title: "Search '%s' in all stores",
    contexts: ["selection"],
    onclick: Search
}) 

Have been searching around numerous sites and found many variations of the answer 'use a content script', but most of the examples were using various browser actions or click events which, obviously, I don't want to use, and being fairly new to javascript (this is the first thing I've written with it) found it difficult to try and tweak these examples to suit my needs.

Any input greatly appreciated, Thanks.

解决方案

Yes, you'll need to run code in the current tab to get the selection, but it's nothing hard in this case:

chrome.commands.onCommand.addListener(function(command) {
  if(command === "search") {
    chrome.tabs.executeScript( {
      code: "window.getSelection().toString();"
    }, function(selection) {
      Search({selectionText: selection[0]});
    });
  }
});

chrome.tabs.executeScript without specifying a tabId parameter will execute code in the context of the active tab of the current window, and will call your callback function with the last expression evaluated in that code (actually the callback function receives an array of values because you could make executeScript to run code in more than one tab).

On a side note, it is common practice in javascript to use lowercase for the first letter of function names, so you may want to rename Search to search.

这篇关于使用键盘快捷键捕获所选文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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