What's the idiomatic way to write a reusable (pagination) script? #1683
Unanswered
DanKaplanSES
asked this question in
Q&A
Replies: 1 comment
-
I would let a try/catch handle the heavy lifting for me. // ==UserScript==
// @name Previous/Next buttons
// @namespace http://tampermonkey.net/
// @version 0.1
// @match https://www.example1.com/*
// @match https://www.example2.com/*
// @match https://www.example3.com/*
(function() {
'use strict';
let knownMarkers = {
"next": [".pagination.next", ".page-pagination.next", "#prev"],
"prev": [".pagination.prev", ".page-pagination.prev", "#next"]
}
// handle next
for (let marker of knownMarkers["next"]){
try {
// Your logic here
} catch (error){
// pass
}
}
// handle next
for (let marker of knownMarkers["prev"]){
try {
// Your logic here
} catch (error){
// pass
}
}
})(); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I want to write a script that adds keyboard shortcuts to websites that have pagination. i.e., a website with a UI like this (everything between the
|
s is a button):If I press the left key, I want to click the
<
button. If I press the right key, it clicks the>
button. This is straightforward for any one site, but I don't want to be copying and pasting it for every additional site. My question is, how do I avoid that?Right now, my script look something like this:
This does a decent job, but it still bothers me that I have to add every site twice, once for the
@match
, and again in theaddPreviousNextListener
. I could modify the@match
to match*
(I think?), but that feels wrong and maybe like a security vulnerability? But is that the only way to reduce this duplication further? Ideally, adding a new website to the script would be a one line change. Is that possible? How would it be done?Beta Was this translation helpful? Give feedback.
All reactions