【问题标题】:userscript - Add item to right-click menuuserscript - 将项目添加到右键菜单
【发布时间】:2011-05-16 12:12:28
【问题描述】:

我正在为 Greasemonkey 编写用户脚本,并希望在右键单击上下文菜单中添加一个项目。如果我没记错的话,这个函数叫做window.addEventListener("contextmenu", ...)

你能给我举个例子吗?

【问题讨论】:

  • 我不知道为什么一个不正确的答案被投了赞成票。询问的人已经说过他正在使用 GM,这是一个能够做到这一点的扩展。它不是一个新功能,但多年来一直是它的主要功能。请参阅here 以获取使用 GreaseMonkey 的具有此功能的代码示例
  • Related 不确定是否重复...你怎么看,@Brock? . . .我可以把我的答案移到这里。
  • @brasofilo,不是重复的。 GM 和 TM 在这方面完全不同。 ...另外,不确定这个问题是否符合当今的标准...

标签: firefox greasemonkey


【解决方案1】:

以下脚本是从http://userscripts-mirror.org/scripts/review/151097复制而来的:

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

/*
    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));
  }
}

【讨论】:

  • 我一直在使用基于类似代码的用户脚本,但遗憾的是自从从 Firefox ESR 78 升级到 ESR 91 后它就停止工作了,我相信因为 menu 不再支持 caniuse.com/menu
【解决方案2】:

不幸的是,&lt;menu&gt;+&lt;menuitem&gt; 解决方案自 Firefox 85 起不再有效(我相信从未在其他浏览器中有效)请参阅 herehere

以下是如何将项目添加到右键菜单的示例(在 Tampermonkey 和 Firefox 91 中测试),但遗憾的是它显示在“Tampermonkey”子菜单下,而不是作为其顶级项目自己的权利。

关键部分是@run-at context-menu

// ==UserScript==
// @name        Name of context menu item
// @include     *
// @version     0.1
// @grant       GM_openInTab
// @run-at      context-menu
// ==/UserScript==

(function() {
    'use strict';

    // If you want to do something based on the selected text
    var selectedText = window.getSelection().toString();

    // Construct your destination URL
    var url = ...

    // Open in new, foreground tab
    GM_openInTab(url, false);
})();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-10
    相关资源
    最近更新 更多