Repository navigation
Expand file tree
/
Copy pathao3_keystrokes.user.js
More file actions
125 lines (118 loc) · 4.45 KB
/
Copy pathao3_keystrokes.user.js
File metadata and controls
125 lines (118 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// ==UserScript==
// @name AO3 - Keystrokes
// @namespace https://hkamran.com
// @version 1.2.2
// @description Next chapter (Shift + N, right arrow), previous chapter (Shift + P, left arrow), and kudos (K)
// @author H. Kamran
// @downloadUrl https://github.com/hkamran80/userscripts/raw/main/ao3_keystrokes.user.js
// @updateUrl https://github.com/hkamran80/userscripts/raw/main/ao3_keystrokes.user.js
// @match http*://archiveofourown.org/works/*
// @match http*://archiveofourown.org/collections/*
// @match http*://archiveofourown.org/tags/*
// @match http*://archiveofourown.org/series/*
// @match http*://www.archiveofourown.org/works/*
// @match http*://www.archiveofourown.org/collections/*
// @match http*://www.archiveofourown.org/tags/*
// @match http*://www.archiveofourown.org/series/*
// @match http*://squidgeworld.org/works/*
// @match http*://squidgeworld.org/collections/*
// @match http*://squidgeworld.org/tags/*
// @match http*://squidgeworld.org/series/*
// @grant none
// ==/UserScript==
/**
* Keystrokes for work pages
* @param {Event} e The event object
*/
const workKeystrokes = (e) => {
const evtobj = window.event ? window.event : e;
if (
document.activeElement.tagName.toLowerCase() !== "textarea" &&
document.activeElement.name !== "comment[comment_content]"
) {
if ((evtobj.keyCode === 78 && evtobj.shiftKey) || evtobj.keyCode === 39) {
// Next chapter/part in series (Shift + N or Right Arrow)
const nextChapterElement =
document.querySelector(".chapter.next a") ||
document.querySelector(".series .next");
window.location.href = nextChapterElement.href;
} else if (
(evtobj.keyCode === 80 && evtobj.shiftKey) ||
evtobj.keyCode === 37
) {
// Previous chapter/part in series (Shift + P or Left Arrow)
const previousChapterElement =
document.querySelector(".chapter.previous a") ||
document.querySelector(".series .previous");
window.location.href = previousChapterElement.href;
} else if (evtobj.keyCode === 75) {
// Kudos (K)
document.querySelector("form#new_kudo input[type=submit]").click();
} else if (evtobj.keyCode === 67) {
// Toggle comments (C)
document.querySelector("#show_comments_link a").click();
} else if (evtobj.keyCode === 83 && evtobj.shiftKey) {
// Subscribe (Shift + S)
document
.querySelector("form#new_subscription input[type=submit]")
.click();
}
} else {
if (
evtobj.keyCode === 13 &&
evtobj.shiftKey &&
document
.querySelector("textarea[name='comment[comment_content]']")
.value.trim().length > 0
) {
// Add comment (Shift + Enter)
document.querySelector("form.new_comment").submit();
}
}
};
/**
* Keystrokes for series pages
* @param {Event} e The event object
*/
const seriesPagination = (e) => {
const evtobj = window.event ? window.event : e;
if (evtobj.keyCode === 83 && evtobj.shiftKey) {
// Subscribe (Shift + S)
document.querySelector("form#new_subscription input[type=submit]").click();
}
};
/**
* Keystrokes for collection pages
* @param {Event} e The event object
*/
const collectionPagination = (e) => {
const evtobj = window.event ? window.event : e;
if ((evtobj.keyCode === 78 && evtobj.shiftKey) || evtobj.keyCode === 39) {
// Next page (Shift + N or Right Arrow)
const nextPageElement = document.querySelector("li.next a[rel=next]");
window.location.href = nextPageElement.href;
} else if (
(evtobj.keyCode === 80 && evtobj.shiftKey) ||
evtobj.keyCode === 37
) {
// Previous page (Shift + P or Left Arrow)
const previousPageElement = document.querySelector(
"li.previous a[rel=prev]",
);
window.location.href = previousPageElement.href;
}
};
if (
(window.location.href.includes("/collections/") ||
window.location.href.includes("/tags/")) &&
!window.location.href.includes("/works/")
) {
console.info("[AO3 Keystrokes] Using collection pagination keystrokes");
document.onkeydown = collectionPagination;
} else if (window.location.href.includes("/works/")) {
console.info("[AO3 Keystrokes] Using work pagination keystrokes");
document.onkeydown = workKeystrokes;
} else if (window.location.href.includes("/series/")) {
console.info("[AO3 Keystrokes] Using series pagination keystrokes");
document.onkeydown = seriesPagination;
}