Chrome扩展中的上下文菜单 [英] Context menus in Chrome extensions

查看:133
本文介绍了Chrome扩展中的上下文菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索并搜索了这个解决方案,但我遇到的每个源都似乎假定我已经对Chrome扩展有深刻的了解,甚至Google的帮助页面也是如此。

我知道Chrome扩展程序的基础知识,并使用一些基本内容脚本制作了一个。然而,现在我正在寻找一个涉及上下文菜单的设备。



假设当您突出显示单词并右键单击它们时,您会看到选项在Google 上搜索'<突出显示的字词>'并点击后,它会打开 http://www.google.com/search?q=<highlighted words> 在新选项卡中。我知道这存在于Chrome中,我确信已经有十亿个扩展模块可以复制它,但这只是我建立的一个例子。



如何可以我做到了这一点?

解决方案

脚本应该如下所示:

函数getword(info,tab){
console.log(Word+ info.selectionText +被点击了。);
chrome.tabs.create({
url:http://www.google.com/search?q=+ info.selectionText,
});
}
chrome.contextMenus.create({
title:Search:%s,
contexts:[selection],
onclick:getword,
});

和manifest.json:



<$ p $ {
name:App name,
version:1.0,
manifest_version:2,
description :你的描述,
权限:[
contextMenus
],
background:{
scripts:[script。 js]
}
}

这里有如何加载扩展: http://developer.chrome.com/extensions/getstarted.html


I've searched and searched and searched for a solution to this but every source I come across seems to assume I already have profound knowledge of Chrome extensions, even Google's help pages

I know the very basics of Chrome extensions and I made one with some basic content scripts. However, now I'm looking to make one that involves context menus.

Let's say when you highlight words and right-click them, you see the option Search '<highlighted words>' on Google and when clicked, it opens http://www.google.com/search?q=<highlighted words> in a new tab. I know this exists in Chrome and I'm sure there have been a billion extensions replicating it, but this is only an example for me to build off of.

How can I do this?

解决方案

Script should look like this:

function getword(info,tab) {
  console.log("Word " + info.selectionText + " was clicked.");
  chrome.tabs.create({  
    url: "http://www.google.com/search?q=" + info.selectionText,
  });           
}
chrome.contextMenus.create({
  title: "Search: %s", 
  contexts:["selection"], 
  onclick: getword,
});

And manifest.json:

{
    "name": "App name",
    "version": "1.0",
    "manifest_version": 2,
    "description": "Your description",
    "permissions": [
      "contextMenus"
     ],
    "background": { 
      "scripts": ["script.js"]
    }
}

Here you have how to load extension: http://developer.chrome.com/extensions/getstarted.html

这篇关于Chrome扩展中的上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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