-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.js
153 lines (131 loc) · 4.66 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
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
const giphyAPIkey = 'dc6zaTOxFJmzC';
const errorGiphyId = 'haZOqHKz9tTfW';
const noResultGiphyId = '14vK3Sc3zepWM0';
const validRatings = ['y', 'g', 'pg', 'pg-13', 'r', 'nsfw', '', 'unrated'];
function setRating(rating) {
chrome.storage.sync.set({
rating: rating
}, function () {});
}
var rating = 'g';
chrome.storage.sync.get('rating', function (items) {
if (validRatings.includes(items.rating)) {
rating = items.rating;
} else {
setRating(rating);
}
});
chrome.storage.onChanged.addListener(function (changes) {
if (changes.rating) {
rating = changes.rating.newValue;
}
});
var addClasses = function (el, ...classes) {
classes.forEach(className => el.classList.add(className));
};
var insertIntoTextarea = function (textarea, text, replaceSelection=true) {
var start = textarea.selectionStart;
var end = textarea.selectionEnd;
if (replaceSelection) {
textarea.value = textarea.value.slice(0, start) + text + textarea.value.slice(end);
textarea.selectionEnd = start + text.length;
} else {
textarea.value = textarea.value.slice(0, end) + text + textarea.value.slice(end);
textarea.selectionEnd = end + text.length;
}
};
var getSelectionInTextarea = function (textarea) {
return textarea.value.slice(textarea.selectionStart, textarea.selectionEnd);
}
// Check if a string could be a giphy id
var isGiphyId = function (string) {
return /[a-zA-Z0-9]{13,19}/.test(string);
}
var getJSON = function (url) {
return new Promise(function (resolve, reject) {
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.onload = function() {
if (this.status >= 200 && this.status < 400) {
resolve(JSON.parse(this.response));
} else {
reject();
}
};
request.onerror = function() {
reject();
};
request.send();
});
};
var getRandomGiphy = function () {
return getJSON(`https://api.giphy.com/v1/gifs/random?api_key=${giphyAPIkey}&rating=${rating}`).then(function (response) {
return response.data;
});
};
var getGiphyByPhrase = function (phrase) {
return getJSON(`https://api.giphy.com/v1/gifs/translate?s=${encodeURI(phrase)}&api_key=${giphyAPIkey}&rating=${rating}`).then(function (response) {
return response.data;
});
};
var getGiphyById = function (id) {
return getJSON(`https://api.giphy.com/v1/gifs/translate?s=${id}&api_key=${giphyAPIkey}`).then(function (response) {
return response.data;
});
}
var formatGiphyMarkdown = function (giphy, altText) {
if (!giphy.id) {
giphy.id = noResultGiphyId;
}
return `![${(altText || giphy.slug || '')}](https://media2.giphy.com/media/${giphy.id}/giphy.gif)`;
};
var handleGIFButtonClick = function(e) {
var textarea = e.target.closest('form').querySelector('textarea');
var selection = getSelectionInTextarea(textarea);
var getGiphy;
e.stopPropagation();
if (selection.length && isGiphyId(selection)) {
getGiphy = getGiphyById(selection);
} else if (selection.length) {
var altText = selection;
getGiphy = getGiphyByPhrase(selection);
} else {
getGiphy = getRandomGiphy();
}
getGiphy.then(giphy => insertIntoTextarea(textarea, formatGiphyMarkdown(giphy, altText)))
.catch(error => insertIntoTextarea(textarea, formatGiphyMarkdown({id: errorGiphyId}, 'Sorry, something went wrong')))
};
var addGiphyToolgroup = function (toolbarEl) {
if (toolbarEl.closest('#js-inline-comments-single-container-template')) {
return;
}
if (toolbarEl.querySelector('.giphy-button')) {
toolbarEl.querySelector('.giphy-button').remove();
}
var toolgroup = document.createElement('div');
addClasses(toolgroup, 'toolbar-group', 'giphy-button');
var giphyButton = document.createElement('button');
giphyButton.innerHTML = 'GIF';
addClasses(giphyButton, 'js-toolbar-item', 'toolbar-item', 'tooltipped', 'tooltipped-nw');
giphyButton.setAttribute('type', 'button');
giphyButton.setAttribute('aria-label', 'Add a random giphy');
giphyButton.setAttribute('data-ga-click', 'Markdown Toolbar, click, giphy');
toolgroup.appendChild(giphyButton);
toolbarEl.appendChild(toolgroup);
giphyButton.addEventListener('click', handleGIFButtonClick, true);
};
var iterateOverToolbars = function (callback) {
var tools = document.querySelectorAll('.toolbar-commenting');
for (let i = 0; i < tools.length; i++) {
callback(tools[i]);
}
};
iterateOverToolbars(addGiphyToolgroup);
var observer = new MutationObserver(function (mutations) {
iterateOverToolbars(addGiphyToolgroup);
});
var config = { attributes: true, childList: true, characterData: true };
var observable = document.querySelector('#js-repo-pjax-container');
if (observable) {
observer.observe(observable, config);
}