-
Notifications
You must be signed in to change notification settings - Fork 4
/
content.js
80 lines (65 loc) · 1.94 KB
/
content.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
var mtb = mtb || {};
mtb.randomShipit = mtb.randomShipit || {};
(function() {
'use strict';
var self = this;
var shipitsArr = [
':shipit:',
':ship: :it:',
':sheep: :it:',
':sheep:',
':ship: :it:',
':boat: :it:',
':speedboat: :it:',
':rocket:',
':ok:',
':ok_hand:',
':thumbsup:',
':100:'
// '![image](http://i.imgur.com/DOs4yj8.gif)'
];
this.init = function() {
self.appendShipitButton();
self.setupBindings();
};
this.appendShipitButton = function() {
var imageUrl = 'https://assets-cdn.github.com/images/icons/emoji/shipit.png';
if (this.isDecember()) {
imageUrl = chrome.extension.getURL('images/xmas/38x38.png');
}
$('.js-comment-and-button').after('<button type="button" class="btn js-random-shipit"><span class="btn-text">Random</span><img class="btn-icon" height="20" width="20" src="' + imageUrl + '"></button>');
};
this.isDecember = function() {
var date = new Date();
var month = date.getMonth();
return month === 11;
};
this.setupBindings = function() {
$(document).on('click', '.js-random-shipit', self.getRandomShipit);
var gitHubElWrapper = document.getElementById('js-pjax-loader-bar');
var observer = new window.MutationObserver(function(mutations, observer) {
// fired when a mutation occurs
if ($('.js-random-shipit').length === 0) {
self.appendShipitButton();
}
});
// define what element should be observed by the observer
// and what types of mutations trigger the callback
observer.observe(gitHubElWrapper, {
subtree: false,
attributes: true
});
};
this.getRandomShipit = function() {
var rnd = Math.floor(Math.random() * shipitsArr.length);
var val = shipitsArr[rnd];
var textToAdd = $('#new_comment_field').val() + ' ' + val;
$('#new_comment_field').val(val);
self.submitForm();
};
this.submitForm = function() {
$('.js-new-comment-form').submit();
$('#new_comment_field').val('');
};
}).apply(mtb.randomShipit);
mtb.randomShipit.init();