-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontentscript.js
83 lines (68 loc) · 2.92 KB
/
contentscript.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
(function($) {
var searchButtonOld = "<div class='ext-search-container'>" +
"<a href='https://www.google.com/searchbyimage?image_url=#IMAGE_URL#&hl=en' " +
"class='Button11 WhiteButton ext-search editbutton ContrastButton Button' target='_blank'>" +
"<strong><em style='background: url("+chrome.extension.getURL('ext_search_action.png')+")'></em>" +
"Search</strong><span></span></a></div>";
var searchButton = "<button type='button' class='rounded Button likeSmall btn hasText searchButton'>\n" +
"<em style='background: url("+chrome.extension.getURL('ext_search_action_2.png')+")'></em>\n" +
"<span class='buttonText'>Search</span>\n</button>"
var gaTrack = function() {
chrome.extension.sendRequest({'track': true});
};
var searchButtonClicked = function(event) {
var src = $(event.currentTarget).closest("div.Pin").find(".pinImageWrapper img.image").attr("src");
event.stopImmediatePropagation();
event.preventDefault();
gaTrack();
window.open("https://www.google.com/searchbyimage?image_url="+encodeURIComponent(src)+"&hl=en", "_blank");
return false;
};
var injectSearch = function() {
if ($("div.pin").length) {
injectSearchOldDesign();
return;
}
// Find all pins without the search button.
var newPins = $("div.Pin").filter(function() {
var $el = $(this);
return $el.find(".repinLikeWrapper > .searchButton").length === 0 &&
$el.find(".pinImageWrapper img.image").length && $el.find(".pinImageWrapper img.image.videoIndicator").length === 0;
});
// Append search button with image url.
newPins.each(function() {
var $el = $(this);
var $search = $(searchButton);
$el.find(".repinLikeWrapper").append($search);
$search.click(searchButtonClicked);
});
};
var injectSearchOldDesign = function() {
// Find all pins without the search button.
var newPins = $("div.pin").filter(function() {
var $el = $(this);
return $el.find(".actions").length &&
$el.find(".actions > .ext-search-container").length === 0 &&
$el.find("a.PinImage.ImgLink > img").length && $el.find("a.PinImage.ImgLink > img.video").length === 0;
});
// Append search button with image url.
newPins.each(function() {
var $el = $(this);
var src = $el.find("a.PinImage.ImgLink > img").attr("data-src") || $el.find("a.PinImage.ImgLink > img").attr("src");
src = src.replace("_b.jpg", "_f.jpg");
var $search = $(new String(searchButtonOld).replace("#IMAGE_URL#", encodeURIComponent(src)));
$el.find(".actions").append($search);
$search.click(gaTrack);
});
};
injectSearch();
$(window).bind("scroll", function() {
setTimeout(injectSearch, 500);
});
$("#NewIndicator").click(function() {
setTimeout(injectSearch, 500);
});
$('.mainContainer').mouseenter(function() {
setTimeout(injectSearch, 500);
});
})(jQuery);