-
Notifications
You must be signed in to change notification settings - Fork 8
/
pageTest.js
336 lines (281 loc) · 12.8 KB
/
pageTest.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
const locationSplitTest = location.href.split('/');
// load only in simulator
if (locationSplitTest.length >= 7 && locationSplitTest[6] === 'test') {
// Icons by Eleonor Wang - https://www.flaticon.com/authors/eleonor-wang
// <div>Icons made by <a href="https://www.flaticon.com/authors/eleonor-wang" title="Eleonor Wang">Eleonor Wang</a> from <a href="https://www.flaticon.com/" title="Flaticon">www.flaticon.com</a> is licensed by <a href="http://creativecommons.org/licenses/by/3.0/" title="Creative Commons BY 3.0" target="_blank">CC 3.0 BY</a></div>
const redoImage = `<img src="data:image/svg+xml;utf8;base64,PD94bWw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NTItMC42NzktMC4wNTItMS4wMWwzMS4zNzUtNTEuMTk0YzEuNzM5LTUuMjIxLTAuMDAxLTEwLjQ0Mi0zLjQ4MS0xMi4xODJ6IiBmaWxsPSIjMDAwMDAwIi8+CiAgPC9nPgo8L3N2Zz4K" />`;
const saveImage = `<img src="data:image/svg+xml;utf8;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iaXNvLTg4NTktMSI/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHZlcnNpb249IjEuMSIgdmlld0JveD0iMCAwIDIyNi4yMTYgMjI2LjIxNiIgZW5hYmxlLWJhY2tncm91bmQ9Im5ldyAwIDAgMjI2LjIxNiAyMjYuMjE2IiB3aWR0aD0iMTZweCIgaGVpZ2h0PSIxNnB4Ij4KICA8Zz4KICAgIDxwYXRoIGQ9Im0yMTcuNTE1LDEwNC40MDdoLTk1LjcwN3YtOTUuNzA2YzAtNS4yMi0zLjQ4LTguNzAxLTguNzAxLTguNzAxcy04LjcwMSwzLjQ4LTguNzAxLDguNzAxdjk1LjcwN2gtOTUuNzA1Yy01LjIyLDAtOC43MDEsMy40OC04LjcwMSw4LjcwMXMzLjQ4LDguNzAxIDguNzAxLDguNzAxaDk1LjcwN3Y5NS43MDdjMCw1LjIyIDMuNDgsOC43MDEgOC43MDEsOC43MDFzOC43MDEtMy40OCA4LjcwMS04LjcwMXYtOTUuNzA3aDk1LjcwN2M1LjIyLDAgOC43MDEtMy40OCA4LjcwMS04LjcwMXMtMy40ODMtOC43MDItOC43MDMtOC43MDJ6IiBmaWxsPSIjMDAwMDAwIi8+CiAgPC9nPgo8L3N2Zz4K" />`;
const skillId = location.href.split('/')[7];
let localeButtons;
let locale = location.href.split('/')[9].substring(0, 2);
let historyIndex = -1;
$(document).ready(function () {
draggableSideBar();
initButtons();
$('.askt-input div.astro-form-group-fields label:first-child input').on('change', function () {
initButtons();
});
$("body").on('DOMSubtreeModified', ".askt-alexa-lang .Select-control .Select-value-label", function() {
// workaround! there is always a delay
setTimeout(() => {
const newLocale = location.href.split('/')[9].substring(0, 2);
if (newLocale !== locale) {
locale = newLocale;
}
initButtons();
}, 750);
});
});
async function getStorage() {
return new Promise((resolve, reject) => {
chrome.storage.sync.get([skillId], function(result) {
resolve(result);
});
})
}
async function setStorage() {
return new Promise((resolve, reject) => {
chrome.storage.sync.set({[skillId]: result[skillId]}, function() {
$('.request-buttons button[value="'+button+'"]').parent().remove();
localeButtons = result[skillId].buttons[locale];
});
})
}
async function updateStorage() {
let result = await getStorage();
if (result && result[skillId] && result[skillId].buttons) {
const buttonTextsArray = [];
$('.asksos-button.post').each(function() {
buttonTextsArray.push($(this).text().trim());
});
result[skillId].buttons[locale] = buttonTextsArray;
chrome.storage.sync.set({[skillId]: result[skillId]}, function() {
localeButtons = result[skillId].buttons[locale];
});
}
}
/**
* Gets buttons from storage and adds to container
*/
function initButtons() {
if ($('div.request-buttons').length === 0) {
$('<div class="request-buttons"></div>').insertAfter('.askt-simulator__input');
}
$('.request-buttons').html('');
chrome.storage.sync.get([skillId], function(result) {
if (result && result[skillId] && result[skillId].buttons) {
localeButtons = result[skillId].buttons[locale];
if (localeButtons) {
localeButtons.forEach((button) => {
addButton(button);
});
}
$( ".request-buttons" ).sortable({
placeholder: "button-placeholder",
stop: function( event, ui ) {
console.log('stop');
// console.log(event);
// $('.asksos-button.post').each(function(item) {
// console.log($(this).text().trim());
// })
updateStorage();
}
});
$( ".request-buttons" ).disableSelection();
}
});
$('.request-buttons').off();
$('.request-buttons').on('click','button.post', function() {
postText($(this).val());
});
$('.request-buttons').on('click','button.remove', function() {
removeButton($(this).val());
});
$(".askt-dialog").bind("DOMNodeInserted",function(el){
if($(el.target).hasClass('askt-dialog__message') &&
$(el.target).hasClass('askt-dialog__message--request')) {
$(el.target).parent().find('.askt-dialog__icon--plus').remove();
$(el.target).prepend(`<span class="asksos-action-icon repost" title="Repost">${redoImage}</span>`);
// show, if it is not saved already
if (!localeButtons || !localeButtons.includes($(el.target).parent().text())) {
$(el.target).prepend(`<span class="asksos-action-icon save" title="Add request button">${saveImage}</span>`);
}
//add to history
addToHistory($(el.target).parent().text());
}
});
// $('.askt-dialog').off();
$('.askt-dialog').on('click', '.repost', function() {
const text = $(this).parent().text();
postText(text);
});
$('.askt-dialog').on('click', '.save', function() {
const text = $(this).parent().text();
saveButton(text);
});
$(".react-autosuggest__input").keydown(function(e) {
chrome.storage.sync.get([skillId], function(result) {
if (result && result[skillId] && result[skillId].history) {
commandHistory = result[skillId].history[locale];
if(commandHistory) {
switch(e.key) {
case "ArrowUp": historyIndex < commandHistory.length-1 ? historyIndex ++ : historyIndex = 0; break;
case "ArrowDown": historyIndex > 0 ? historyIndex-- : historyIndex = 0; break;
default: return;
}
}
// Trigger the change event on the input
var ev = new Event('input', { bubbles: true});
ev.simulated = true;
$(".react-autosuggest__input")[0].value = commandHistory[historyIndex];
$(".react-autosuggest__input")[0].dispatchEvent(ev);
}
});
});
}
/**
* Adds button to container
* @param button
*/
function addButton(button) {
$('.request-buttons').append(`<div class="button-container"><button class="asksos-button post" value="${button}" title="Post ${button}">${button}</button><button class="asksos-button remove" value="${button}" title="Remove button">x</button></div>`);
}
/**
* Removes button from storage and container
* @param button
*/
function removeButton(button) {
chrome.storage.sync.get([skillId], function(result) {
if (result && result[skillId] && result[skillId].buttons) {
result[skillId].buttons[locale] = result[skillId].buttons[locale].filter((text) => text !== button);
chrome.storage.sync.set({[skillId]: result[skillId]}, function() {
$('.request-buttons button[value="'+button+'"]').parent().remove();
localeButtons = result[skillId].buttons[locale];
});
}
});
}
/**
* Posts text
* @param text
*/
function postText(text) {
console.log('posting: ' + text);
$('.react-autosuggest__input').val(text).change();
// trigger input
$('.react-autosuggest__input')[0].dispatchEvent(new Event("input", { 'bubbles': true,'cancelable': true }));
$('.react-autosuggest__input').focus();
// trigger press on enter
const keypress = new KeyboardEvent("keypress", {
bubbles: true, cancelable: true, keyCode: 13, key: 'Enter'
});
$('.react-autosuggest__input')[0].dispatchEvent(keypress);
}
/**
* Saves button to synced chrome storage
* @param text
*/
function saveButton(text) {
// get object for given skill id
chrome.storage.sync.get([skillId], function(result) {
if (result && result[skillId] && result[skillId].buttons) {
localeButtons = result[skillId].buttons[locale];
// push to existing or create new
if (localeButtons) {
if (!localeButtons.includes(text)) {
localeButtons.push(text);
}
} else {
result[skillId].buttons[locale] = [text];
}
chrome.storage.sync.set({[skillId]: result[skillId]}, function () {
addButton(text);
});
} else if (result && result[skillId]) {
result[skillId].buttons = {
[locale]: [text]
};
chrome.storage.sync.set({[skillId]: result[skillId]}, function () {
addButton(text);
});
} else { // create new object for given skill id
const obj = {
buttons: {
[locale]: [text]
},
history: {
[locale]: []
},
};
chrome.storage.sync.set({[skillId]: obj}, function() {
addButton(text);
});
}
});
}
//
/**
* Add new entry to history
* @param text
*/
function addToHistory(text) {
// Get object for given skill id
chrome.storage.sync.get([skillId], function(result) {
// Set index back to starting point
historyIndex = -1;
if (result && result[skillId] && result[skillId].history) {
commandHistory = result[skillId].history[locale];
// Add entry to front, and remove it if already exists
if (commandHistory) {
let index = commandHistory.indexOf(text);
if (index >= 0) {
commandHistory.splice(index, 1);
}
// Cap length at 50 to prevent this from growing too large.
if (commandHistory.length > 50) {
commandHistory.length = 50;
}
commandHistory.unshift(text);
} else {
result[skillId].history[locale] = [text];
}
chrome.storage.sync.set({[skillId]: result[skillId]});
} else if (result && result[skillId]) { // create new object for given skill id
result[skillId].history = {
[locale]: [text]
};
chrome.storage.sync.set({[skillId]: result[skillId]});
} else {
const obj = {
buttons: {
[locale]: []
},
history: {
[locale]: [text]
},
};
chrome.storage.sync.set({[skillId]: obj})
}
});
}
function draggableSideBar() {
$('.askt-input').append('<div id="dragbar"></div>');
let dragging = false;
$('#dragbar').on('mousedown', function(e) {
e.preventDefault();
dragging = true;
$(document).on('mousemove', function(e){
const percentage = (e.pageX / window.innerWidth) * 100;
const mainPercentage = 100 - percentage;
$('.askt-input').css("width", percentage + "%");
$('.askt-output').css("width", mainPercentage + "%");
});
});
$(document).on('mouseup', function(e){
if (dragging) {
$(document).unbind('mousemove');
dragging = false;
}
});
}
}