Skip to content

Commit

Permalink
add site-specific behaviors for twitter, instagram, and expand lib wi…
Browse files Browse the repository at this point in the history
…th xpath selector functions

- autoplay + autofetch, if enabled, start on load
- autoscroll used if no site-specific behaviors
- build to 'behaviors.js', include dist build for package
  • Loading branch information
ikreymer committed Mar 6, 2021
1 parent 717d32d commit 6650b70
Show file tree
Hide file tree
Showing 12 changed files with 720 additions and 34 deletions.
29 changes: 29 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module.exports = {
"env": {
"browser": true,
"es2021": true
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 12,
"sourceType": "module"
},
"rules": {
"indent": [
"error",
2
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"double"
],
"semi": [
"error",
"always"
]
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
1 change: 1 addition & 0 deletions dist/behaviors.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 8 additions & 9 deletions src/autofetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// also extract any urls from media query stylesheets that have not necessarily been loaded
// (May not work for cross-origin stylesheets)

import { runOnload } from "./lib/utils";

const SRC_SET_SELECTOR = "img[srcset], img[data-srcset], img[data-src], " +
"video[srcset], video[data-srcset], video[data-src], audio[srcset], audio[data-srcset], audio[data-src], " +
"picture > source[srcset], picture > source[data-srcset], picture > source[data-src], " +
Expand All @@ -22,17 +24,14 @@ export class AutoFetcher
this.urlSet = new Set();
this.urlqueue = [];
this.numPending = 0;
this.start();
}

init() {
console.log("init autofetch");
this.run();
this.initObserver();
}

done() {
//todo:
return Promise.resolve();
start() {
runOnload(() => {
this.run();
this.initObserver();
});
}

async run() {
Expand Down
12 changes: 8 additions & 4 deletions src/autoplay.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sleep } from "./lib/utils";
import { sleep, runOnload } from "./lib/utils";


const domainSpecificRedirect = [
Expand Down Expand Up @@ -36,6 +36,8 @@ export class Autoplay {
this.mediaSet = new Set();

this.promises = [];

this.start();
}

async checkAutoPlayRedirect() {
Expand All @@ -54,9 +56,11 @@ export class Autoplay {
}
}

init() {
this.checkAutoPlayRedirect();
this.initObserver();
start() {
runOnload(() => {
this.checkAutoPlayRedirect();
this.initObserver();
});
}

initObserver() {
Expand Down
15 changes: 4 additions & 11 deletions src/autoscroll.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { sleep } from "./lib/utils";
import { sleep, Behavior } from "./lib/utils";


// ===========================================================================
export class AutoScroll
export class AutoScroll extends Behavior
{
init() {
this.running = this.run();
}

async run() {
async* [Symbol.asyncIterator]() {
const canScrollMore = () =>
self.scrollY + self.innerHeight <
Math.max(
Expand All @@ -23,11 +19,8 @@ export class AutoScroll

while (canScrollMore()) {
self.scrollBy(scrollOpts);
yield {"msg": "Scrolling by " + scrollOpts.top};
await sleep(500);
}
}

done() {
return Promise.race([this.running, sleep(30000)]);
}
}
60 changes: 53 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import { AutoFetcher } from "./autofetcher";
import { Autoplay } from "./autoplay";
import { AutoScroll } from "./autoscroll";
import { runOnload } from "./lib/utils";
import { runOnload, sleep } from "./lib/utils";

import siteBehaviors from "./site";


// ===========================================================================
export class BehaviorManager
{
constructor() {
this.behaviors = [];
this.mainBehavior = null;
}

run(opts = {}) {
init(opts = {}) {
if (opts.autofetch) {
this.behaviors.push(new AutoFetcher());
}
Expand All @@ -18,19 +23,60 @@ export class BehaviorManager
this.behaviors.push(new Autoplay());
}

if (opts.autoscroll) {
this.behaviors.push(new AutoScroll());
let siteMatch = false;

if (opts.siteSpecific) {
for (const siteBehaviorClass of siteBehaviors) {
if (siteBehaviorClass.isMatch()) {
console.log("Starting Site-Specific Behavior: " + siteBehaviorClass.name);
this.mainBehavior = new siteBehaviorClass();
siteMatch = true;
break;
}
}
}

if (!siteMatch && opts.autoscroll) {
this.mainBehavior = new AutoScroll();
}

if (this.mainBehavior) {
this.behaviors.push(this.mainBehavior);
}

this.timeout = opts.timeout;
}

start() {
runOnload(() => {
for (const behavior of this.behaviors) {
behavior.init();
if (this.mainBehavior) {
this.mainBehavior.start();
}
});
}

done() {
return Promise.all(this.behaviors.map(x => x.done()));
const allBehaviors = Promise.all(this.behaviors.map(x => x.done()));

if (this.timeout) {
return Promise.race([allBehaviors, sleep(this.timeout)]);
} else {
return allBehaviors;
}
}

pause() {
console.log("pausing");
if (this.mainBehavior) {
this.mainBehavior.pause();
}
}

unpause() {
console.log("unpausing");
if (this.mainBehavior) {
this.mainBehavior.unpause();
}
}
}

Expand Down
Loading

0 comments on commit 6650b70

Please sign in to comment.