-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
52 lines (44 loc) · 1.82 KB
/
popup.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
window.onload = () => {
const $topBtn = document.getElementById('topBtn')
const $select = document.getElementById('select')
// 默认获取 value
chrome.storage.sync.get('speed', function (speed) {
$select.value = speed.speed
})
$topBtn.addEventListener('click', () => {
chrome.storage.sync.get('speed', function (speed) {
chrome.tabs.query({
active: true,
currentWindow: true
}, function (tabs) {
chrome.tabs.executeScript({
code: `
speed = '${speed.speed}'
function speedScroll() {
let currentTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;
if (currentTop > 0) {
window.requestAnimationFrame ? window.requestAnimationFrame(speedScroll) : setTimeout(speedScroll, 16)
switch (speed) {
case 'average':
window.scrollTo(0, currentTop - 60)
break
case 'fast-to-slow':
window.scrollTo(0, currentTop - (currentTop / 5))
break
case 'immediate':
window.scrollTo(0, 0)
break
}
}
}
speedScroll()
`
})
})
})
})
$select.addEventListener('change', () => {
const bg = chrome.extension.getBackgroundPage()
bg.setSelectedSpeed($select.value)
})
}