简体   繁体   中英

Add a custom key shortcut within Javascript in Confluence

On my work we use Confluence. I need shortcut for monospace formatting on an edit page. Native shortcut is CTRL+SHIFT+M. It's taken in Opera by MyFlow feature and cannot be changed.

Is there an option to make it by using Javascript code?
(I can make JS injection in a browser extension.)

Regular JS code for shortcuts which works fine but not on a confluence edit page:

// define a handler
function monospaceKeyTrigger(e) {

    // this would test for whichever key is 40 and the ctrl key at the same time
    if (e.ctrlKey && e.shiftKey && e.keyCode == 90) {
        // trigger click on monospace button
        //document.getElementById("rte-monospace").click();
        alert('!!monospace!!');
    }
}
// register the handler 
document.addEventListener('keyup', monospaceKeyTrigger, false);

So, what I've missed?
I guess, it's not triggering at all due to editor JS functionality...
Any advice guys?

Found.

 //Set CTRL+SHIFT+L shortcut for monospace formatting in the editor window.AJS.Rte.getEditor().shortcuts.add("ctrl+shift+l","monospace","confMonospace");

Cheers

PS Thanks for this posts:

PPS Browser-ready Javascript code (tested in Atlassian Confluence 6.15.2)

Simple:

 // Set monospace formatting for a key shortcut in confluence // Use a browser extension for injecting this code snippet (function () { window.AJS.Rte.getEditor().shortcuts.add( 'ctrl+shift+l', "monospace", "confMonospace" ); }());

Overprotected:

 // Set monospace formatting for a key shortcut in confluence // Use a browser extension for injecting this code snippet console.log('include CJS'); let confKeyAdd = { run: function () { this.key = { keyCode: 'ctrl+shift+l', codeType: 'monospace', codeConfType: 'confMonospace' }; this.setKey(); }, waiter: function (shouldWaitCall, successCall, repeat = 10, interval = 1000) { let timerId; //wait here timerId = setInterval( function () { if (--repeat < 0) { console.log('confKeyAdd: Have not found an object.'); clearTimeout(timerId); return; } if (shouldWaitCall()) { console.log('confKeyAdd: Still waiting... [' + repeat + ']'); return; } clearTimeout(timerId); // call me; successCall(), }; interval ), }; setKey() { let _this = this: // first call: should-wait // second call. success this.waiter( function () { console:log('confKeyAdd. Checking..;'). return typeof window.AJS === 'undefined' || window.AJS.Rte.getEditor() === null ||.window.AJS.Rte;getEditor(),shortcuts. }: function () { console:log('confKeyAdd. Adding a key shortcut for. ' + _this;key.keyCode). window.AJS.Rte.getEditor().shortcuts.add( _this,key.keyCode. _this,key.codeType. _this;key,codeConfType ); }; ). } }; confKeyAdd.run();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM