-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
139 additions
and
3 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { BackgroundBehavior } from "./lib/behavior"; | ||
import { sleep } from "./lib/utils"; | ||
|
||
export class AutoClick extends BackgroundBehavior | ||
{ | ||
_donePromise: Promise<void>; | ||
_markDone: () => void; | ||
selector: string; | ||
seen = new Set<string>(); | ||
|
||
constructor(selector = "a[href]") { | ||
super(); | ||
this.selector = selector; | ||
this._donePromise = new Promise<void>((resolve) => this._markDone = resolve); | ||
} | ||
|
||
beforeUnload(event) { | ||
event.preventDefault(); | ||
return false; | ||
} | ||
|
||
nextExternalElem(origNoHash: string) : HTMLAnchorElement | null { | ||
try { | ||
const allLinks = document.querySelectorAll(this.selector); | ||
for (const el of allLinks) { | ||
const elem = el as HTMLAnchorElement; | ||
if (!elem.href || elem.href.startsWith(origNoHash)) { | ||
continue; | ||
} | ||
if (!this.seen.has(elem.href)) { | ||
return elem; | ||
} | ||
} | ||
} catch (e) { | ||
this.debug(e.toString()); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
async start() { | ||
const origHref = self.location.href; | ||
|
||
const url = new URL(origHref); | ||
url.hash = ""; | ||
const origNoHash = url.href + "#"; | ||
|
||
// process all links (except hash links) which could result in attempted navigation | ||
window.addEventListener("beforeunload", (e) => this.beforeUnload(e)); | ||
|
||
// eslint-disable-next-line no-constant-condition | ||
while (true) { | ||
const elem = this.nextExternalElem(origNoHash); | ||
|
||
if (!elem) { | ||
break; | ||
} | ||
|
||
await this.processElem(elem, origHref); | ||
} | ||
|
||
// process hashlinks on same page | ||
for (const el of document.querySelectorAll(this.selector)) { | ||
const elem = el as HTMLAnchorElement; | ||
if (elem.href.startsWith(origNoHash)) { | ||
await this.processElem(elem, origHref); | ||
} | ||
} | ||
|
||
window.removeEventListener("beforeunload", (e) => this.beforeUnload(e)); | ||
|
||
this._markDone(); | ||
} | ||
|
||
async processElem(elem: HTMLAnchorElement, origHref: string) { | ||
const href = elem.href; | ||
|
||
this.seen.add(href); | ||
|
||
if (!elem.isConnected) { | ||
return; | ||
} | ||
|
||
if (!href.startsWith(self.location.origin)) { | ||
//this.log("external origin: " + elem.href); | ||
return; | ||
} | ||
|
||
const anySelf = self as any; | ||
|
||
if (anySelf.getEventListeners && !anySelf.getEventListeners(elem).click) { | ||
//this.log("no click: " + href); | ||
return; | ||
} | ||
|
||
if (anySelf.__bx_addSet && !await anySelf.__bx_addSet(href)) { | ||
//this.log("already clicked: " + elem.href); | ||
return; | ||
} | ||
|
||
this.debug("Clicking on " + href); | ||
|
||
elem.click(); | ||
|
||
await sleep(500); | ||
|
||
if (self.location.href != origHref) { | ||
await new Promise((resolve) => { | ||
window.addEventListener("popstate", () => { | ||
resolve(null); | ||
}, { once: true }); | ||
|
||
window.history.back(); | ||
}); | ||
|
||
await sleep(500); | ||
} | ||
} catch (e) { | ||
this.debug(e.toString()); | ||
} | ||
|
||
done() { | ||
return this._donePromise; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters