upload
This commit is contained in:
+122
@@ -0,0 +1,122 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""鼠标停在已打开的谷歌浏览器上时才向下滚,移开就停。Ctrl+C 停止。
|
||||
|
||||
用法:
|
||||
python chrome_自动下滑.py
|
||||
不移动鼠标,不抢焦点。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import ctypes
|
||||
import subprocess
|
||||
import time
|
||||
from ctypes import wintypes
|
||||
|
||||
# ==================== 运行参数(改这里) ====================
|
||||
SCROLL_INTERVAL = 0.08 # 每轮间隔,秒
|
||||
WHEEL_NOTCHES = 4 # 每轮滚几格,正数向下
|
||||
# 只滚标题包含这段文字的窗口;留空 = 全部 Chrome
|
||||
TITLE_KEYWORD = ''
|
||||
# ============================================================
|
||||
|
||||
subprocess.run('chcp 65001', shell=True, capture_output=True)
|
||||
|
||||
user32 = ctypes.windll.user32
|
||||
kernel32 = ctypes.windll.kernel32
|
||||
|
||||
WHEEL_DELTA = 120
|
||||
MOUSEEVENTF_WHEEL = 0x0800
|
||||
GA_ROOT = 2
|
||||
|
||||
class MOUSEINPUT(ctypes.Structure):
|
||||
_fields_ = [
|
||||
('dx', wintypes.LONG),
|
||||
('dy', wintypes.LONG),
|
||||
('mouseData', wintypes.DWORD),
|
||||
('dwFlags', wintypes.DWORD),
|
||||
('time', wintypes.DWORD),
|
||||
('dwExtraInfo', ctypes.POINTER(ctypes.c_ulong)),
|
||||
]
|
||||
|
||||
|
||||
class INPUT(ctypes.Structure):
|
||||
_fields_ = [
|
||||
('type', wintypes.DWORD),
|
||||
('mi', MOUSEINPUT),
|
||||
]
|
||||
|
||||
|
||||
def _window_text(hwnd):
|
||||
length = user32.GetWindowTextLengthW(hwnd)
|
||||
buf = ctypes.create_unicode_buffer(length + 1)
|
||||
user32.GetWindowTextW(hwnd, buf, length + 1)
|
||||
return buf.value
|
||||
|
||||
|
||||
PROCESS_QUERY_LIMITED_INFORMATION = 0x1000
|
||||
|
||||
|
||||
def _exe_path(hwnd):
|
||||
pid = wintypes.DWORD()
|
||||
user32.GetWindowThreadProcessId(hwnd, ctypes.byref(pid))
|
||||
handle = kernel32.OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, False, pid.value)
|
||||
if not handle:
|
||||
return ''
|
||||
try:
|
||||
size = wintypes.DWORD(32768)
|
||||
buf = ctypes.create_unicode_buffer(size.value)
|
||||
if not kernel32.QueryFullProcessImageNameW(handle, 0, buf, ctypes.byref(size)):
|
||||
return ''
|
||||
return buf.value
|
||||
finally:
|
||||
kernel32.CloseHandle(handle)
|
||||
|
||||
|
||||
def chrome_under_cursor():
|
||||
"""鼠标当前所在的窗口是 chrome.exe 时返回根窗口,否则 None。"""
|
||||
pt = wintypes.POINT()
|
||||
if not user32.GetCursorPos(ctypes.byref(pt)):
|
||||
return None
|
||||
hwnd = user32.WindowFromPoint(pt)
|
||||
if not hwnd:
|
||||
return None
|
||||
root = user32.GetAncestor(hwnd, GA_ROOT)
|
||||
exe = _exe_path(root).lower()
|
||||
if not exe.endswith('\\chrome.exe'):
|
||||
return None
|
||||
if TITLE_KEYWORD and TITLE_KEYWORD not in _window_text(root):
|
||||
return None
|
||||
return root
|
||||
|
||||
|
||||
def _send_wheel(notches):
|
||||
# 只发滚轮,不移动光标
|
||||
delta = (-WHEEL_DELTA * notches) & 0xFFFFFFFF
|
||||
wheel = INPUT()
|
||||
wheel.type = 0
|
||||
wheel.mi.mouseData = delta
|
||||
wheel.mi.dwFlags = MOUSEEVENTF_WHEEL
|
||||
user32.SendInput(1, ctypes.byref(wheel), ctypes.sizeof(INPUT))
|
||||
|
||||
|
||||
def main():
|
||||
print('鼠标放在谷歌浏览器上才下滑,移开即停。Ctrl+C 结束。', flush=True)
|
||||
scrolling = False
|
||||
try:
|
||||
while True:
|
||||
hwnd = chrome_under_cursor()
|
||||
if hwnd:
|
||||
if not scrolling:
|
||||
print(f'开始下滑: {_window_text(hwnd)}', flush=True)
|
||||
scrolling = True
|
||||
_send_wheel(WHEEL_NOTCHES)
|
||||
elif scrolling:
|
||||
print('鼠标已离开浏览器,暂停', flush=True)
|
||||
scrolling = False
|
||||
time.sleep(SCROLL_INTERVAL)
|
||||
except KeyboardInterrupt:
|
||||
print('已停止', flush=True)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user