-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpairing.js
108 lines (97 loc) · 1.97 KB
/
pairing.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
var get_popup = function()
{
var popup = chrome.extension.getViews({type: 'popup'})[0];
if(popup) return popup;
// vivaldi hax
var windows = chrome.extension.getViews();
return windows
.filter(function(win) { return win.is_popup; })[0];
};
ext.pairing = {
start: function()
{
var popup = get_popup();
ext.comm.send('pair', null, {
success: function(res) {
console.log('success!', popup);
if(popup) popup.switch_tab('pair');
},
error: function(err, code) {
console.error('pair: error: ', err, code);
ext.pairing.show_error(err, code);
}
});
},
finish: function()
{
var popup = get_popup();
ext.comm.send('ping', 'hai', {
success: function(res) {
// finally, complete the action we set out to do
ext.pairing.do_bookmark();
if(popup) popup.switch_tab('load');
},
error: function(err, code) {
console.error('pair: ping: ', err, code);
ext.pairing.show_error(err, code);
}
});
},
do_bookmark: function()
{
ext.bookmarker.scrape({
complete: function(data) {
ext.comm.send('bookmark', data, {
success: function() {
ext.pairing.close();
},
error: function(err, code) {
console.error('error bookmarking: ', err);
ext.pairing.show_error(err, code);
}
});
}
});
},
show_error: function(err, code)
{
var popup = get_popup();
if(code == -1)
{
if(popup) popup.switch_tab('connect_error');
}
else
{
if(popup)
{
popup.set_error(err, code);
popup.switch_tab('error');
}
}
},
close: function()
{
var popup = get_popup();
if(!popup) return false;
popup.close();
},
set_key: function(key_hex)
{
var key = tcrypt.key_to_string(tcrypt.from_hex(key_hex));
localStorage['pairing_key'] = key;
},
get_key: function(options)
{
options || (options = {});
var key = localStorage.pairing_key;
if(options.binary)
{
key = tcrypt.key_to_bin(key);
}
return key;
},
have_key: function()
{
return !!ext.pairing.get_key();
}
};