最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - GM_setClipboard (and other GM functions) gives an error in a Firefox but not ChromeTampermonkey? - Stack Overflow

programmeradmin4浏览0评论

I'm working in Firefox and trying to create a function that will copy a link's text when I press Shift+* (Numpad).

The code works in Chrome Tampermonkey usually, but for some reason it's doing nothing in Firefox.
The following error is reported in the console:

"ReferenceError: GM_setClipboard is not defined"

This is my code:

document.addEventListener( "keydown", function(i) {
    var selectLink = $('a').eq(8); // The link by index
    var targetLink = selectLink.text(); // The link text

    if (i.keyCode === 106 && i.shiftKey) // Shift+Num*
    {
        GM_setClipboard(targetLink); // Copy to clipboard
    }
});

I'm working in Firefox and trying to create a function that will copy a link's text when I press Shift+* (Numpad).

The code works in Chrome Tampermonkey usually, but for some reason it's doing nothing in Firefox.
The following error is reported in the console:

"ReferenceError: GM_setClipboard is not defined"

This is my code:

document.addEventListener( "keydown", function(i) {
    var selectLink = $('a').eq(8); // The link by index
    var targetLink = selectLink.text(); // The link text

    if (i.keyCode === 106 && i.shiftKey) // Shift+Num*
    {
        GM_setClipboard(targetLink); // Copy to clipboard
    }
});
Share Improve this question edited Feb 23, 2016 at 18:41 Brock Adams 93.5k23 gold badges240 silver badges304 bronze badges asked Feb 23, 2016 at 11:36 DjHDjH 1,4984 gold badges23 silver badges43 bronze badges 2
  • Is there any error message or something like that? What is the actual and the expected behaviour? – Martin Zabel Commented Feb 23, 2016 at 12:03
  • Yes, sorry I just figured out how to retrieve the error messages from Greasemonkey in console, The error is "ReferenceError: GM_setClipboard is not defined". The expected behavior is that when I press Shift+* it will copy the text of the selected link. – DjH Commented Feb 23, 2016 at 12:05
Add a ment  | 

1 Answer 1

Reset to default 16

Greasemonkey requires explicit @grant statements to use GM_ functions. Whereas Tampermonkey still does some auto detection (a potential security hole).

So:

  1. You need to specify // @grant GM_setClipboard in your metadata block.

  2. However, this switches the sandbox back on (a good thing), so you also need to make sure you've @required jQuery.

This script will work in both Greasemonkey and Tampermonkey:

// ==UserScript==
// @name     _YOUR_SCRIPT_NAME
// @match    http://YOUR_SERVER.COM/YOUR_PATH/*
// @require  http://ajax.googleapis./ajax/libs/jquery/2.1.0/jquery.min.js
// @grant    GM.setClipboard
// ==/UserScript==

document.addEventListener ( "keydown", function (i) {
    var selectLink = $('a').eq (8); // The link by index
    var targetLink = selectLink.text ().trim (); // The link text

    if (i.keyCode === 106  &&  i.shiftKey) // Shift+Num*
    {
        GM.setClipboard (targetLink); // Copy to clipboard
    }
} );

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论