SimulateClick

2025/3/23 14:17:37

当前为 2025-03-23 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        SimulateClick
// @namespace   http://fulicat.com
// @author      Jack.Chan ([email protected])
// @version     1.0.0
// @run-at      document-start
// @match       *://*/*
// @match       file:///*
// @url         https://greasyfork.org/zh-CN/scripts/530591-simulateclick
// @grant       none
// @description 2025/3/23 14:17:37
// @license MIT
// ==/UserScript==
 
 
;(function(T) {
	T.simulateClick = T.simulateClick || simulateClick;

	function simulateClick(target, options) {

		if (typeof options == 'string') {
			// console.log('options-string:', options);
			if (options.indexOf(',') > 1) {
				options = options.split('|');
				options = options.map((point) => {
					point = point.trim().split(',');
					return {
						clientX: Number((point[0] || 0).toString().trim()),
						clientY: Number((point[1] || 0).toString().trim())
					}
				});
			} else {
				options = undefined;
			}
			// console.log('options-string-2-array:', options);
		}
		if (Array.isArray(options)) {
			options.forEach((item) => {
				if (item.clientX !== undefined && item.clientY !== undefined) {
					((point) => {
						console.log('point:', point);
						simulatedClick.apply(simulatedClick, [target, point]);
					})(item);
				}
			});
			return false;
		}

		var event = target.ownerDocument.createEvent('MouseEvents'),
		options = options || {},
		opts = { // These are the default values, set up for un-modified left clicks
			type: 'click',
			canBubble: true,
			cancelable: true,
			view: target.ownerDocument.defaultView,
			detail: 1,
			screenX: 0, //The coordinates within the entire page
			screenY: 0,
			clientX: 0, //The coordinates within the viewport
			clientY: 0,
			ctrlKey: false,
			altKey: false,
			shiftKey: false,
			metaKey: false, //I *think* 'meta' is 'Cmd/Apple' on Mac, and 'Windows key' on Win. Not sure, though!
			button: 0, //0 = left, 1 = middle, 2 = right
			relatedTarget: null,
			related: true
		};

		//Merge the options with the defaults
		for (var key in options) {
			if (options.hasOwnProperty(key)) {
				opts[key] = options[key];
			}
		}

		// related element offset
		if (opts.related) {
			var offset = target.getBoundingClientRect();
			if (offset) {
				opts.clientX = offset.left + opts.clientX;
				opts.clientY = offset.top + opts.clientY;
			}
			delete opts.related;
		}

		// console.log('opts.clientX & Y:', opts.clientX, opts.clientY);

		//Pass in the options
		event.initMouseEvent(
				opts.type,
				opts.canBubble,
				opts.cancelable,
				opts.view,
				opts.detail,
				opts.screenX,
				opts.screenY,
				opts.clientX,
				opts.clientY,
				opts.ctrlKey,
				opts.altKey,
				opts.shiftKey,
				opts.metaKey,
				opts.button,
				opts.relatedTarget
		);

		//Fire the event
		target.dispatchEvent(event);
	}

})(window.alert || window.open || window);