-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathsVimHelper.js
150 lines (132 loc) · 4.08 KB
/
sVimHelper.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
// Create helper object
var sVimHelper = {};
var animationFrame
// Determines if the element given is an input type
sVimHelper.isElementInput = function(element) {
return (
(element.localName === "textarea" || element.localName === "input" || element.getAttribute("contenteditable") === "true")
&& !element.disabled
&& !/button|radio|file|image|checkbox|submit/i.test(element.getAttribute("type"))
);
};
// Determines if the element given is visible
sVimHelper.isElementVisible = function(element) {
return (
element.offsetParent
&& !element.disabled
&& element.getAttribute("type") !== "hidden"
&& getComputedStyle(element).visibility !== "hidden"
&& element.getAttribute("display") !== "none"
);
}
// Determines if the element given is in the viewport
sVimHelper.isElementInView = function(element) {
var rect = element.getClientRects()[0];
return (
rect.top + rect.height >= 0
&& rect.left + rect.width >= 0
&& rect.right - rect.width <= window.innerWidth
&& rect.top < window.innerHeight
);
};
// Scroll by, smooth or not
sVimHelper.scrollBy = function(x, y) {
// If smooth scroll is off then use regular scroll
if (!sVimTab.settings.smoothscroll) {
scrollBy(x, y);
return;
}
window.cancelAnimationFrame(animationFrame)
// Smooth scroll
var i = 0;
var delta = 0;
// Ease function
function easeOutExpo(t, b, c, d) {
return c * (-Math.pow(2, -10 * t / d ) + 1 ) + b;
}
// Animate the scroll
function animLoop() {
var toScroll = Math.round(easeOutExpo(i, 0, y, sVimTab.settings.scrollduration) - delta);
if (toScroll != 0) {
if (y) {
window.scrollBy(0, toScroll);
} else {
window.scrollBy(toScroll, 0);
}
}
if (i < sVimTab.settings.scrollduration) {
animationFrame = window.requestAnimationFrame(animLoop);
}
delta = easeOutExpo(i, 0, (x || y), sVimTab.settings.scrollduration);
i += 1;
}
// Start scroll
animLoop();
};
// Checks if location @matches the pattern (/~https://github.com/1995eaton/chromium-vim/blob/master/content_scripts/utils.js)
sVimHelper.matchLocation = function(location, pattern) {
// Check pattern is non-empty string
if (typeof pattern !== "string" || !pattern.trim()) {
return false;
}
var protocol = (pattern.match(/.*:\/\//) || [""])[0].slice(0, -2);
var hostname;
var path;
var pathMatch;
var hostMatch;
// Check the pattern is a pattern
if (/\*\*/.test(pattern)) {
console.error("sVim - Invalid pattern: " + pattern);
return false;
}
// Check protocol is in pattern
if (!protocol.length) {
console.error("sVim - Invalid protocol in pattern: ", pattern);
return false;
}
// Check protocol mismatch
if (protocol !== "*:" && location.protocol !== protocol) {
return false;
}
// Check host mismatch
pattern = pattern.replace(/.*:\/\//, "");
if (location.protocol !== "file:") {
hostname = pattern.match(/^[^\/]+\//g);
if (!hostname) {
console.error("sVim - Invalid host in pattern: ", pattern);
return false;
}
var origHostname = hostname;
hostname = hostname[0].slice(0, -1).replace(/([.])/g, "\\$1").replace(/\*/g, ".*");
hostMatch = location.hostname.match(new RegExp(hostname, "i"));
if (!hostMatch || hostMatch[0].length !== location.hostname.length) {
return false;
}
pattern = "/" + pattern.slice(origHostname[0].length);
}
// Check path mismatch
if (pattern.length) {
path = pattern.replace(/([.&\\\/\(\)\[\]!?])/g, "\\$1").replace(/\*/g, ".*");
pathMatch = location.pathname.match(new RegExp(path));
if (!pathMatch || pathMatch[0].length !== location.pathname.length) {
return false;
}
}
return true;
};
// Copy text to clipboard
sVimHelper.copyToClipboard = function(text) {
var input = document.createElement('input');
input.setAttribute('value', text);
document.body.appendChild(input);
input.select();
document.execCommand('copy');
document.body.removeChild(input);
};
sVimHelper.inIframe = function () {
try{
return window.self !== window.top;
}catch(e) {
return true;
}
}