generated at
scrapbox-pointer-emulation@0.1.0
MouseEventを発生させてマウスクリックを再現するUserScript
クリック処理が終了するまで10msほど待つ

2021-08-23

実装したいこと
done長押しの再現

dependency
script.js
import {sleep} from '../sleep/script.js'; export async function mimicClick(element, {button = 0, X, Y, shiftKey = false, ctrlKey = false, altKey = false, wait = 10} = {}) { const mouseOptions = { button: 0, clientX: X, clientY: Y, bubbles: true, cancelable: true, shiftKey: shiftKey, ctrlKey: ctrlKey, altKey: altKey, view: window, }; element.dispatchEvent(new MouseEvent("mousedown", mouseOptions)); element.dispatchEvent(new MouseEvent("mouseup", mouseOptions)); element.dispatchEvent(new MouseEvent("click", mouseOptions)); await sleep(wait); // 少し待つ }

長押し
script.js
export async function mimicHoldDown(element, {button = 0, X, Y, shiftKey = false, ctrlKey = false, altKey = false, holding = 1000, wait = 10} = {}) { const touch = new Touch({ identifier: 0, target: element, clientX: X, clientY: Y, pageX: X + window.scrollX, pageY: Y + window.scrollY, }); const mouseOptions = { button: 0, clientX: X, clientY: Y, changedTouches: [touch], touches: [touch], bubbles: true, cancelable: true, shiftKey: shiftKey, ctrlKey: ctrlKey, altKey: altKey, view: window, }; element.dispatchEvent(new TouchEvent("touchstart", mouseOptions)); element.dispatchEvent(new MouseEvent("mousedown", mouseOptions)); await sleep(holding); element.dispatchEvent(new MouseEvent("mouseup", mouseOptions)); element.dispatchEvent(new TouchEvent("touchend", mouseOptions)); element.dispatchEvent(new MouseEvent("click", mouseOptions)); await sleep(wait); // 少し待つ }

#2021-08-23 10:59:30
#2021-08-22 21:51:49
#2021-06-08 23:23:06
#2021-05-16 09:00:54