-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfly-to-top.js
100 lines (99 loc) · 3.88 KB
/
fly-to-top.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
// ==UserScript==
// @name Fly to the top
// @namespace https://github.com/makaria/Tampermonkey-script/blob/master/fly-to-top.js
// @version 1.0
// @description Fly you to the top.
// @author maka
// @include http://*
// @include https://*
// @exclude http://localhost*
// @exclude about:blank
// @exclude about:newtab
// @exclude chrome://newtab/
// @grant none
// @run-at document-start
// ==/UserScript==
(function() {
'use strict';
console.log('reload');
var start = function() {
load();
};
var load = function () {
var span = window.top.document.getElementById('fly-to-top');
if (span) {
console.info('No need to create the button');
} else {
createElement();
}
};
var createElement = function () {
console.log('createElement');
var button = document.createElement('span');
button.id = 'fly-to-top';
button.style.opacity = 0.2;
button.style.transitionDuration = '0.2s';
button.style.backgroundImage = 'url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAAIGNIUk0AAHomAACAhAAA+gAAAIDoAAB1MAAA6mAAADqYAAAXcJy6UTwAAAAJcEhZcwAACxMAAAsTAQCanBgAAAACYktHRAD/h4/MvwAAARlJREFUaEPtlD2KAkEQRkc91sTqiSb1B2QRvJOBiaCewcxcEUx3v3K6VMRRu7qL2oF68JjWQL5HD3aqqiraTDc8W4sHWOMB1niANR5gjQdY4wHWeIA12gGToBqaATR8HFSL0Arg8YxahEbA83hGJSJ3QNN4JntEzoBX48/wWB9vZI3IFdA0fgj7UC0iR8C78Wu4g2oRqQGfxjNqESkB345nKGIAs0ZIA2LHM1tIEafrpzviCEmAdDxDEfQ6ZYmQBPyGJxMznmm6iWgkAVM4qo+i8cwGPkbQ70bfQK8sy3CMYgUv8AdKxjMHuIR7OKMvYkn5F1rAlPEM3cS8PsaTEvAv8ABrPMAaD7DGA6zxAGs8wBoPsKblAUXxB4ozRWtoGc5gAAAAAElFTkSuQmCC")';
button.style.backgroundRepeat = 'no-repeat';
button.style.backgroundAttachment = 'scroll';
button.style.backgroundPosition = '50% 50%';
button.style.backgroundColor = 'black';
button.style.borderRadius = '0px';
button.style.cursor = 'pointer';
button.style.position = 'fixed';
button.style.bottom = '6.18%';
button.style.right = '6px';
button.style.width = '48px';
button.style.height = '48px';
button.style.zIndex = 9999;
button.href = "#shadow-root";
button.addEventListener('mouseover', function() {
button.style.opacity = 1;
}, false);
button.addEventListener('mouseout', function() {
button.style.opacity = 0.2;
}, false);
button.addEventListener('click', function() {
getScrollElement();
}, false);
button.addEventListener('contextmenu', function(e) {
e.preventDefault();
button.style.display = 'none';
});
window.top.document.body.appendChild(button);
window.removeEventListener('DOMContentLoaded', start, false);
};
var traversalUsingTreeWalker = function (node) {
var els = [];
var treeWalker = document.createTreeWalker(node, NodeFilter.SHOW_ELEMENT,null,false);
node = treeWalker.nextNode();
while (node != null) {
// console.log(node.tagName);
var scrollTop = node.scrollTop;
if (scrollTop !== 0) {
els.push(node);
}
node = treeWalker.nextNode();
}
return els;
};
var getScrollElement = function () {
var els = traversalUsingTreeWalker(window.top.document);
fly(els);
};
var fly = function (els) {
window.scrollTo(0, 0);
els.forEach((el) => {
el.scrollTop = 0;
});
};
var domReady = function(callback) {
// console.log('dom ready');
if (document.readyState === "complete" || document.readyState === "interactive") {
setTimeout(callback, 1);
} else {
window.addEventListener('DOMContentLoaded', callback, false);
}
};
domReady(start);
})();