-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds new autoclick behavior, which: - Attempts to click on all 'a' elements as the default selector, customizable via opts.clickSelector option. - Click on all links that are different page on same domain to trigger custom event handlers, re-query all links in case DOM is changed due to dynamic navigation / custom event handler, store previously seen elements. - Use heuristics to determine when link may just be a simple navigation (check event click handlers, check if target is set, check if element is visible, etc...) - Use onbeforeunload to attempt to block navigation if link navigates away - bump to 0.7.0 - Fixes #81
- Loading branch information
Showing
7 changed files
with
149 additions
and
14 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
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,124 @@ | ||
import { BackgroundBehavior } from "./lib/behavior"; | ||
import { sleep } from "./lib/utils"; | ||
|
||
declare let getEventListeners: any; | ||
|
||
export class AutoClick extends BackgroundBehavior | ||
{ | ||
_donePromise: Promise<void>; | ||
_markDone: () => void; | ||
selector: string; | ||
seenElem = new WeakSet<HTMLElement>(); | ||
|
||
constructor(selector = "a") { | ||
super(); | ||
this.selector = selector; | ||
this._donePromise = new Promise<void>((resolve) => this._markDone = resolve); | ||
} | ||
|
||
nextSameOriginLink() : HTMLAnchorElement | null { | ||
try { | ||
const allLinks = document.querySelectorAll(this.selector); | ||
for (const el of allLinks) { | ||
const elem = el as HTMLAnchorElement; | ||
|
||
// skip URLs to different origin as they won't be handled dynamically, most likely just regular navigation | ||
if (elem.href && !elem.href.startsWith(self.location.origin)) { | ||
continue; | ||
} | ||
if (!elem.isConnected) { | ||
continue; | ||
} | ||
if (!elem.checkVisibility()) { | ||
continue; | ||
} | ||
if (this.seenElem.has(elem)) { | ||
continue; | ||
} | ||
this.seenElem.add(elem); | ||
return elem; | ||
} | ||
} catch (e) { | ||
this.debug(e.toString()); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
async start() { | ||
const origHref = self.location.href; | ||
|
||
const beforeUnload = (event) => { | ||
event.preventDefault(); | ||
return false; | ||
}; | ||
|
||
// process all links (except hash links) which could result in attempted navigation | ||
window.addEventListener("beforeunload", beforeUnload); | ||
|
||
// process external links on current origin | ||
|
||
// eslint-disable-next-line no-constant-condition | ||
while (true) { | ||
const elem = this.nextSameOriginLink(); | ||
|
||
if (!elem) { | ||
break; | ||
} | ||
|
||
await this.processElem(elem, origHref); | ||
} | ||
|
||
window.removeEventListener("beforeunload", beforeUnload); | ||
|
||
this._markDone(); | ||
} | ||
|
||
async processElem(elem: HTMLAnchorElement, origHref: string) { | ||
// if successfully called getEventListeners and no click handler, we can skip | ||
try { | ||
if (!getEventListeners(elem).click) { | ||
return; | ||
} | ||
} catch (_e) { | ||
// getEventListeners not available, need to actually click | ||
} | ||
|
||
if (elem.target) { | ||
return; | ||
} | ||
|
||
const anySelf = self as any; | ||
|
||
if (elem.href) { | ||
// skip if already clicked this URL, tracked in external state | ||
if (anySelf.__bx_addSet && !await anySelf.__bx_addSet(elem.href)) { | ||
return; | ||
} | ||
|
||
this.debug("Clicking on link: " + elem.href); | ||
} else { | ||
this.debug("Click empty link"); | ||
} | ||
|
||
elem.click(); | ||
|
||
await sleep(250); | ||
|
||
if (self.location.href != origHref) { | ||
await new Promise((resolve) => { | ||
window.addEventListener("popstate", () => { | ||
resolve(null); | ||
}, { once: true }); | ||
|
||
window.history.back(); | ||
}); | ||
} | ||
} 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
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