-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathpopup.js
189 lines (156 loc) · 5.6 KB
/
popup.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
var MIN_WIDTH = 50;
var MIN_HEIGHT = 50;
var MIN_AREA = MIN_WIDTH * MIN_HEIGHT * 2;
var MAX_RATIO = 4;
var existing_image_urls = [];
var validate_image = function (image_url) {
var def = $.Deferred();
if (existing_image_urls.indexOf(image_url) !== -1) {
def.reject();
}
existing_image_urls.push(image_url);
var image = $('<img>').attr('src', image_url);
image.imagesLoaded().done(function (image) {
if (typeof image === 'undefined') {
def.reject();
}
image = $(image);
if (!image.length) {
def.reject();
}
var width = image.get(0).width, height = image.get(0).height;
if (width < MIN_WIDTH ||
height < MIN_HEIGHT ||
width * height < MIN_AREA ||
width / height > MAX_RATIO ||
height / width > MAX_RATIO) {
def.reject();
}
def.resolve(image);
}).fail(function () {
def.reject();
});
return def;
};
var get_image = function (image_url) {
var def = $.Deferred();
validate_image(image_url).done(function (image) {
var width = image.get(0).width;
var height = image.get(0).height
def.resolve({
node: image,
width: width,
height: height,
area: width * height,
});
}).fail(function () {
def.reject();
});
return def;
};
var parse_url = function (url) {
var l = document.createElement("a");
l.href = url;
return l;
};
var show_no_images_message = function () {
$('#loading').hide();
$('#images').hide();
$('#no_images').show();
};
var show_repost_message = function (options) {
if (typeof options === 'undefined') { options = {}; }
options = $.extend({
message: "It looks like you're trying to repost an image from Canvas.",
savnac: false,
}, options);
var clippy = $('#clippy');
$('#loading').hide();
$('#images').hide();
if (options.savnac) {
clippy.addClass('savnac');
}
clippy.find('.message p').first().text(options.message);
clippy.show();
$('#clippy input[type="text"]').focus();
};
var add_images = function (image_urls) {
var images = [];
var deferreds = [];
if (!image_urls.length) {
show_no_images_message();
return;
}
$.each(image_urls, function (_, image_url) {
var def = get_image(image_url)
deferreds.push(def);
def.done(function (image) {
images.push(image);
});
});
$.whenAll.apply(null, deferreds).always(function () {
$('#loading').hide();
if (!images.length) {
show_no_images_message();
return;
}
images.sort(function (a, b) {
a = a.area, b = b.area;
return (a < b) ? -1 : (a > b) ? 1 : 0;
});
var container = $('#images');
$.each(images, function (_, image) {
var url = $(image.node).attr('src');
var image_wrapper = $('<div></div>').addClass('image').prependTo(container);
image_wrapper.css('background-image', 'url(' + url + ')');
if (image.width < 150 && image.height < 150) {
image_wrapper.css('background-size', 'auto');
}
var overlay = $('<div></div>').addClass('overlay').appendTo(image_wrapper);
$('<img>').addClass('label').attr('src', 'remix_button_large.png').appendTo(overlay);
overlay.click(function () {
//chrome.tabs.getCurrent(function (tab) {
chrome.windows.getCurrent(function (win) {
chrome.tabs.query({ 'windowId': win.id, 'active': true }, function (tabs) {
var remix = function () {
var remix_url = 'https://example.com/post_thread/popup?upload_url=' + encodeURIComponent(url);
window.open(remix_url, '_blank', 'resizable=yes,toolbar=no,location=no,scrollbars=yes,menubar=yes,status=yes,width=950,height=700');
};
if (!tabs.length) {
remix();
return;
}
var tab = tabs[0];
var url_parts = parse_url(tab.url);
var tab_host = url_parts.hostname;
var tab_path = url_parts.pathname;
if (tab_host === 'example.com' && tab_path === '/user/photocopier') {
show_repost_message({
message: "It looks like you're trying to repost something awesome from the guy that drew me.",
});
} else if (tab_host === 'example.com') {
show_repost_message();
} else if (tab_host === 'savn.ac') {
show_repost_message({ savnac: true });
} else {
remix();
}
});
});
});
});
});
};
$(function () {
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
if (request.action === 'add_images') {
add_images(request.image_urls);
}
});
chrome.tabs.executeScript(null, { file: "jquery-1.7.2.min.js" }, function () {
chrome.tabs.executeScript(null, { file: "grab_images.js" });
});
$('.clippy button.cancel').click(function (event) {
window.close();
});
});