【问题标题】:I want to capture keyboard events in tinymce editor using a userscript (i.e add keyboard shortcuts)我想使用用户脚本在tinymce编辑器中捕获键盘事件(即添加键盘快捷键)
【发布时间】:2016-11-23 18:31:16
【问题描述】:

我想在 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"]/buttondiv标签,包含了要被组合键触发的按钮。

谁能提供一个解决方案,我对tinymce编辑器的了解不多,所以简单的解释会更有帮助。

提前致谢。

【问题讨论】:

  • 问题在于时间和 iframe。发布指向您尝试修改的页面的链接,我们可以查看。
  • 正在处理的页面仅供内部使用,并且是保密的,因此我无法与您分享该页面的链接。你能告诉我你需要哪部分html代码来找到解决方案,因为我不能给你页面的整个代码。

标签: javascript tinymce greasemonkey userscripts tampermonkey


【解决方案1】:

又找了一天,终于找到了我的查询here的解决方案。

    tinymce.activeEditor.on('keyup', function(e) {
        console.debug("keyup"); 
    });

【讨论】:

    猜你喜欢
    • 2011-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-26
    • 2017-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多