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

javascript - Adding a custom keyboard shortcut using userscript to Chrome with Tampermonkey - Stack Overflow

programmeradmin0浏览0评论

I would like to add some custom keyboard shortcuts to a certain web page.

Using the accepted answer from this question as a guide: How can I add a JavaScript keyboard shortcut to an existing JavaScript Function?

I made my own little function and added a listener:

// ==UserScript==
// @name       ChartGame
// @namespace  /
// @version    0.1
// @description  enter something useful
// @match      *
// @copyright  2012+, You
// ==/UserScript==
function doc_keyUp(e) {
  switch(e.keyCode)
  {
  case 49: //1
    mon_clk(3);
    break;
  case 50:
    mon_clk(6);
    break;
  case 83: //s
    BuySell(0);
    break;
  case 68: //d
    BuySell(1);
    break;
  case 70: //f
    TimelapseDwn();
    TimelapseUp();
    break;
   default:
     break;
  }
}
document.addEventListener('keyup', doc_keyUp, false);

This code runs just perfectly fine if I enter it into the Chrome javascript console while on the appropriate web page. I can use the keyboard shortcuts as I intended. The only problem is that I have to reenter javascript code including the listener if I go to a next game(that is chart..).

My impression was that Tampermonkey would allow me to run this script automatically on specific pages that match the expression on @match. The code does appear to run, but there is no keyboard shortcut functionality.

What is missing or what is different from running javascript code from Chrome console and from an extension such as Tampermonkey?

I would like to add some custom keyboard shortcuts to a certain web page.

Using the accepted answer from this question as a guide: How can I add a JavaScript keyboard shortcut to an existing JavaScript Function?

I made my own little function and added a listener:

// ==UserScript==
// @name       ChartGame
// @namespace  http://www.chartgame.com/
// @version    0.1
// @description  enter something useful
// @match      http://www.chartgame.com/play*
// @copyright  2012+, You
// ==/UserScript==
function doc_keyUp(e) {
  switch(e.keyCode)
  {
  case 49: //1
    mon_clk(3);
    break;
  case 50:
    mon_clk(6);
    break;
  case 83: //s
    BuySell(0);
    break;
  case 68: //d
    BuySell(1);
    break;
  case 70: //f
    TimelapseDwn();
    TimelapseUp();
    break;
   default:
     break;
  }
}
document.addEventListener('keyup', doc_keyUp, false);

This code runs just perfectly fine if I enter it into the Chrome javascript console while on the appropriate web page. I can use the keyboard shortcuts as I intended. The only problem is that I have to reenter javascript code including the listener if I go to a next game(that is chart..).

My impression was that Tampermonkey would allow me to run this script automatically on specific pages that match the expression on @match. The code does appear to run, but there is no keyboard shortcut functionality.

What is missing or what is different from running javascript code from Chrome console and from an extension such as Tampermonkey?

Share Improve this question edited May 23, 2017 at 11:51 CommunityBot 11 silver badge asked Nov 13, 2012 at 13:57 SintSint 1,6203 gold badges22 silver badges39 bronze badges 3
  • 1 Just an example of script adding different shortcuts to google docs: github.com/WiliTest/userscript/blob/master/… – JinSnow Commented Jul 19, 2018 at 13:43
  • @JinSnow that link is broken, do you have an alternative link for that? I would actually like to see how to add shortcuts to google docs with a userscript. – localhost Commented Dec 6, 2023 at 22:40
  • sorry for my late reply, here is the script: gist.github.com/WiliTest/b78ad2c234565ba8ce40df15440540d9 – JinSnow Commented Feb 4, 2024 at 21:20
Add a comment  | 

1 Answer 1

Reset to default 17

That code does not work in a userscript because it is calling javascript functions defined by the target page. Userscripts operate in various sandboxes, and so cannot see the target page's JS so easily.

Tampermonkey (and Greasemonkey) provide a way around this with unsafeWindow. (Plain Chrome userscripts do not support unsafeWindow in any useful way.)

So, to use those functions, prefix them like so:

// ==UserScript==
// @name       ChartGame
// @namespace  http://www.chartgame.com/
// @version    0.1
// @description  enter something useful
// @match      http://www.chartgame.com/play*
// @copyright  2012+, You
// ==/UserScript==
function doc_keyUp(e) {
    switch (e.keyCode) {
        case 49:
            //1
            unsafeWindow.mon_clk(3);
            break;
        case 50:
            unsafeWindow.mon_clk(6);
            break;
        case 83:
            //s
            unsafeWindow.BuySell(0);
            break;
        case 68:
            //d
            unsafeWindow.BuySell(1);
            break;
        case 70:
            //f
            unsafeWindow.TimelapseDwn();
            unsafeWindow.TimelapseUp();
            break;
        default:
            break;
    }
}
document.addEventListener('keyup', doc_keyUp, false);


An alternative approach, and one that works on plain Chrome userscripts, is to Inject your code. But since you are using Tampermonkey, just use the unsafeWindow approach in this case.

发布评论

评论列表(0)

  1. 暂无评论