-
Notifications
You must be signed in to change notification settings - Fork 5
/
bootstrap.js
204 lines (171 loc) · 5.5 KB
/
bootstrap.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
const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
Cu.import("resource://gre/modules/AddonManager.jsm");
Cu.import("resource://gre/modules/Services.jsm");
var button, menu, exportMenu, importMenu;
const BACKUP_FOLDER = '/sdcard/tabs_backup/';
function loadIntoWindow(window) {
var parentId = window.NativeWindow.menu.toolsMenuID
exportMenu = window.NativeWindow.menu.add({
name:"Export Tabs",
callback:function(){
exportTabs(window);
},
parent:parentId
});
importMenu = window.NativeWindow.menu.add({
name:"Import Tabs",
callback:function(){
importTabs(window);
},
parent:parentId
});
}
function unloadFromWindow(window) {
if (!window) return;
window.NativeWindow.menu.remove(exportMenu);
window.NativeWindow.menu.remove(importMenu);
}
// --------------------------- main ---------------------------
function exportTabs(window){
var res = [];
window.BrowserApp.tabs.forEach(function(tab){
var addr = tab.window.location;
var title = tab.window.document.title;
if (!title) title = addr;
if(validUrl(addr) || !tab.browser.__SS_data) {
res.push(title + "\n" + addr);
}
else {
var entries = tab.browser.__SS_data.entries;
var last = entries.length - 1;
var zombieAddr = entries[last].url;
var zombieTitle = entries[last].title;
if(!zombieTitle) zombieTitle = zombieAddr;
if(validUrl(zombieAddr)) {
res.push(zombieTitle + "\n" + zombieAddr);
}
}
});
res = res.join("\n\n");
var path = BACKUP_FOLDER;
var token = getUniqueToken();
path += "backup_" + token + ".txt";
writeFile(path, res);
window.NativeWindow.toast.show("Tabs exported to: " + path, "long");
}
function importTabs(window){
let fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
fp.init(window, "Import Tabs", Ci.nsIFilePicker.modeOpen);
try{
fp.show();
var selectedFile = fp.file;
if(!selectedFile) return;
var path = Services.io.newFileURI(selectedFile).spec;
}
catch(e){
var path = "file://" + BACKUP_FOLDER + "import.txt";
}
var res = [];
fetchData(window, path, function(data) {
if (/\n\n/.test(data)) {
var urls = data.split(/\n\n/);
urls = urls.map(function(url) {
return url.split(/\n/)[1];
})
}
else if(/\r\n/.test(data)) {
var urls = data.split(/\r\n/);
}
else {
var urls = data.split(/\n/);
}
for (var i in urls) {
var url = urls[i].trim();
res.push(url);
window.BrowserApp.addTab(url);
}
});
}
// --------------------------- utils ---------------------------
function validUrl(url) {
return url != "about:blank" && url != "about:home";
}
function getUniqueToken() {
var res = '';
var d = new Date();
try {
var date = formatDate(d.getDate());
var month = formatDate(d.getMonth() + 1);
var year = d.getFullYear();
var hours = formatDate(d.getHours());
var minutes = formatDate(d.getMinutes());
var seconds = formatDate(d.getSeconds());
res = date + "-" + month + "-" + year + "_" + hours + "-" + minutes + "-" + seconds;
}
catch(e) {
res = d.getTime();
}
return res;
}
function formatDate(arg) {
return (arg < 10) ? '0'+arg: arg;
}
function writeFile(path, text) {
var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
file.initWithPath(path);
file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE,0644);
var foStream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(Ci.nsIFileOutputStream);
foStream.init(file, 0x02 | 0x08 | 0x20, 0666, 0);
var converter = Cc["@mozilla.org/intl/converter-output-stream;1"].createInstance(Ci.nsIConverterOutputStream);
converter.init(foStream, "UTF-8", 0, 0);
converter.writeString(text);
converter.close();
}
function fetchData(window, path, onFinish) {
let xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
try {
xhr.open("GET", path, true);
}
catch (e) {
window.NativeWindow.toast.show("File '" + path + "' not found", "long");
}
xhr.onload = function onload(event) {
if (xhr.status == 200 || xhr.status===0) {
onFinish(xhr.responseText);
}
}
xhr.onerror = function(event) {
var path = path.replace("file://", "");
window.NativeWindow.toast.show("File '" + path + "' not found","long");
}
xhr.send(null);
}
// --------------------------- addon manage ---------------------------
function startup(data, reason) {
let windows = Services.wm.getEnumerator("navigator:browser");
while (windows.hasMoreElements()) {
let domWindow = windows.getNext().QueryInterface(Ci.nsIDOMWindow);
loadIntoWindow(domWindow);
}
Services.wm.addListener(windowListener);
}
function shutdown(data, reason) {
if (reason == APP_SHUTDOWN) return;
Services.wm.removeListener(windowListener);
let windows = Services.wm.getEnumerator("navigator:browser");
while (windows.hasMoreElements()) {
let domWindow = windows.getNext().QueryInterface(Ci.nsIDOMWindow);
unloadFromWindow(domWindow);
}
}
var windowListener = {
onOpenWindow: function(aWindow) {
let domWindow = aWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
domWindow.addEventListener("UIReady", function onLoad() {
domWindow.removeEventListener("UIReady", onLoad, false);
loadIntoWindow(domWindow);
}, false);
},
onCloseWindow: function(aWindow) {},
onWindowTitleChange: function(aWindow, aTitle) {}
};