熔岩泡芙
频道主
键盘快捷键JS
/*键盘链接*/
/* Keyboard links v1.3 - Start */
var KBIntervalID = 0;
$(document).on(":passagerender", function (ev) {
clearInterval(KBIntervalID);
UpdateLinks(ev.content);
// Search passages for links every 300ms, just in case they get updated, and marks them for key clicks
KBIntervalID = setInterval(UpdateLinks, 300);
});
// Adds key shortcut indicators to links in passage if there are less than 11 links in the passsage.
function UpdateLinks(Container) {
// Enables keyboard shortcuts on passages that do not have the "DisableKeyLinks" tag
if (!tags().includes("DisableKeyLinks")) {
var Links, i;
if (typeof Container === "undefined") {
Container = document;
Links = $("#passages a").toArray();
} else {
Links = $(Container).find("a").toArray();
}
if ( > 0) {
for (i = - 1; i >= 0; i--) {
if ($(Links[i]).data("nokeys") || $(Links[i]).parent().data("nokeys")) {
Links.deleteAt(i);
}
}
}
if ( === 1) {
if (!Links[0].id.includes("Link") && !Links[0].id.includes("NextLnk")) {
Links[0].id = "NextLnk";
}
} else if ( >= 1 && <= 10) {
if ($("#NextLnk").length > 0) { // Remove "NextLnk" ID since the passage now has more than one link.
$("#NextLnk").removeAttr("id");
}
var n = 1;
for (i = 0; i < Links.length; i++) {
if (!Links[i].id.includes("Link")) {
while ($(Container).find("#Link" + n).length) {
++n;
if (n > 10) {
break;
}
}
if (n < 10) {
$("<sup>[" + n + "]</sup>").appendTo(Links[i]);
Links[i].id = "Link" + n;
} else if (n === 10) {
$("<sup>[0]</sup>").appendTo(Links[i]);
Links[i].id = "Link0";
break;
} else {
break;
}
}
}
}
}
}
$(document).on("keyup", function (e) {
// Enables keyboard shortcuts on passages that do not have the "DisableKeyLinks" tag and when you're not entering text
if (!tags().includes("DisableKeyLinks") && ($("input:focus").length === 0) && ($("textarea:focus").length === 0) && ($("div[contenteditable='true']:focus").length == 0)) {
// Trigger next link click on right arrow key or "1" (normal and numpad)
if ((( == "ArrowRight") || ( == "1") || (e.keyCode === 97)) && ($("#NextLnk").length > 0)) {
if (!(tags().includes("IgnoreArrowKeys") && ( == "ArrowRight"))) {
e.preventDefault();
$("#NextLnk").click();
return false;
}
} else {
// Trigger link click on keys "0" through "9"
if ((e.keyCode > 47) && (e.keyCode < 58)) {
if ($("#Link" + (e.keyCode - 48)).length) {
e.preventDefault();
$("#Link" + (e.keyCode - 48)).click();
return false;
}
}
// Trigger link click on numpad keys "0" through "9"
if ((e.keyCode > 95) && (e.keyCode < 106)) {
if ($("#Link" + (e.keyCode - 96)).length) {
e.preventDefault();
$("#Link" + (e.keyCode - 96)).click();
return false;
}
}
}
// Trigger random click on "." or "r" key
if (( == ".") || ( == "r")) {
e.preventDefault();
var Links = $("#passages a"), n, UsableLinks = [];
if ( > 0) {
for (n = 0; n < Links.length; n++) {
if (!$(Links[n]).data("nokey")) {
UsableLinks.push(n);
}
}
if ( > 0) {
n = random( - 1);
Links[UsableLinks[n]].click();
return false;
}
}
}
// Trigger back click on left arrow key or backquote
if (( == "ArrowLeft") || ( == "`")) {
if ((!tags().includes("IgnoreArrowKeys")) || ( != "ArrowLeft")) {
e.preventDefault();
Engine.backward();
return false;
}
}
}
});
/* Keyboard links - End */
- 下载图片
- 复制图片
2024-07-30
浏览476
js使用
登录后评论
6
评论
1