SimulateClick

2025/3/23 14:17:37

目前為 2025-03-23 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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.frames || window);