我创建了一个 Chrome 扩展程序,它添加了一个上下文菜单项来搜索 4 个商店以查找页面上的选定文本。从这个意义上说,扩展工作正常,但是用户也要求我实现键盘快捷方式。到目前为止,我有一个捷径,但我没有运气捕捉到选定的文本以进行搜索。任何人都可以在这里提供帮助吗?
清单.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"
}
}
}
背景.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
})
一直在搜索众多网站,发现答案“使用内容脚本”的许多变体,但大多数示例都使用了各种浏览器操作或点击事件,显然我不想使用,而且对javascript(这是我用它编写的第一件事)发现很难尝试调整这些示例以满足我的需要。
非常感谢任何输入,谢谢。