-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathScripts.html
208 lines (170 loc) · 6.8 KB
/
Scripts.html
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
// constants
var REFRESH_INTERVAL = 1000; // update selected text inspector every 1 second
var TIMEOUT = 20000; // timout of 20s for fetching recommendations
// global variables
var lastSelectedText;
var lastSearchedKeywords;
var searchResultList;
/**
* On document load.
*/
$(function() {
// init search bar
$("#eexcess-search").keyup(function (e) {
if (e.keyCode == 13) { // Enter keycode
$('#btn-search').click();
}
});
$('#btn-search').click(getRecommendationsFromSearchBarInput);
// init search results list
searchResultList = new SearchResultList();
$('#settings-button').click(openSettings);
inspectSelectedText();
});
function openSettings() {
google.script.run.openSettingsDialog();
}
/**
* Runs a server-side function to get the recommendations for the user-entered text entered into the search bar and
* updates the sidebar UI with the results from the privacy proxy.
*/
function getRecommendationsFromSearchBarInput() {
var searchInput = $("#eexcess-search").val();
var subStrings = searchInput.split('"');
var keywords = [];
for (var subStringIdx = 0; subStringIdx < subStrings.length; subStringIdx++) {
var str = subStrings[subStringIdx].trim();
if ((subStringIdx % 2) == 1) { // odd -> string
str = removePunctuationMarks(str);
if (str !== "")
keywords.push(str);
} else { // even -> single keyword(s)
var singleWords = str.split(' ');
for (var singleWordIdx = 0; singleWordIdx < singleWords.length; singleWordIdx++) {
var word = singleWords[singleWordIdx];
word = removePunctuationMarks(word);
if (word !== "")
keywords.push(word);
}
}
}
if (keywords.length === 0) {
searchResultList.showError('<?!= msg('SEARCHBAR_EMPTY') ?>');
} else {
this.disabled = true;
fetchAndDisplayRecommendations(keywords, this);
}
}
/**
* Removes punctuation marks from the given term.
*
* @param {String} term
* @return {String} term without punctuation marks
*/
function removePunctuationMarks(term) {
return term.replace(/[\.,#-\/!$%\^&\*;:{}=\-_`~()“”]/g,"");
}
/**
* Checks if the current selected text in the document has changed. If it changed automatically a search request
* will be submitted to the recommender.
*/
function inspectSelectedText() {
google.script.run
.withSuccessHandler(
function(selectedText) {
if (selectedText.length > 0 && !arraysEqual(selectedText, lastSelectedText)) {
lastSelectedText = selectedText;
// extract keywords
var keywords = [];
// Split the text into keywords
for (var selectedTextIdx = 0; selectedTextIdx < selectedText.length; selectedTextIdx++) {
var text = selectedText[selectedTextIdx];
var words = text.split(" ");
for (var wordIdx = 0; wordIdx < words.length; wordIdx++) {
var word = words[wordIdx];
word = removePunctuationMarks(word);
if (word !== "")
keywords.push(word);
}
}
if (keywords.length > 0) {
var keywordString = keywords[0];
for (var keywordIdx = 1; keywordIdx < keywords.length; keywordIdx++) {
keywordString += " " + keywords[keywordIdx];
}
$("#eexcess-search").val(keywordString);
}
}
})
.withFailureHandler(
function() {
// ignore errors
})
.getSelectedText();
window.setTimeout(inspectSelectedText, REFRESH_INTERVAL);
}
/**
* Runs a server-side function to fetch the recommendations for the given text and updates the sidebar UI with the
* results from the privacy proxy or displays and error message in the sidebar.
*
* @param {String} or {Array<String>} text text for which the recommendations should be fetched
* @param button disabled button which triggered the action or undefined if action was not triggered by a button
*/
function fetchAndDisplayRecommendations(keywords, button){
searchResultList.showAjaxLoader();
var responded = false;
var timeout = false;
lastSearchedKeywords = keywords;
setTimeout(function() {
if(!responded) {
timeout = true;
if (lastSearchedKeywords == keywords)
searchResultList.showError("<?!= msg('TIMEOUT') ?>");
if (button)
button.disabled = false;
}
}, TIMEOUT);
google.script.run
.withSuccessHandler(
function(recommendations, button) {
if(!timeout && lastSearchedKeywords == keywords){
responded = true;
searchResultList.showResults(recommendations);
if (button)
button.disabled = false;
}
})
.withFailureHandler(
function(errorMsg, button) {
if(!timeout && lastSearchedKeywords == keywords) {
responded = true;
searchResultList.showError(errorMsg);
if (button)
button.disabled = false;
}
})
.withUserObject(button)
.fetchRecommendations(keywords);
}
/**
* Compares two 1-dimensional arrays by their content.
*
* @param a array 1
* @param b array 2
*/
function arraysEqual(a, b) {
if (a === b)
return true;
if (a == null || b == null)
return false;
if (a.length != b.length)
return false;
for (var i = 0; i < a.length; ++i) {
if (a[i] !== b[i])
return false;
}
return true;
}
</script>