-
-
Save aleksanderwalczuk/53d02042d18d3efdaac8060ff86b9cf6 to your computer and use it in GitHub Desktop.
Scheduled touch events on ATF
This file contains hidden or 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
| (function () { | |
| const MAX_SCHEDULE_SECONDS = 5; | |
| function getCenterOfScreen() { | |
| // Get the width and height of the viewport | |
| // Calculate the center coordinates of the viewport | |
| return { | |
| x: window.screen.width, | |
| y: window.screen.height, | |
| }; | |
| } | |
| function registerInterval(interval) { | |
| if (window != null && window.intervals == null) { | |
| window.intervals = []; | |
| } | |
| window.startTimestamp = Date.now(); | |
| window.intervals.push(interval); | |
| } | |
| function unregisterInterval() { | |
| clearInterval(window.intervals[window.intervals.length - 1]); | |
| window.intervals = window.intervals.slice(0, -1); | |
| console.log(`Interval unregistered, ${window.intervals.length} left`); | |
| if (window.startTimestamp) { | |
| const now = Date.now(); | |
| const diff = new Date(now - window.startTimestamp); | |
| console.log( | |
| `Was active for about ${diff.getMinutes()} minutes and ${Math.floor( | |
| diff.getSeconds() | |
| )} seconds` | |
| ); | |
| } | |
| } | |
| window.prevCoordinates = getCenterOfScreen(); | |
| const visibleElements = []; | |
| const touchedElementsIndexes = []; | |
| // wrap in timeout for performance optimization | |
| function getVisibleElements() { | |
| const elements = document.body.querySelectorAll("*"); | |
| for (let i = 0; i < elements.length; i++) { | |
| const element = elements[i]; | |
| const rect = element.getBoundingClientRect(); | |
| const excludedTags = [ | |
| "A", | |
| "BUTTON", | |
| "TEXTAREA", | |
| "INPUT", | |
| "SCRIPT", | |
| "IFRAME", | |
| "STYLE", | |
| "META", | |
| ]; | |
| if ( | |
| rect.top >= 0 && | |
| rect.bottom <= window.innerHeight && | |
| rect.width > 0 && | |
| rect.height > 0 && | |
| excludedTags.includes(element.tagName) === false && | |
| element.hasAttribute("onclick") === false | |
| ) { | |
| visibleElements.push({ | |
| x: rect.left, | |
| y: rect.top, | |
| el: element, | |
| index: i, | |
| }); | |
| } | |
| } | |
| } | |
| function sendTouches(visibleElementData) { | |
| function sendTouchEvent(x, y, element, eventType) { | |
| const touchObj = new Touch({ | |
| identifier: Date.now(), | |
| target: element, | |
| clientX: x, | |
| clientY: y, | |
| radiusX: 2.5, | |
| radiusY: 2.5, | |
| rotationAngle: 10, | |
| force: 0.5, | |
| }); | |
| const touchEvent = new TouchEvent(eventType, { | |
| cancelable: true, | |
| bubbles: true, | |
| touches: [touchObj], | |
| targetTouches: [], | |
| changedTouches: [touchObj], | |
| shiftKey: true, | |
| }); | |
| element.dispatchEvent(touchEvent); | |
| } | |
| const { el, x, y } = visibleElementData; | |
| sendTouchEvent( | |
| window.prevCoordinates.x, | |
| window.prevCoordinates.y, | |
| el, | |
| "touchstart" | |
| ); | |
| sendTouchEvent(x, y, el, "touchmove"); | |
| sendTouchEvent(x, y, el, "touchend"); | |
| window.prevCoordinates.x = x; | |
| window.prevCoordinates.y = y; | |
| console.log( | |
| `created touch event at x: ${x} and y: ${y} form ${window.prevCoordinates.x} and ${window.prevCoordinates.y}. Current element is ${el.tagName}`, | |
| el | |
| ); | |
| } | |
| function handler() { | |
| const timeout = Math.floor(Math.random() * 1000 * 14) + 1000; | |
| // schedule touch | |
| setTimeout(() => { | |
| if (visibleElements.length === 0) { | |
| // gracefully stop | |
| unregisterInterval(); | |
| } | |
| const filtered = visibleElements.filter( | |
| (el) => touchedElementsIndexes.includes(el.index) === false | |
| ); | |
| const randomIndex = Math.floor(Math.random() * filtered.length); | |
| const item = filtered[randomIndex]; | |
| touchedElementsIndexes.push(item.index); | |
| sendTouches(item); | |
| }, timeout); | |
| } | |
| setTimeout(() => { | |
| getVisibleElements(); | |
| const unregisteredInterval = setInterval( | |
| handler, | |
| 1000 * MAX_SCHEDULE_SECONDS | |
| ); | |
| registerInterval(unregisteredInterval); | |
| document.body.addEventListener( | |
| "mousemove", | |
| () => { | |
| unregisterInterval(); | |
| console.log("Gracefully stopped, happy hacking"); | |
| }, | |
| { once: true } | |
| ); | |
| }, 0); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment