Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Two fingers dragging on #ascendBG
- // @namespace http://tampermonkey.net/
- // @version 1.1
- // @description Imitates mousedown–mousemove–mouseup with two fingers on #ascendBG
- // @match https://orteil.dashnet.org/cookieclicker/
- // @icon https://www.google.com/s2/favicons?sz=64&domain=dashnet.org
- // @grant none
- // ==/UserScript==
- (function(){
- 'use strict';
- const target = document.getElementById('ascendBG');
- if (!target) {
- console.warn('ascendBG not found');
- return;
- }
- let isDragging = false;
- let prevMidX = 0;
- let prevMidY = 0;
- function getMidPoint(t0, t1) {
- return {
- x: (t0.clientX + t1.clientX) / 2,
- y: (t0.clientY + t1.clientY) / 2
- };
- }
- function isInside(touch) {
- const r = target.getBoundingClientRect();
- return touch.clientX >= r.left &&
- touch.clientX <= r.right &&
- touch.clientY >= r.top &&
- touch.clientY <= r.bottom;
- }
- document.addEventListener('touchstart', function(e){
- if (e.touches.length === 2 && isInside(e.touches[0]) && isInside(e.touches[1])) {
- const mid = getMidPoint(e.touches[0], e.touches[1]);
- prevMidX = mid.x;
- prevMidY = mid.y;
- isDragging = true;
- // mousedown
- const md = new MouseEvent('mousedown', {
- clientX: mid.x,
- clientY: mid.y,
- buttons: 1,
- bubbles: true,
- cancelable: true
- });
- target.dispatchEvent(md);
- e.preventDefault();
- }
- }, {passive: false});
- document.addEventListener('touchmove', function(e){
- if (!isDragging) return;
- if (e.touches.length < 2) {
- return;
- }
- const mid = getMidPoint(e.touches[0], e.touches[1]);
- const mm = new MouseEvent('mousemove', {
- clientX: mid.x,
- clientY: mid.y,
- buttons: 1,
- bubbles: true,
- cancelable: true
- });
- target.dispatchEvent(mm);
- prevMidX = mid.x;
- prevMidY = mid.y;
- e.preventDefault();
- }, {passive: false});
- document.addEventListener('touchend', function(e){
- if (!isDragging) return;
- if (e.touches.length < 2) {
- const mu = new MouseEvent('mouseup', {
- clientX: prevMidX,
- clientY: prevMidY,
- buttons: 1,
- bubbles: true,
- cancelable: true
- });
- target.dispatchEvent(mu);
- isDragging = false;
- }
- }, {passive: false});
- })();
Advertisement
Add Comment
Please, Sign In to add comment