-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhover-button.js
More file actions
201 lines (154 loc) · 4.53 KB
/
hover-button.js
File metadata and controls
201 lines (154 loc) · 4.53 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
*
* Sociocaster hover button on images
* Modified from buffer-hover-button extension
*/
;
(function () {
// Do not insert for iframes
if (window !== window.parent) return;
// Do no insert for content editing windows
if (!document.body || document.body.hasAttribute('contenteditable')) return;
// Domain detection (remove www)
var domain = window.location.hostname.replace('www.', '');
var site = {
isGmail: /mail.google.com/.test(domain),
isInstagram: /instagram.com/.test(domain)
};
// List of sites to disable this on:
var disabledDomains = [
'sociocaster.com',
'cstr.com',
'socioplayer.com',
'grafy.io',
'facebook.com'
];
if (disabledDomains.indexOf(domain) > -1) {
return;
}
//chrome.extension.getURL(string path)
/**
* Create a global button
*/
var currentImageUrl = null;
var buttonWidth = 100;
var buttonHeight = 25;
var dpr = window.devicePixelRatio;
var backgroundImage = (dpr && dpr > 1) ?
chrome.extension.getURL('hover-icon@2x.png') :
chrome.extension.getURL('hover-icon@1x.png');
var isButtonVisible = false;
var button = document.createElement('span');
button.id = 'sociocaster-hover-button';
button.setAttribute('style', [
'display: none;',
'position: absolute;',
'z-index: 8675310;',
'width: ' + buttonWidth + 'px;',
'height: ' + buttonHeight + 'px;',
'background-image: url(' + backgroundImage + ');',
'background-size: ' + buttonWidth + 'px ' + buttonHeight + 'px;',
'opacity: 0.9;',
'cursor: pointer;'
].join(''));
var offset = 5;
var image;
var box;
var showButton = function (e) {
image = e.target;
box = image.getBoundingClientRect();
if (box.height < 250 || box.width < 350) return;
button.style.display = 'block';
currentImageUrl = getImageUrl(image);
isButtonVisible = true;
};
var locateButton = function () {
box = image.getBoundingClientRect();
// Use image.width and height if available
var width = image.width || box.width,
height = image.height || box.height,
extraXOffset = 0,
extraYOffset = 0;
// In Gmail, we slide over the button for inline images to not block g+ sharing
if (site.isGmail &&
window.getComputedStyle(image).getPropertyValue('position') !== 'absolute') {
extraXOffset = 83;
extraYOffset = 4;
}
var x = window.pageXOffset + box.left + width - buttonWidth - offset - extraXOffset;
var y = window.pageYOffset + box.top + height - buttonHeight - offset - extraYOffset;
// If body is positioned, the button will be positioned against it. So, if body is positioned and shifted
// up or down, or is being shifted up or down by a children having a top margin other than 0, account for
// that additional vertical offset.
var isBodyPositioned = window.getComputedStyle(document.body).getPropertyValue('position') != 'static';
if (isBodyPositioned) {
var bodyTopOffset = document.body.getBoundingClientRect().top + window.pageYOffset;
y -= bodyTopOffset;
}
button.style.top = y + 'px';
button.style.left = x + 'px';
};
var onScroll = function () {
if (isButtonVisible) locateButton();
};
var hoverButton = function () {
button.style.opacity = '1.0';
button.style.display = 'block';
};
var hideButton = function (e) {
button.style.display = 'none';
button.style.opacity = '0.9';
isButtonVisible = false;
};
var onMouseleaveButton = function () {
hideButton();
};
var onImageMouseEnter = function (e) {
showButton(e);
locateButton();
};
var shareImage = function (e) {
if (!currentImageUrl) return;
e.preventDefault();
injectPoster('', currentImageUrl, '');
};
$(button)
.on('click', shareImage)
.on('mouseenter', hoverButton)
.on('mouseleave', onMouseleaveButton);
var getImageUrl = (function (domain) {
if (site.isInstagram) {
return function (el) {
return el.style.backgroundImage
.replace('url(', '')
.replace(')', '');
};
}
return function (el) {
return el.src;
};
}(domain));
var addButtonImageOverlays = function () {
var selector = 'img';
if (site.isInstagram) {
selector = '.Image.timelinePhoto, .Image.Frame';
}
document.body.appendChild(button);
$(document)
.on('mouseenter', selector, onImageMouseEnter)
.on('mouseleave', selector, hideButton);
// scroll events don't bubble, so we listen to them during their capturing phase
window.addEventListener('scroll', onScroll, true);
};
addButtonImageOverlays();
/*
chrome.storage.sync.get({
isImageEnable: true,
isFacebookEnable: true
}, function(items) {
if(items.isImageEnable){
addButtonImageOverlays();
}
});
*/
})();