summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars-Dominik Braun <lars@6xq.net>2018-03-18 11:08:28 +0100
committerLars-Dominik Braun <lars@6xq.net>2018-03-18 11:08:28 +0100
commit0af88d497664514a0acd542b05620d5dbbe9a38b (patch)
treeb1fad56e95ddcd04fde3aff914442f0ae53eb2d0
parent95ee40fa8cdca9cff0aba033a4352fc0621a9583 (diff)
downloadcrocoite-0af88d497664514a0acd542b05620d5dbbe9a38b.tar.gz
crocoite-0af88d497664514a0acd542b05620d5dbbe9a38b.tar.bz2
crocoite-0af88d497664514a0acd542b05620d5dbbe9a38b.zip
behavior: Add click selectors for reddit
This is slightly obnoxious, since their JavaScript rate-limits clicks to ≤3 Hz and simply ignores everything beyond that.
-rw-r--r--crocoite/data/click.js34
1 files changed, 27 insertions, 7 deletions
diff --git a/crocoite/data/click.js b/crocoite/data/click.js
index 7013487..03e9f37 100644
--- a/crocoite/data/click.js
+++ b/crocoite/data/click.js
@@ -9,6 +9,8 @@ const selectorFlag = Object.freeze ({
none: 0,
multi: 1, /* click item multiple times */
});
+const defaultClickThrottle = 50; /* in ms */
+const discoverInterval = 1000; /* 1 second */
const sites = Object.freeze ([
{
hostname: /^www\.facebook\.com$/i,
@@ -34,6 +36,13 @@ const sites = Object.freeze ([
/* load more comments */
{s: 'a.load-more__button', flags: selectorFlag.multi},
],
+ }, {
+ hostname: /^(www|np)\.reddit\.com$/i,
+ selector: [
+ /* show more comments, reddit’s javascript ignores events if too
+ * frequent */
+ {s: 'span.morecomments a', flags: selectorFlag.none, throttle: 500},
+ ],
}
]);
@@ -56,12 +65,22 @@ function makeClickEvent () {
/* throttle clicking */
let queue = [];
+let clickTimeout = null;
function click () {
- let o = queue.shift ();
- if (o !== undefined) {
+ if (queue.length > 0) {
+ const item = queue.shift ();
+ const o = item.o;
+ const selector = item.selector;
o.dispatchEvent (makeClickEvent ());
+
+ if (queue.length > 0) {
+ const nextTimeout = 'throttle' in selector ?
+ selector.throttle : defaultClickThrottle;
+ clickTimeout = window.setTimeout (click, nextTimeout);
+ } else {
+ clickTimeout = null;
+ }
}
- return queue.length > 0;
}
/* Element is visible if itself and all of its parents are
@@ -93,19 +112,20 @@ function discover () {
let obj = document.querySelectorAll (s.s);
for (let o of obj) {
if (!have.has (o) && isClickable (o)) {
- queue.push (o);
+ queue.push ({o: o, selector: s});
if (!(s.flags & selectorFlag.multi)) {
have.add (o);
}
}
}
}
- if (queue.length > 0) {
- window.setInterval (click, 50);
+ if (queue.length > 0 && clickTimeout === null) {
+ /* start clicking immediately */
+ clickTimeout = window.setTimeout (click, 0);
}
return true;
}
/* XXX: can we use a mutation observer instead? */
-window.setInterval (discover, 1000);
+window.setInterval (discover, discoverInterval);
}());