NOTICE: By continued use of this site you understand and agree to the binding Terms of Service and Privacy Policy.
// ==UserScript== // @name Vim shortcut for ieltscat // @namespace http://tampermonkey.net/ // @version 0.1 // @description Vim shortcut for ieltscat // @author EchoSun // @include *://ieltscat.xdf.cn/intensive/intensive/* // @grant none // @license GPL-3.0-or-later // @copyright 2021, echosun (https://openuserjs.org/users/echosun) // ==/UserScript== (function() { 'use strict'; })(); function fireKeyEvent(el, evtType, keyCode){ var doc = el.ownerDocument, win = doc.defaultView || doc.parentWindow, evtObj; if(doc.createEvent){ if(win.KeyEvent) { evtObj = doc.createEvent('KeyEvents'); evtObj.initKeyEvent( evtType, true, true, win, false, false, false, false, keyCode, 0 ); } else { evtObj = doc.createEvent('UIEvents'); Object.defineProperty(evtObj, 'keyCode', { get : function() { return this.keyCodeVal; } }); Object.defineProperty(evtObj, 'which', { get : function() { return this.keyCodeVal; } }); evtObj.initUIEvent( evtType, true, true, win, 1 ); evtObj.keyCodeVal = keyCode; if (evtObj.keyCode !== keyCode) { console.log("keyCode " + evtObj.keyCode + " 和 (" + evtObj.which + ") 不匹配"); } } el.dispatchEvent(evtObj); } else if(doc.createEventObject){ evtObj = doc.createEventObject(); evtObj.keyCode = keyCode; el.fireEvent('on' + evtType, evtObj); } } document.body.addEventListener("keydown", function(k) { // j -> ↓ if (k.keyCode == 74) { fireKeyEvent(document.body, 'keydown', 40); } // k -> ↑ if (k.keyCode == 75) { fireKeyEvent(document.body, 'keydown', 38); } // h -> ← if (k.keyCode == 72) { fireKeyEvent(document.body, 'keydown', 37); } // l -> → if (k.keyCode == 76) { fireKeyEvent(document.body, 'keydown', 39); } }, null);