-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpopup.js
127 lines (102 loc) · 3.46 KB
/
popup.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
window.addEventListener('DOMContentLoaded', function() {
remoteStorage.defineModule('bookmarks', function(privateClient) {
privateClient.use('');
function buildId(string) {
return Sha1.hex_sha1(string);
}
return {
exports: {
deactivateSync: privateClient.deactivateSync,
activateSync: privateClient.activateSync,
// Add or update a bookmark.
//
// Parameters:
// path - path to the bookmark (without the final part), '' for root.
// title - human readable title of the bookmark.
// url - URL to link to. must be unique.
// index - position within "path", to display at in lists.
store: function(parentPath, title, url, index, cb) {
var path = parentPath + buildId(url);
privateClient.storeObject('bookmark', path, {
title: title,
url: url,
index: index
}, cb);
},
createDirectory: function(parentPath, title, index, cb) {
var dirPath = parentPath + buildId(title) + '/';
var path = dirPath + '_meta';
privateClient.storeObject('directory-meta', path, {
title: title,
index: index,
}, function() { cb(dirPath); });
},
syncNow: function() {
privateClient.syncNow('');
}
}
}
});
function pushAll(allDone) {
function pushOne(node, parentPath, cb) {
if(node.url) {
remoteStorage.bookmarks.store(parentPath, node.title, node.url, node.index, cb);
} else {
var i = 0, dirPath;
var pushIter = function() {
if(i < node.children.length) {
pushOne(node.children[i++], dirPath, pushIter);
} else {
allDone();
}
}
remoteStorage.bookmarks.createDirectory(
parentPath, node.title, node.index,
function(p) { dirPath = p; pushIter(); }
);
}
}
chrome.bookmarks.getTree(function(tree) {
var root = tree[0];
remoteStorage.bookmarks.deactivateSync();
for(var i=0;i<root.children.length;i++) {
pushOne(root.children[i], '');
}
remoteStorage.bookmarks.activateSync();
remoteStorage.bookmarks.syncNow();
});
}
function pullAll() {
var note = document.getElementById('not-impl');
note.style.display = 'block';
setTimeout(function() {
note.style.display = 'none';
}, 2500);
}
remoteStorage.claimAccess({'bookmarks':'rw'});
remoteStorage.displayWidget('remotestorage-connect', {
syncShortcut: false,
authDialog: function(url) {
chrome.tabs.create({url: url}, function(tab) {
localStorage.setItem('auth-tab-id', tab.id);
});
}
});
var connected = ( remoteStorage.getWidgetState() == 'connected' ||
remoteStorage.getWidgetState() == 'busy' );
document.getElementById(
connected ? 'connected-info' : 'intro'
).style.display = 'block';
if(connected) {
document.getElementById('push-all').addEventListener('click', function(event) {
event.target.setAttribute('disabled', true);
pushAll(function() {
event.target.setAttribute('disabled', false);
var succ = document.getElementById('succ');
succ.style.display = 'block';
setTimeout(function() { succ.style.display = 'none'; }, 2500);
});
});
document.getElementById('pull-all').addEventListener('click', pullAll);
}
});