|
| 1 | +import { BackgroundBehavior } from "./lib/behavior"; |
| 2 | +import { sleep } from "./lib/utils"; |
| 3 | + |
| 4 | +declare let getEventListeners: any; |
| 5 | + |
| 6 | +export class AutoClick extends BackgroundBehavior |
| 7 | +{ |
| 8 | + _donePromise: Promise<void>; |
| 9 | + _markDone: () => void; |
| 10 | + selector: string; |
| 11 | + seenElem = new WeakSet<HTMLElement>(); |
| 12 | + |
| 13 | + constructor(selector = "a") { |
| 14 | + super(); |
| 15 | + this.selector = selector; |
| 16 | + this._donePromise = new Promise<void>((resolve) => this._markDone = resolve); |
| 17 | + } |
| 18 | + |
| 19 | + nextSameOriginLink() : HTMLAnchorElement | null { |
| 20 | + try { |
| 21 | + const allLinks = document.querySelectorAll(this.selector); |
| 22 | + for (const el of allLinks) { |
| 23 | + const elem = el as HTMLAnchorElement; |
| 24 | + |
| 25 | + // skip URLs to different origin as they won't be handled dynamically, most likely just regular navigation |
| 26 | + if (elem.href && !elem.href.startsWith(self.location.origin)) { |
| 27 | + continue; |
| 28 | + } |
| 29 | + if (!elem.isConnected) { |
| 30 | + continue; |
| 31 | + } |
| 32 | + if (!elem.checkVisibility()) { |
| 33 | + continue; |
| 34 | + } |
| 35 | + if (this.seenElem.has(elem)) { |
| 36 | + continue; |
| 37 | + } |
| 38 | + this.seenElem.add(elem); |
| 39 | + return elem; |
| 40 | + } |
| 41 | + } catch (e) { |
| 42 | + this.debug(e.toString()); |
| 43 | + } |
| 44 | + |
| 45 | + return null; |
| 46 | + } |
| 47 | + |
| 48 | + async start() { |
| 49 | + const origHref = self.location.href; |
| 50 | + |
| 51 | + const beforeUnload = (event) => { |
| 52 | + event.preventDefault(); |
| 53 | + return false; |
| 54 | + }; |
| 55 | + |
| 56 | + // process all links (except hash links) which could result in attempted navigation |
| 57 | + window.addEventListener("beforeunload", beforeUnload); |
| 58 | + |
| 59 | + // process external links on current origin |
| 60 | + |
| 61 | + // eslint-disable-next-line no-constant-condition |
| 62 | + while (true) { |
| 63 | + const elem = this.nextSameOriginLink(); |
| 64 | + |
| 65 | + if (!elem) { |
| 66 | + break; |
| 67 | + } |
| 68 | + |
| 69 | + await this.processElem(elem, origHref); |
| 70 | + } |
| 71 | + |
| 72 | + window.removeEventListener("beforeunload", beforeUnload); |
| 73 | + |
| 74 | + this._markDone(); |
| 75 | + } |
| 76 | + |
| 77 | + async processElem(elem: HTMLAnchorElement, origHref: string) { |
| 78 | + // if successfully called getEventListeners and no click handler, we can skip |
| 79 | + try { |
| 80 | + if (!getEventListeners(elem).click) { |
| 81 | + return; |
| 82 | + } |
| 83 | + } catch (_e) { |
| 84 | + // getEventListeners not available, need to actually click |
| 85 | + } |
| 86 | + |
| 87 | + if (elem.target) { |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + const anySelf = self as any; |
| 92 | + |
| 93 | + if (elem.href) { |
| 94 | + // skip if already clicked this URL, tracked in external state |
| 95 | + if (anySelf.__bx_addSet && !await anySelf.__bx_addSet(elem.href)) { |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + this.debug("Clicking on link: " + elem.href); |
| 100 | + } else { |
| 101 | + this.debug("Click empty link"); |
| 102 | + } |
| 103 | + |
| 104 | + elem.click(); |
| 105 | + |
| 106 | + await sleep(250); |
| 107 | + |
| 108 | + if (self.location.href != origHref) { |
| 109 | + await new Promise((resolve) => { |
| 110 | + window.addEventListener("popstate", () => { |
| 111 | + resolve(null); |
| 112 | + }, { once: true }); |
| 113 | + |
| 114 | + window.history.back(); |
| 115 | + }); |
| 116 | + } |
| 117 | + } catch (e) { |
| 118 | + this.debug(e.toString()); |
| 119 | + } |
| 120 | + |
| 121 | + done() { |
| 122 | + return this._donePromise; |
| 123 | + } |
| 124 | +} |
0 commit comments