-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpebble-actions.js
530 lines (459 loc) · 15.7 KB
/
pebble-actions.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/*
This file loads any interactions provided by the user and manages their local controls when they've chosen them
*/
console.log("Extension loaded.");
var uniqueId;
var currentPlugin;
//tells whether or not the extension is activated (the controls drop down)
var dropDownShown = false;
var pickMode = false;
var currentlyPicking = null;
var pickingText = false;
var appDraft = {
map: {
site: null,
buttons: {
up: {
cssPath: null,
action: null
},
down: {
cssPath: null,
action: null
},
select: {
cssPath: null,
action: null
}
}
},
text: {
header: {
cssPath: null,
content: null
},
main: {
cssPath: null,
content: null
}
}
};
var getSite = function() {
var a = domainatrix.parse(window.location.host);
return a.domain + '.' + a.publicSuffix;
};
var previousElement = null;
//controls harnessing key events
//example usage: Podium.keyEvent(32, "keydown");
Podium = {};
Podium.keyEvent = function(key, pressType) {
var oEvent = document.createEvent('KeyboardEvent');
// Chromium Hack
Object.defineProperty(oEvent, 'keyCode', {
get : function() {
return this.keyCodeVal;
}
});
Object.defineProperty(oEvent, 'which', {
get : function() {
return this.keyCodeVal;
}
});
if (oEvent.initKeyboardEvent) {
oEvent.initKeyboardEvent(pressType, true, true, document.defaultView, false, false, false, false, key, key);
} else {
oEvent.initKeyEvent(pressType, true, true, document.defaultView, false, false, false, false, key, 0);
}
oEvent.keyCodeVal = key;
if (oEvent.keyCode !== key) {
alert("keyCode mismatch " + oEvent.keyCode + "(" + oEvent.which + ")");
console.log("Key code mismatch.");
}
//$('*').focus();
//^^^^ *NEED TO FIND WHICH ELEMENT TO FOCUS WHEN DOING KEY CONTROLS?!
//$('*').preventDefault();
document.dispatchEvent(oEvent);
}
$(document).on('click', '.ctrl-popup .ctrl-close', function(e){
$('.ctrl-popup').hide();
$('.ctrl-popup-bg').hide();
$('.inside-after').remove();
pickMode = false;
});
$(document).on('click', '.ctrl-popup .ctrl-submit', function(e){
var par = $(e.target).parent();
submitApp();
$('.ctrl-popup-bg').hide();
$('.inside-after').remove();
par.remove();
pickMode = false;
$('.ctrl-popup').hide();
});
//hover over elements when the extension is live (for the clicks)
$(document).on('mouseover', function(event) {
if(!pickMode){
return;
}
if ($(event.target).children().length > 0){
return;
}
if(!$(event.target).hasClass("inside-after")) {
$('.outlineElement').removeClass('outlineElement');
var cursorType = $(event.target).css('cursor');
if (cursorType == 'pointer' || cursorType == 'auto') {
var el = parentPointer(event.target);
$(el).addClass('outlineElement');
$('.inside-after').remove();
var newDiv = $('<div class="inside-after"></div>');
$(el).append(newDiv);
}
else {
$('.inside-after').remove();
}
} else {
$('.inside-after').click(function(event) {
event.preventDefault();
console.log($(event.target).parent().getPath());
if (pickingText) {
appDraft.text[currentlyPicking]['cssPath'] = $(event.target).parent().getPath();
appDraft.text[currentlyPicking]['content'] = $(appDraft.text[currentlyPicking]['cssPath']).text();
} else {
appDraft.map.buttons[currentlyPicking].action = 'click';
appDraft.map.buttons[currentlyPicking].cssPath = $(event.target).parent().getPath();
}
pickMode = false;
$('.ctrl-popup').removeClass('pick-mode');
showPopup();
$('.inside-after').remove();
});
}
});
var loadControlBox = function() {
showPopup();
}
var initPopup = function(){
var time = new Date();
var timeString = '';
var min = time.getMinutes();
if (min < 10) {
min = '0' + min;
}
if (time.getHours() > 12) {
timeString += (time.getHours() - 12) + ':' + min + ' PM';
}
else {
timeString += time.getHours() + ':' + min + ' AM';
}
console.log('initPopup');
var newHTML = '';
newHTML += '<div class="ctrl">';
newHTML += '<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">';
newHTML += '<div class="popup-bg"></div>';
newHTML += '<div class="ctrl-popup">';
newHTML += '<div class="ctrl-preview-button-top"></div>';
newHTML += '<div class="ctrl-preview-button-main"></div>';
newHTML += '<div class="ctrl-preview-button-bottom"></div>';
newHTML += '<div class="ctrl-preview-button-left"></div>';
newHTML += '<div class="ctrl-preview">';
newHTML += '<div class="ctrl-preview-header">';
newHTML += '<p class="ctrl-preview-header-time">' + timeString + '</p>';
newHTML += '</div>';
newHTML += '<div class="ctrl-preview-actionbar">';
newHTML += '<p>•</p>';
newHTML += '<p>▸</p>';
newHTML += '<p>◼</p>';
newHTML += '</div>';
newHTML += '<p class="ctrl-preview-top selectable" data-text="header">Click to set</p>';
newHTML += '<p class="ctrl-preview-main selectable" data-text="main">Click to set text text text text blah</p>';
newHTML += '<p class="ctrl-preview-bottom"></p>';
newHTML += '</div>';
newHTML += '<div class="ctrl-submit disabled"><i class="fa fa-2x fa-check"></i></div>';
newHTML += '<div class="ctrl-close"><i class="fa fa-2x fa-close"></i></div>';
newHTML += '<h1>Name your Controller</h1>';
newHTML += '<input class="ctrl-input">';
newHTML += '<div class="pebble-button-list">';
newHTML += '<p class="up"><button data-button="up" class="ctrl-pebble-button-label">click to configure <span class="accent">"up"</span> </button></p>';
newHTML += '<p class="select"><button data-button="select" class="ctrl-pebble-button-label">click to configure <span class="accent">"select"</span> </button></p>';
newHTML += '<p class="down"><button data-button="down" class="ctrl-pebble-button-label">click to configure <span class="accent">"down"</span> </button></p>';
newHTML += '</div>';
newHTML += '</div>';
newHTML += '</div>';
var el = $(newHTML);
$('body').append(el);
$('.ctrl-popup').draggable();
$('.ctrl-popup-bg').hide();
$('.ctrl-popup').hide();
$('.ctrl-input').keyup(function(event) {
$('.ctrl-preview-bottom').html($(this).val());
});
};
var showPopup = function(){
console.log('showPopup');
var currentlyPicking = null;
var saveReady = false;
appDraft.map.site = getSite();
if(appDraft.text.header.content !== null) {
$('.ctrl-preview-top').html(appDraft.text.header.content);
}
if(appDraft.text.main.content !== null) {
$('.ctrl-preview-main').html(appDraft.text.main.content);
}
if (appDraft.map.buttons.up.cssPath !== null){
var eName = $(appDraft.map.buttons.up.cssPath)[0].innerText;
if (isEmpty(eName) || isBlank(eName) || eName === null) {
eName = $(appDraft.map.buttons.up.cssPath)[0].getAttribute('aria-label');
}
if (isEmpty(eName) || isBlank(eName) || eName === null) {
eName = $(appDraft.map.buttons.up.cssPath)[0].getAttribute('title');
}
if (isEmpty(eName) || isBlank(eName) || eName === null) {
var curr = $(appDraft.map.buttons.up.cssPath);
while(isEmpty(eName) || isBlank(eName) || eName === null){
curr = curr.parent();
if (curr === null){
break;
}
eName = curr.attr('class');
console.log(eName);
}
}
$('.ctrl-popup .up').html('configured to <span class="action">' + eName + '</span>');
saveReady = true;
}
if (appDraft.map.buttons.select.cssPath !== null){
var eName = $(appDraft.map.buttons.select.cssPath)[0].innerText;
if (isEmpty(eName) || isBlank(eName) || eName === null) {
eName = $(appDraft.map.buttons.select.cssPath)[0].getAttribute('aria-label');
}
if (isEmpty(eName) || isBlank(eName) || eName === null) {
eName = $(appDraft.map.buttons.select.cssPath)[0].getAttribute('title');
}
if (isEmpty(eName) || isBlank(eName) || eName === null) {
var curr = $(appDraft.map.buttons.select.cssPath);
while(isEmpty(eName) || isBlank(eName) || eName === null){
curr = curr.parent();
if (curr === null){
break;
}
eName = curr.attr('class');
}
}
$('.ctrl-popup .select').html('configured to <span class="action">' + eName + '</span>');
saveReady = true;
}
if (appDraft.map.buttons.down.cssPath !== null){
var eName = $(appDraft.map.buttons.down.cssPath)[0].innerText;
if (isEmpty(eName) || isBlank(eName) || eName === null) {
eName = $(appDraft.map.buttons.down.cssPath)[0].getAttribute('aria-label');
}
if (isEmpty(eName) || isBlank(eName) || eName === null) {
eName = $(appDraft.map.buttons.down.cssPath)[0].getAttribute('title');
}
if (isEmpty(eName) || isBlank(eName) || eName === null) {
var curr = $(appDraft.map.buttons.down.cssPath);
while(isEmpty(eName) || isBlank(eName) || eName === null){
curr = curr.parent();
if (curr === null){
break;
}
eName = curr.attr('class');
}
}
$('.ctrl-popup .down').html('configured to <span class="action">' + eName + '</span>');
saveReady = true;
}
if (saveReady){
$('.ctrl-popup .ctrl-submit').removeClass('disabled');
}
$('.ctrl-popup').show();
$('.ctrl-popup-bg').show();
}
initPopup();
$(document).on('click', '.pebble-button-list button', function(e){
var button = $(this).data('button');
currentlyPicking = button;
pickingText = false;
pickMode = true;
$('.ctrl-popup-bg').hide();
$('.ctrl-popup').addClass('pick-mode');
});
$(document).on('click', '.ctrl-preview-main, .ctrl-preview-top', function(event) {
currentlyPicking = $(this).data('text');
pickingText = true;
pickMode = true;
$('.ctrl-popup-bg').hide();
$('.ctrl-popup').addClass('pick-mode');
});
// Pebble emulator button animations
$('.pebble-button-list .up').hover(function() {
$('.ctrl-popup .ctrl-preview-button-top').stop().animate({width: '6px'}, 100);
}, function() {
$('.ctrl-popup .ctrl-preview-button-top').stop().animate({width: '12px'}, 100);
});
$('.pebble-button-list .select').hover(function() {
$('.ctrl-popup .ctrl-preview-button-main').stop().animate({width: '6px'}, 100);
}, function() {
$('.ctrl-popup .ctrl-preview-button-main').stop().animate({width: '12px'}, 100);
});
$('.pebble-button-list .down').hover(function() {
$('.ctrl-popup .ctrl-preview-button-bottom').stop().animate({width: '6px'}, 100);
}, function() {
$('.ctrl-popup .ctrl-preview-button-bottom').stop().animate({width: '12px'}, 100);
});
// Helper Methods
function isBlank(str) {
return (!str || /^\s*$/.test(str));
}
function isEmpty(str) {
return (!str || 0 === str.length);
}
jQuery.fn.getPath = function () {
if (this.length != 1) throw 'Requires one element.';
var path, node = this;
while (node.length) {
var realNode = node[0], name = realNode.localName;
if (!name) break;
name = name.toLowerCase();
var parent = node.parent();
var siblings = parent.children(name);
if (siblings.length > 1) {
name += ':eq(' + siblings.index(realNode) + ')';
}
path = name + (path ? '>' + path : '');
node = parent;
}
return path;
};
var parentPointer = function(el) {
if ($(el).parent().css('cursor') == 'pointer') {
return parentPointer($(el).parent());
}
else {
return el;
}
}
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse){
if(request['action'] == 'launchControlBox') {
loadControlBox();
} else if (request['action'] == 'redirectToMarketplace') {
var win = window.open('http://wristctrl.com/marketplace', '_blank');
win.focus();
} else if(Object.keys(request)[0] == 'uniqueId') {
uniqueId = request['uniqueId'];
localStorage.setItem("ctrl-uniqueId", uniqueId);
} else if(Object.keys(request)[0] == 'launchSite') {
var site = 'http://' + request['launchSite'];
var win = window.open(site, '_blank');
win.focus();
}
});
var trim = function(str) {
var strWithoutWhiteSpace = str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
return strWithoutWhiteSpace.substring(0, 63); // max size on pebble
}
var execFireAction = function(cssPath, action){
if (action === 'click'){
$(cssPath).click();
} else if (action === 'raw_js'){
window.eval(cssPath);
}
}
var onCorrectSite = function (target) {
if(target == '*') {
return true;
} else if(target == getSite()) {
return true;
} else {
return false;
}
}
var commandListener = function() {
var first = true;
console.log('here2 ' + currentPlugin);
fb.child('plugins').child(currentPlugin).child('commands').limitToLast(1).on("child_added", function(snapshot) {
if(!first) {
var lastCommand = snapshot.val();
fb.child('plugins').child(currentPlugin).child('map').once('value', function(snapshot2) {
var map = snapshot2.val();
var site = map['site'];
var cssPath = map['buttons'][lastCommand]['cssPath'];
var action = map['buttons'][lastCommand]['action'];
console.log('here');
if (onCorrectSite(site)){
console.log('Execing action: ' + action + ', path: ' + cssPath);
execFireAction(cssPath, action);
}
});
} else {
first = false;
}
});
};
var getCurrentPlugin = function() {
fb.child('plugin-map').once('value', function(snapshot) {
var plugins = snapshot.val();
var keys = Object.keys(plugins);
for(var i=0; i<keys.length; i++) {
if(plugins[keys[i]] == getSite()) {
currentPlugin = keys[i];
commandListener();
}
}
});
};
var submitApp = function() {
var submissionAppName = $('.ctrl-popup .ctrl-input').val();
fb.child('plugins').child(submissionAppName).update(appDraft);
var pluginMap = {};
pluginMap[submissionAppName] = appDraft.map.site;
fb.child('plugin-map').update(pluginMap);
getCurrentPlugin(); // this will launch commandListener
};
var loadContentScript = function() {
Firebase.INTERNAL.forceWebSockets();
fb = new Firebase('https://8tracks-pebble.firebaseio.com/codes/' + uniqueId);
getCurrentPlugin();
};
var getAndSetPebbleText = function() {
var headerCssPath;
var mainCssPath;
var headerText;
var mainText;
var oldHeaderText;
var oldMainText;
var textRef = fb.child('plugins').child(currentPlugin).child('text');
// get the cssPaths
textRef.once('value', function(snapshot) {
headerCssPath = snapshot.child('header').child('cssPath').val();
mainCssPath = snapshot.child('main').child('cssPath').val();
setInterval(function() {
oldHeaderText = headerText;
oldMainText = mainText;
headerText = trim($(headerCssPath).text());
mainText = trim($(mainCssPath).text());
if(oldHeaderText != headerText){
console.log('Header Changed');
textRef.child('header').child('content').set(headerText);
}
if(oldMainText != mainText){
textRef.child('main').child('content').set(mainText);
}
}, 500);
});
};
uniqueId = localStorage.getItem("ctrl-uniqueId");
// wait until we hear from the popup with the uniqueId.
(function wait() {
if(uniqueId != null){
loadContentScript();
} else {
setTimeout(wait, 500);
}
})();
(function wait() {
if(currentPlugin != null){
getAndSetPebbleText();
} else {
setTimeout(wait, 500);
}
})();