-
Notifications
You must be signed in to change notification settings - Fork 8
/
pairing.js
106 lines (96 loc) · 1.94 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
const get_popup = function() {
return chrome.extension.getViews({type: 'tab', tabId: ext.pairtab.id})[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(pubkey)
{
var popup = get_popup();
ext.comm.send('ping', 'hai', {
pubkey: pubkey,
success: function(res) {
ext.pairing.set_key(pubkey);
ext.pairing.close(function() {
// finally, complete the action we set out to do
ext.pairing.do_bookmark();
});
},
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() {
},
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(fn)
{
if(!ext.pairtab) return false;
setTimeout(function() {
chrome.tabs.remove(ext.pairtab.id, function() {
fn();
});
}, 100);
},
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();
}
};