Skip to content

Instantly share code, notes, and snippets.

@m4lvin
Last active December 26, 2024 16:28
Show Gist options
  • Save m4lvin/b548cad79463c75204a7dc7106594faf to your computer and use it in GitHub Desktop.
Save m4lvin/b548cad79463c75204a7dc7106594faf to your computer and use it in GitHub Desktop.
Userscript to add Ctrl+C keyboard shortcut for Forward-PDF-Search in Overleaf.
// ==UserScript==
// @name Overleaf - Forward-PDF
// @description Add Ctrl+C keyboard shortcut for Forward-PDF-Search.
// @version 2024.12.26
// @author m4lvin
// @match https://www.overleaf.com/project/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
window.addEventListener("UNSTABLE_editor:extensions", (event) => {
const { CodeMirror, extensions } = event.detail;
//console.log("user script activating");
//console.log(event);
//console.log(CodeMirror);
document.onkeydown = function(e){
if(e.ctrlKey && e.keyCode==67) { // ctrl + c
// console.log(e.keyCode);
if (document.querySelector('.synctex-control-icon')) {
document.querySelector('.synctex-control-icon').click();
}
// also make it work with detached PDF tab:
if (document.querySelector('.detach-synctex-control')) {
document.querySelector('.detach-synctex-control').click();
}
}
};
console.log("user script set up");
});
})();
/*
NOTES
The method used above may slow down typing in overleaf in general.
The "right" approach would be to adapt CodeMirror, the JS editor that is
used as part of Overleaf. However, especially since the newer version of
CodeMirror (that uses a more functional framework), modfying the keymap
at runtime, i.e. *after* it has already started, seems complicated ;-)
References:
- https://discuss.codemirror.net/t/add-new-keymap-after-editorview-instantiated/5313/3
- https://codemirror.net/examples/config/
- https://stackoverflow.com/q/9954701
Unused garbage:
const uppercaseKeybinding = {
key: 'Alt-Space',
run: uppercase,
};
var map = {"Alt-Space": function(cm){ alert("hello"); }}
CodeMirror.addKeyMap(map);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment