Custom hotkeys for switching between files on overleaf.com
当前为
// ==UserScript==
// @name Overleaf fileswitcher
// @namespace http://tampermonkey.net/
// @version 1.0
// @license apache2
// @description Custom hotkeys for switching between files on overleaf.com
// @author Aditya Sriram
// @match https://www.overleaf.com/project/*
// @grant none
// @require http://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js
// @require http://cdnjs.cloudflare.com/ajax/libs/mousetrap/1.6.1/mousetrap.min.js
// @require http://cdnjs.cloudflare.com/ajax/libs/mousetrap/1.6.1/plugins/global-bind/mousetrap-global-bind.min.js
// ==/UserScript==
(function() {
var $ = jQuery.noConflict();
var allow_ext = ['tex', 'bib'];
var shortcuts = {};
function keybindg(key, desc, func) {
shortcuts[key] = desc;
Mousetrap.bindGlobal(key, function() {
console.log("triggered " + key);
func();
});
}
function init() {
console.log("activating custom overleaf hotkeys...");
}
function getfilename(i,e) {
if (typeof(e) === "undefined") e = i;
return $(e).find('span.ng-binding').eq(0).text().trim();
}
function match_ext(i, f) {
var fname = getfilename(f);
for (var ext of allow_ext) {
if (fname.endsWith(ext))
return true;
}
return false;
}
function switchFile(n) {
var files = $('file-entity:not(:has(div[ng-controller]))');
files = files.filter(match_ext);
var idx = files.index(files.filter(':has(.selected)'));
if (idx < 0) {
idx = 0;
console.log('no filtered file selected, falling back to first file');
}
var newidx = idx+n;
if (newidx >= files.length) newidx = 0;
if (newidx < 0) newidx = files.length-1;
var newfile = files.eq(newidx);
newfile.find('li').click(); // click li to focus file
var filename = getfilename(newfile); // get file name
var parents = newfile.parents('file-entity');
var filepath = parents.map(getfilename).get().reverse();
//filename = filename.replace(/RenameDelete/g, "");
if ($('#monkey-filename').length == 0) { // add file name display if it doesn't already exist
$('span.name.ng-binding').parent().after('<span id="monkey-filename" style="color:white;"></span>');
}
$('#monkey-filename').text("/" + filepath.concat(filename).join("/"));
}
$(document).ready(function() {
init();
keybindg('ctrl+shift+pageup', 'Previous File', function() {switchFile(-1);});
keybindg('ctrl+shift+pagedown', 'Next File', function() {switchFile(+1);});
console.log('hotkeys:', JSON.parse(JSON.stringify(shortcuts)));
});
})();