forked from estum/Google-Image-Search-safariextension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_image_search.js
166 lines (129 loc) · 4.43 KB
/
google_image_search.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
var allowSearchByBackgroundImage = false;
function requestSettings() {
safari.self.tab.dispatchMessage("requestSettings", true);
}
function getSettings(theMessageEvent) {
if (theMessageEvent.name === "settings") {
allowSearchByBackgroundImage = theMessageEvent.message;
}
}
safari.self.addEventListener("message", getSettings, false);
requestSettings();
// allowSearchByBackgroundImage(getSettings);
function handleContextMenu(event) {
var sendUserInfo = {
'ifImage': false,
'imageSrc': ''
};
if (event.target.nodeName === 'IMG') {
sendUserInfo.ifImage = true;
sendUserInfo.imageSrc = event.target.src;
}
else if (allowSearchByBackgroundImage) {
var bgImage = getElementsWithBackgroundImage(event.target, event.pageX, event.pageY);
if (bgImage) {
sendUserInfo.ifImage = true;
sendUserInfo.imageSrc = bgImage;
}
}
safari.self.tab.setContextMenuEventUserInfo(event, sendUserInfo);
}
document.addEventListener("contextmenu", handleContextMenu, false);
function getElementsWithBackgroundImage(element, x, y) {
if (isElementHaveBackgroundImage(element)) {
return parseBackgroundImage(element);
}
else {
var elementsIntersectingPoint = getElementsIntersectingPoint(
document, x, y, '*',
isElementHaveBackgroundImage
);
if (elementsIntersectingPoint.length > 0) {
return parseBackgroundImage(elementsIntersectingPoint[elementsIntersectingPoint.length-1]);
}
}
return false;
}
function isElementHaveBackgroundImage(element) {
var backgroundImage = getStyle(element, 'backgroundImage');
return (backgroundImage && backgroundImage !== 'none') ? true : false;
}
function parseBackgroundImage(element) {
return getStyle(element, 'backgroundImage').replace(/^url\(|\)$/g,'');
}
function getStyle(element, strCssRule){
var strValue = '';
if (element && document.defaultView && document.defaultView.getComputedStyle){
var computed = document.defaultView.getComputedStyle(element, '');
if (computed) {
strValue = computed[strCssRule];
}
}
return strValue;
}
/**
* http://neverfear.org/blog/view/36/JavaScript_tip_How_to_find_the_document_elements_that_intersect_at_a_certain_coordinate
*
* Get the offset coordinates of the element.
* @param {Object} element The element.
* @return {Object} An object with four properties named x1, x2, y1 and y2 containing the elements left, right, top and bottom edges respectively.
*/
function getElementPosition(element) {
var elementX = 0,
elementY = 0,
elementW = element.offsetWidth,
elementH = element.offsetHeight;
while (element.offsetParent) {
elementX += element.offsetLeft;
elementY += element.offsetTop;
element = element.offsetParent;
}
elementX += element.offsetLeft;
elementY += element.offsetTop;
elementW += elementX;
elementH += elementY;
return {
x1: elementX,
y1: elementY,
x2: elementW,
y2: elementH
};
}
/**
* http://neverfear.org/blog/view/36/JavaScript_tip_How_to_find_the_document_elements_that_intersect_at_a_certain_coordinate
*
* Finds all elements that intersects with the given coordinates.
* @param {Object} doc The document DOM object.
* @param {Integer} x The X coordinate of the doc.
* @param {Integer} y The Y coordinate of the doc.
* @param {String} [tagName] An optional tag name filter.
* @param {Function} [cmpCallback] A call back function to test whether or not an element should be included in the results (such as testing element id's, values, etc). The call back should return true or false.
* @return {Array} Returns a primitive array of elements that intersects with point (x, y).
*/
function getElementsIntersectingPoint(doc, x, y, tagName, cmpCallback) {
var elements = [],
results = [],
i = 0,
element = null,
pt = null;
if ( tagName === undefined || tagName === null) {
tagName = "*";
}
if (cmpCallback === undefined || cmpCallback === null) {
cmpCallback = function (e) {
return true;
};
}
elements = doc.getElementsByTagName(tagName);
for (i = 0; i < elements.length; i++) {
element = elements[i];
if (cmpCallback(element)) {
pt = getElementPosition(element);
if (x >= pt.x1 && x <= pt.x2 && y >= pt.y1 && y <= pt.y2) {
results[results.length] = element;
continue;
}
}
}
return results;
}