我想使用用户脚本在 tinymce 编辑器中捕获键盘事件(即添加键盘快捷键)
I want to capture keyboard events in tinymce editor using a userscript (i.e add keyboard shortcuts)
我想在 tinymce 编辑器中为按钮添加键盘快捷键。问题是我无权访问该网站的源代码。所以这需要使用用户脚本(即 tampermonkey、greasemonkey)来完成。
如果我从浏览器开发工具中可用的控制台执行脚本,我已经成功创建了一个添加快捷方式的脚本,但此代码在 tampermonkey 或 greasemonkey 中不起作用。脚本如下:
function simulate(element, eventName)
{
var options = extend(defaultOptions, arguments[2] || {});
var oEvent, eventType = null;
for (var name in eventMatchers)
{
if (eventMatchers[name].test(eventName)) { eventType = name; break; }
}
if (!eventType)
throw new SyntaxError('Only HTMLEvents and MouseEvents interfaces are supported');
if (document.createEvent)
{
oEvent = document.createEvent(eventType);
if (eventType == 'HTMLEvents')
{
oEvent.initEvent(eventName, options.bubbles, options.cancelable);
}
else
{
oEvent.initMouseEvent(eventName, options.bubbles, options.cancelable, document.defaultView,
options.button, options.pointerX, options.pointerY, options.pointerX, options.pointerY,
options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, options.button, element);
}
element.dispatchEvent(oEvent);
}
else
{
options.clientX = options.pointerX;
options.clientY = options.pointerY;
var evt = document.createEventObject();
oEvent = extend(evt, options);
element.fireEvent('on' + eventName, oEvent);
}
return element;
}
function extend(destination, source) {
for (var property in source)
destination[property] = source[property];
return destination;
}
var eventMatchers = {
'HTMLEvents': /^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/,
'MouseEvents': /^(?:click|dblclick|mouse(?:down|up|over|move|out))$/
}
var defaultOptions = {
pointerX: 0,
pointerY: 0,
button: 0,
ctrlKey: false,
altKey: false,
shiftKey: false,
metaKey: false,
bubbles: true,
cancelable: true
}
tinymce.activeEditor.on('keydown', function(e) {
var e = e || window.event; // for IE to cover IEs window object
if(e.altKey && e.which == 81) { //Alt + Q
//alert('Keyboard shortcut working!');
simulate(document.evaluate('//div[@id="mceu_3"]/button', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue, "click");
return false;
}
});
在上面的代码中,div[@id="mceu_3"]/button
是 div
标签,其中包含要由组合键触发的按钮。
请问哪位大神给个解决办法,我对tinymce编辑器了解不多,所以简单的解释一下会更有帮助。
提前致谢。
又找了一天,终于找到了我的问题的答案here。
tinymce.activeEditor.on('keyup', function(e) {
console.debug("keyup");
});
我想在 tinymce 编辑器中为按钮添加键盘快捷键。问题是我无权访问该网站的源代码。所以这需要使用用户脚本(即 tampermonkey、greasemonkey)来完成。
如果我从浏览器开发工具中可用的控制台执行脚本,我已经成功创建了一个添加快捷方式的脚本,但此代码在 tampermonkey 或 greasemonkey 中不起作用。脚本如下:
function simulate(element, eventName)
{
var options = extend(defaultOptions, arguments[2] || {});
var oEvent, eventType = null;
for (var name in eventMatchers)
{
if (eventMatchers[name].test(eventName)) { eventType = name; break; }
}
if (!eventType)
throw new SyntaxError('Only HTMLEvents and MouseEvents interfaces are supported');
if (document.createEvent)
{
oEvent = document.createEvent(eventType);
if (eventType == 'HTMLEvents')
{
oEvent.initEvent(eventName, options.bubbles, options.cancelable);
}
else
{
oEvent.initMouseEvent(eventName, options.bubbles, options.cancelable, document.defaultView,
options.button, options.pointerX, options.pointerY, options.pointerX, options.pointerY,
options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, options.button, element);
}
element.dispatchEvent(oEvent);
}
else
{
options.clientX = options.pointerX;
options.clientY = options.pointerY;
var evt = document.createEventObject();
oEvent = extend(evt, options);
element.fireEvent('on' + eventName, oEvent);
}
return element;
}
function extend(destination, source) {
for (var property in source)
destination[property] = source[property];
return destination;
}
var eventMatchers = {
'HTMLEvents': /^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/,
'MouseEvents': /^(?:click|dblclick|mouse(?:down|up|over|move|out))$/
}
var defaultOptions = {
pointerX: 0,
pointerY: 0,
button: 0,
ctrlKey: false,
altKey: false,
shiftKey: false,
metaKey: false,
bubbles: true,
cancelable: true
}
tinymce.activeEditor.on('keydown', function(e) {
var e = e || window.event; // for IE to cover IEs window object
if(e.altKey && e.which == 81) { //Alt + Q
//alert('Keyboard shortcut working!');
simulate(document.evaluate('//div[@id="mceu_3"]/button', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue, "click");
return false;
}
});
在上面的代码中,div[@id="mceu_3"]/button
是 div
标签,其中包含要由组合键触发的按钮。
请问哪位大神给个解决办法,我对tinymce编辑器了解不多,所以简单的解释一下会更有帮助。
提前致谢。
又找了一天,终于找到了我的问题的答案here。
tinymce.activeEditor.on('keyup', function(e) {
console.debug("keyup");
});