-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
137 lines (121 loc) · 3.5 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
/*
get the position of a node compared to the top of the page
*/
var cumulativeOffset = function(element) {
var top = 0, left = 0;
do {
top += element.offsetTop || 0;
left += element.offsetLeft || 0;
element = element.offsetParent;
} while(element);
return {
top: top,
left: left
};
};
/*
get the index of a node in parent's children list
*/
var indexInParentNode = function(node) {
var i = 1;
while (node = node.previousSibling) {
if (node.nodeType === 1) { ++i; }
}
return i;
};
/*
When the current page changes, run the initialization script again
*/
function hashHandler() {
this.oldHash = window.location.hash;
this.Check;
var that = this;
var detect = function(){
if(that.oldHash!=window.location.hash){
window.setTimeout(initialize, 100);
that.oldHash = window.location.hash;
}
};
this.Check = setInterval(function() { detect(); }, 100);
}
var hashDetection = new hashHandler();
/*
handle debug
*/
var debug = function(message) {
if(shouldDebug) {
console.log(message);
}
};
/*
actually do something
*/
var SCROLLING_AREA_SELECTOR = 'Tm aeJ';
var ACTION_BAR_SELECTOR = 'nH aqK';
var PIXELS_OFFSET = 60;
// memory
var originalActionBarIndex, originlActionBarPosition, scrollingArea;
var actionBarCandidates, actionBar, scrolled, rowToMove;
var processTimeout;
var shouldDebug = true;
var initialize = function() {
if(window.location.hash.indexOf("#inbox") !== -1) {
debug('INITIALIZATION');
try {
actionBar = null;
actionBarCandidates = document.getElementsByClassName(ACTION_BAR_SELECTOR);
/* the ation bar is the last visible element from the former list */
for (var i = actionBarCandidates.length - 1; i >= 0; i--) {
if(actionBarCandidates[i].offsetParent !== null) {
actionBar = actionBarCandidates[i];
break;
}
}
if(actionBar === null) {
throw "nothing visible yet";
}
rowToMove = actionBar.parentNode.parentNode;
originalActionBarIndex = indexInParentNode(rowToMove);
originlActionBarPosition = cumulativeOffset(actionBar);
scrollingArea = document.getElementsByClassName(SCROLLING_AREA_SELECTOR)[0];
scrollingArea.addEventListener("scroll", deferProcess);
debug(actionBar);
debug(rowToMove);
debug(originalActionBarIndex);
debug(originlActionBarPosition);
debug(scrollingArea);
} catch(error) {
debug(error);
window.setTimeout(initialize, 1000);
}
} else { window.setTimeout(initialize, 1000); }
};
var computeIsUp = function(node) {
return [].indexOf.call(node.parentNode.children, node) !== node.parentNode.children.length - 1;
};
// debounce the process function to avoid computing it too often
var deferProcess = function() {
window.clearTimeout(processTimeout);
processTimeout = window.setTimeout(process, 100);
};
var process = function() {
scrolled = scrollingArea.scrollTop;
var isUp = computeIsUp(rowToMove);
debug('-');
debug('top: ' + originlActionBarPosition.top);
debug('scrolled: ' + scrolled);
debug('isUp: ' + isUp);
if(originlActionBarPosition.top < scrolled + PIXELS_OFFSET) {
if(isUp) {
rowToMove.parentNode.appendChild(rowToMove);
isUp = false;
}
}
else {
if(!isUp) {
rowToMove.parentNode.insertBefore(rowToMove, rowToMove.parentNode.children[originalActionBarIndex]);
isUp = true;
}
}
};
window.setTimeout(initialize, 5000);