-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
151 lines (129 loc) · 4.31 KB
/
content.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// content.js (更新)
let isScrolling = false;
let scrollSpeed = 1;
let currentScrollElement = null;
let lastScrollTop = 0;
let scrollInterval = null;
const BASE_SCROLL_AMOUNT = 2.5; // 将基础滚动量从 5 减少到 2.5
// 在页面加载时读取保存的速度设置
chrome.storage.sync.get(['scrollSpeed'], function(result) {
if (result.scrollSpeed) {
scrollSpeed = parseFloat(result.scrollSpeed);
}
});
function getScrollableElement(element) {
while (element && element !== document.body) {
const { overflowY } = window.getComputedStyle(element);
if (overflowY === 'auto' || overflowY === 'scroll') {
return element;
}
element = element.parentElement;
}
return window;
}
function startScrolling(direction) {
stopScrolling();
isScrolling = true;
const centerElement = document.elementFromPoint(window.innerWidth / 2, window.innerHeight / 2);
currentScrollElement = getScrollableElement(centerElement);
scrollInterval = setInterval(() => {
if (!isScrolling) {
clearInterval(scrollInterval);
return;
}
let adjustedSpeed = scrollSpeed;
if (scrollSpeed > 5) {
// 对于高速滚动,使用指数增长
adjustedSpeed = 5 + Math.pow(scrollSpeed - 5, 1.5);
}
const scrollAmount = direction === 'up' ? -adjustedSpeed : adjustedSpeed;
const target = currentScrollElement === window ? document.documentElement : currentScrollElement;
const currentScrollTop = target.scrollTop;
target.scrollTop += scrollAmount * BASE_SCROLL_AMOUNT;
// 检查是否真的滚动了
if (Math.abs(target.scrollTop - lastScrollTop) < 0.1) {
// 如果没有滚动,尝试滚动窗口
window.scrollBy(0, scrollAmount * BASE_SCROLL_AMOUNT);
}
lastScrollTop = target.scrollTop;
}, 16); // 约60fps
}
function stopScrolling() {
isScrolling = false;
if (scrollInterval) {
clearInterval(scrollInterval);
}
}
function createScrollButton(direction) {
const button = document.createElement('div');
button.className = `scroll-button scroll-${direction}`;
const img = document.createElement('img');
img.src = chrome.runtime.getURL(`images/${direction}_arrow.svg`);
img.alt = direction === 'up' ? 'Scroll Up' : 'Scroll Down';
button.appendChild(img);
button.addEventListener('click', (e) => {
e.stopPropagation();
if (isScrolling) {
stopScrolling();
} else {
startScrolling(direction);
}
});
return button;
}
function createScrollControls() {
const controls = document.createElement('div');
controls.className = 'scroll-controls';
controls.appendChild(createScrollButton('up'));
controls.appendChild(createScrollButton('down'));
document.body.appendChild(controls);
makeDraggable(controls);
}
function makeDraggable(element) {
let pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
element.onmousedown = dragMouseDown;
function dragMouseDown(e) {
e.preventDefault();
pos3 = e.clientX;
pos4 = e.clientY;
document.onmouseup = closeDragElement;
document.onmousemove = elementDrag;
}
function elementDrag(e) {
e.preventDefault();
pos1 = pos3 - e.clientX;
pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;
element.style.top = (element.offsetTop - pos2) + "px";
element.style.left = (element.offsetLeft - pos1) + "px";
}
function closeDragElement() {
document.onmouseup = null;
document.onmousemove = null;
}
}
// createScrollControls();
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'setSpeed') {
scrollSpeed = request.speed;
// 当收到新的速度设置时,也将其保存
chrome.storage.sync.set({scrollSpeed: scrollSpeed}, function() {
console.log('Speed setting updated');
});
} else if (request.action === 'getSpeed') {
sendResponse({speed: scrollSpeed});
}
return true;
});
// 初始化函数,在页面加载完成后调用
function initializeScrollControls() {
createScrollControls();
// 可以在这里添加其他初始化逻辑
}
// 在页面加载完成后初始化
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initializeScrollControls);
} else {
initializeScrollControls();
}