userscript - 将项目添加到右键单击菜单 [英] userscript - Add item to right-click menu

查看:127
本文介绍了userscript - 将项目添加到右键单击菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Greasemonkey编写一个userscript,并且想要添加一个项目到右键单击上下文菜单。如果我没有弄错,这个函数被称为 window.addEventListener(contextmenu,...)

<你能举个例子吗?

解决方案

以下脚本是从 http://userscripts-mirror.org/scripts/review/151097


重要警告!此副本未更改且未经测试。用户脚本可能存在安全问题。通用汽车是非常强大的,如果在错误的页面上使用,您的计算机的安全可能会有风险。



  / * 
Google图片搜索上下文菜单

右键点击图片以搜索Google图片时,在浏览器上下文菜单中添加按图片搜索。
版权所有(C)2012 LouCypher

本程序是免费软件:您可以根据GNU通用公共许可证条款重新发布和/或修改
b $ b自由软件基金会,许可证的第3版,或任何更新版本的
(可选)。

这个程序是分发的,希望它有用,
但没有任何的保证;甚至没有隐含的保证
的适销性或特定用途的适用性。有关更多详细信息,请参阅
GNU通用公共许可证。

您应该收到一份GNU通用公共许可证
的副本。如果没有,请参阅< http://www.gnu.org/licenses/>
* /

// == UserScript ==
// @name Google图片搜索上下文菜单
// @namespace http://userscripts.org/ users / 12
// @description在右键单击图片时,在浏览器上下文菜单中添加按图片搜索,以便用该图片搜索Google。
// @version 1.2
// @author LouCypher
//许可证GPL
// @资源许可证https://raw.github.com/LouCypher/userscripts/ master / licenses / GPL / LICENSE.txt
// @updateURL https://userscripts.org/scripts/source/151097.meta.js
// @include *
// @排除文件:// *
// @grant GM_openInTab
// == / UserScript ==

if(!(documentMethodin document.documentElement&&
HTMLMenuItemElement窗口))return;

var body = document.body;
body.addEventListener(contextmenu,initMenu,false);

var menu = body.appendChild(document.createElement(menu));
menu.outerHTML ='< menu id =userscript-search-by-imagetype =context> \
图标=数据:图像/ PNG; BASE64,\
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8 / 9hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\
AAAK6wAACusBgosNWgAAABx0RVh0U29mdHdhcmUAQWRvYmUgRmlyZXdvcmtzIENTNXG14zYAAAEl\
SURBVDiNY / Z // z8DJYCRkIKsthv / kRX9Z2BgmFalARdiIcaGKZXqcH5O + 01U + ay2G3MYGBiSiXUm\\ \\
mofnsBDSjEUTMkiBe2Eq1JnZ7TcZBHhZGNythBl0lLkZODmYGX7 ++ SDW / sZnhl3H3zF8 + voHwwsY\
FkR5ijNICLMzTF31hOHnr38MHGxMDJlhMgwv3vxkWL7jJYpaJmzu0lTigWtmYGBg + PHrH8P0VU8Y\
tJV5MNRiNYCfmxmuGQZ + / PrHwMmOqRyrAX /// WfgYEOV4mBjwjAUpwHHL31iyA6XgRvCwcbEkBUm\
w3DuxmcMtVgDkYONicHLVoTBSJOXgYONieHHz38Ml + 98Ydh88DXDtx // CBtACmBiYGCYS4H + OYyU\\ \\
5kasgUgKAADN8WLFzlj9rgAAAABJRU5ErkJggg ==>< / menuitem> \
< / menu>';
$ b document.querySelector(#userscript-search-by-image menuitem)
.addEventListener(click,searchImage,false);

函数initMenu(aEvent){
//当用户右键点击网页正文时执行
// aEvent.target是右键单击元素
var node = aEvent.target;
var item = document.querySelector(#userscript-search-by-image menuitem);
if(node.localName ==img){
body.setAttribute(contextmenu,userscript-search-by-image);
item.setAttribute(imageURL,node.src);
} else {
body.removeAttribute(contextmenu);
item.removeAttribute(imageURL);


$ b函数addParamsToForm(aForm,aKey,aValue){
var hiddenField = document.createElement(input);
hiddenField.setAttribute(type,hidden);
hiddenField.setAttribute(name,aKey);
hiddenField.setAttribute(value,aValue);
Form.appendChild(hiddenField);
}

函数searchImage(aEvent){
//当用户点击菜单项
时执行// aEvent.target是< menuitem>元素
var imageURL = aEvent.target.getAttribute(imageURL); (imageURL.indexOf(data:)== 0){
var base64Offset = imageURL.indexOf(,);
if(base64Offset!= -1){
var inlineImage = imageURL.substring(base64Offset + 1)
.replace(/ \ + / g, - )
.replace(/ \ // g,_)
.replace(/\./g,=);

var form = document.createElement(form);
form.setAttribute(method,POST);
form.setAttribute(action,//www.google.com/searchbyimage/upload);
form.setAttribute(enctype,multipart / form-data);
form.setAttribute(target,_blank);
addParamsToForm(form,image_content,inlineImage);
addParamsToForm(form,filename,);
addParamsToForm(form,image_url,);
body.appendChild(form);
form.submit();
} else {
GM_openInTab(https://www.google.com/searchbyimage?image_url=+
encodeURIComponent(imageURL));
}
}


I am writing a userscript for Greasemonkey and want to add an item to right click context menu. If I'm not mistaken, this function is called window.addEventListener("contextmenu", ...).

Can you give me example of this?

解决方案

Following script was copied from http://userscripts-mirror.org/scripts/review/151097:

Important warning! This copy is unchanged and untested. There might be security issues with userscripts. GM is very powerful and if used on the wrong page the safety of your computer may be at risk.

/*
    Google Image Search Context Menu
    Add 'Search by Image' in browser context menu when you
    right click on image to search Google with that image.
    Copyright (C) 2012 LouCypher

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program. If not, see <http://www.gnu.org/licenses/>
*/

// ==UserScript==
// @name            Google Image Search Context Menu
// @namespace       http://userscripts.org/users/12
// @description     Add 'Search by Image' in browser context menu when you right click on image to search Google with that image.
// @version         1.2
// @author          LouCypher
// @license         GPL
// @resource        license https://raw.github.com/LouCypher/userscripts/master/licenses/GPL/LICENSE.txt
// @updateURL       https://userscripts.org/scripts/source/151097.meta.js
// @include         *
// @exclude         file://*
// @grant           GM_openInTab
// ==/UserScript==

if (!("contextMenu" in document.documentElement &&
      "HTMLMenuItemElement" in window)) return;

var body = document.body;
body.addEventListener("contextmenu", initMenu, false);

var menu = body.appendChild(document.createElement("menu"));
menu.outerHTML = '<menu id="userscript-search-by-image" type="context">\
                    <menuitem label="Search Google with this image"\
                              icon="data:image/png;base64,\
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\
AAAK6wAACusBgosNWgAAABx0RVh0U29mdHdhcmUAQWRvYmUgRmlyZXdvcmtzIENTNXG14zYAAAEl\
SURBVDiNY/z//z8DJYCRkIKsthv/kRX9Z2BgmFalARdiIcaGKZXqcH5O+01U+ay2G3MYGBiSiXUm\
mofnsBDSjEUTMkiBe2Eq1JnZ7TcZBHhZGNythBl0lLkZODmYGX7++sdw/sZnhl3H3zF8+voHwwsY\
FkR5ijNICLMzTF31hOHnr38MHGxMDJlhMgwv3vxkWL7jJYpaJmzu0lTigWtmYGBg+PHrH8P0VU8Y\
tJV5MNRiNYCfmxmuGQZ+/PrHwMmOqRyrAX///WfgYEOV4mBjwjAUpwHHL31iyA6XgRvCwcbEkBUm\
w3DuxmcMtVgDkYONicHLVoTBSJOXgYONieHHz38Ml+98Ydh88DXDtx//CBtACmBiYGCYS4H+OYyU\
5kasgUgKAADN8WLFzlj9rgAAAABJRU5ErkJggg=="></menuitem>\
                  </menu>';

document.querySelector("#userscript-search-by-image menuitem")
        .addEventListener("click", searchImage, false);

function initMenu(aEvent) {
  // Executed when user right click on web page body
  // aEvent.target is the element you right click on
  var node = aEvent.target;
  var item = document.querySelector("#userscript-search-by-image menuitem");
  if (node.localName == "img") {
    body.setAttribute("contextmenu", "userscript-search-by-image");
    item.setAttribute("imageURL", node.src);
  } else {
    body.removeAttribute("contextmenu");
    item.removeAttribute("imageURL");
  }
}

function addParamsToForm(aForm, aKey, aValue) {
  var hiddenField = document.createElement("input");
  hiddenField.setAttribute("type", "hidden");
  hiddenField.setAttribute("name", aKey);
  hiddenField.setAttribute("value", aValue);
  aForm.appendChild(hiddenField);
}

function searchImage(aEvent) {
  // Executed when user click on menuitem
  // aEvent.target is the <menuitem> element
  var imageURL = aEvent.target.getAttribute("imageURL");
  if (imageURL.indexOf("data:") == 0) {
    var base64Offset = imageURL.indexOf(",");
    if (base64Offset != -1) {
      var inlineImage = imageURL.substring(base64Offset + 1)
                                 .replace(/\+/g, "-")
                                 .replace(/\//g, "_")
                                 .replace(/\./g, "=");

      var form = document.createElement("form");
      form.setAttribute("method", "POST");
      form.setAttribute("action", "//www.google.com/searchbyimage/upload");
      form.setAttribute("enctype", "multipart/form-data");
      form.setAttribute("target", "_blank");
      addParamsToForm(form, "image_content", inlineImage);
      addParamsToForm(form, "filename", "");
      addParamsToForm(form, "image_url", "");
      body.appendChild(form);
      form.submit();
    }
  } else {
    GM_openInTab("https://www.google.com/searchbyimage?image_url=" +
                 encodeURIComponent(imageURL));
  }
}

这篇关于userscript - 将项目添加到右键单击菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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