-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.js
executable file
·113 lines (82 loc) · 3.25 KB
/
main.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
var gmail;
function refresh(f) {
if( (/in/.test(document.readyState)) || (undefined === Gmail) ) {
setTimeout('refresh(' + f + ')', 1);
} else {
f();
}
}
var main = function(){
var typingTimer;
gmail = new Gmail();
//Register events if composes are open
gmail.observe.on("compose", function(compose, type) {
$('.Ap').on("keyup", function(){
window.clearTimeout(typingTimer);
typingTimer = window.setTimeout(doneTyping , 1000);
});
$('.Ap').on("keydown", function(){
window.clearTimeout(typingTimer);
});
$('.Ap').on("click", ".gifLine", function(){
var separator = $("<span />", {
"class": "a5u",
"style": "-webkit-user-select: none;"
}).html("|");
var element = $("<span />", {
"class": "Lh",
"id": "reload-image",
"style": "-webkit-user-select: none;"
}).html("Reload Gif");
element.click({gif: this}, reloadGif);
$('.a5s').find("#tr_sizes-div").append(separator).append(element);
//$(this).closest( "table" ).next("");
});
});
function reloadGif(event) {
var keyword = $(event.data.gif).attr("title");
$.ajax({
url: 'https://tv.giphy.com/v1/gifs/random?api_key=xTiTnvpvz9ucREOFRS&tag='+keyword,
cache: false
}).done(function( obj ) {
var data = obj.data.image_url;
data = data.replace("http://", "https://");
var string = '<img src = "'+data+'" class="gifLine" title="'+keyword+'">';
//$(event.data.gif).attr("src", data);
var composes = gmail.dom.composes();
$.each(composes, function(key,email){
var t = $(event.data.gif).map(function() { return this.outerHTML; }).get().join("");
body = email.body().replace(t, string);
email.body(body);
$("body").find("#default-resize-image").trigger("click");
});
});
}
function doneTyping() {
var composes = gmail.dom.composes();
$.each(composes, function(key,email){
var body = email.body();
var commandLine = body.match(/(\/|::)gif(me| me)? ([\w\??| ]+)/i);
if (commandLine != null) {
var keyword = commandLine[3].replace(" ", "+");
var replace = commandLine[0];
var string;
$.ajax({
url: 'https://tv.giphy.com/v1/gifs/random?api_key=xTiTnvpvz9ucREOFRS&tag='+keyword,
cache: false
}).done(function( obj ) {
var data = obj.data.image_url;
data = data.replace("http://", "https://");
if (data) {
string = '<img src = "'+data+'" class="gifLine" title="'+keyword+'">';
} else {
string = '<em>I can\'t find any GIF with this reaction.</em>';
}
body = body.replace(replace, string);
email.body(body);
});
}
});
}
}
refresh(main);