-
Notifications
You must be signed in to change notification settings - Fork 55
/
FeiRuoBackup.uc.js
296 lines (288 loc) · 11 KB
/
FeiRuoBackup.uc.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
// ==UserScript==
// @name FeiRuoBackup.uc.js
// @description 备份、整合和还原文件。
// @author feiruo
// @License Version: MPL 2.0/GPL 3.0/LGPL 2.1
// @compatibility Firefox 45
// @charset UTF-8
// @include chrome://browser/content/browser.xul
// @id [12FA3E5D]
// @inspect window.FeiRuoBackup
// @startup window.FeiRuoBackup.init();
// @shutdown window.FeiRuoBackup.onDestroy();
// @optionsURL about:config?filter=FeiRuoBackup.
// @homepageURL https://www.feiruo.pw/
// @homepageURL https://github.com/feiruo/userChromeJS
// @downloadURL https://github.com/feiruo/userChromeJS/FeiRuoBackup.uc.js
// @note Begin 2016-07-25
// @note 备份、整合和还原文件
// @note 仅供个人测试、研究,作者不承担因使用此脚本对自己和他人造成任何形式的损失或伤害之任何责任。
// @version 0.0.1 2016.07.25 17:00 Building。
// ==/UserScript==
(function() {
let {
classes: Cc,
interfaces: Ci,
utils: Cu,
results: Cr
} = Components;
if (!window.FileUtils) Cu.import("resource://gre/modules/FileUtils.jsm");
const FileInputStream = Components.Constructor("@mozilla.org/network/file-input-stream;1", "nsIFileInputStream", "init");
const ConverterInputStream = Components.Constructor("@mozilla.org/intl/converter-input-stream;1", "nsIConverterInputStream", "init");
if (window.FeiRuoBackup) {
window.FeiRuoBackup.onDestroy();
delete window.FeiRuoBackup;
}
var FeiRuoBackup = {
Prefs: Services.prefs.getBranch("userChromeJS.FeiRuoBackup."),
BackList: [ //
{
Path: "ProfD", //路径 UChrm ProfD and so on
File: "user.js", //文件名
Type: "line", //读取方式 line-按行读取、json-作为json、sandbox
Backup: false, //是否备份
BackupOn: 0, //备份选项:0-启动和关闭,1-启动,2-关闭
Recover: true, //是否还原
RecoverOn: 0, //还原选项:0-启动和关闭,1-启动,2-关闭
ReadFunc: function(val) { //读取转换函数,仅 line
var matcher = val.match(/(\/\/)?([ ]*)?(user_)?pref\(([^,]*),(.*)\);([ ]*)?(\/\/(.*))?/i);
if (!matcher) return;
return {
Ignore: matcher[1] == "//" ? true : false,
Pref: matcher[3] || "",
Key: matcher[4],
Val: matcher[5],
Notes: matcher[8]
};
},
ToStrFunc: function(FileData, BackData) { //转为字符串函数
var List = FeiRuoBackup.CheckDuplicate((FileData || []).concat(BackData || []), "Key"); //内置去重函数
var str = "";
for (var i in List) {
if (!!List[i].Ignore) continue;
var val = List[i].Val;
str += List[i].Pref + 'pref(' + List[i].Key + ',' + val + ');' + (List[i].Notes ? (" // " + List[i].Notes) : "") + "\n";
}
return str;
}
}, {
Path: "ProfD",
File: "logins.json",
Type: "json",
Backup: true,
BackupOn: 0,
Recover: true,
RecoverOn: 0,
ToStrFunc: function(FileData, BackData) {
FileData = JSON.parse(FileData || '{"nextId": 1,"logins": [],"disabledHosts": [],"version": 1}');
BackData = JSON.parse(BackData || '{"nextId": 1,"logins": [],"disabledHosts": [],"version": 1}');
var NextId = FileData.nextId > BackData.nextId ? FileData.nextId : (BackData.nextId || FileData.nextId);
var Logins = FileData.logins || BackData.logins || [];
FileData.logins && (Logins = FeiRuoBackup.CheckDuplicate(FileData.logins.concat(BackData.logins)));
var DisabledHosts = FileData.disabledHosts || BackData.disabledHosts || [];
FileData.disabledHosts && (DisabledHosts = FeiRuoBackup.CheckDuplicate(FileData.disabledHosts.concat(BackData.disabledHosts)));
return JSON.stringify({
nextId: NextId,
logins: Logins,
disabledHosts: DisabledHosts,
version: FileData.version || 1
});
}
}
],
init: function() {
var StartupTime = new Date();
this.Debug = this.GetPrefs(0, "Debug", false);
var ins = $("devToolsSeparator");
ins.parentNode.insertBefore($C("menuitem", {
id: "FeiRuoBackup_set",
label: "备份还原文件",
type: "checkbox",
autoCheck: "false",
oncommand: "FeiRuoBackup.Toggle(event);"
}), ins);
this.LoadSetting();
this.Prefs.addObserver('', this.PrefsObs, false);
window.addEventListener("unload", function() {
FeiRuoBackup.onDestroy(FeiRuoBackup.Enable);
}, false);
},
onDestroy: function(isAlert) {
if (isAlert) this.Combination(isAlert);
if ($("FeiRuoBackup_set")) $("FeiRuoBackup_set").parentNode.removeChild($("FeiRuoBackup_set"));
Services.appinfo.invalidateCachesOnRestart();
Services.obs.notifyObservers(null, "startupcache-invalidate", "");
},
PrefsObs: function(subject, topic, data) {
if (topic == 'nsPref:changed') {
switch (data) {
case 'Debug':
case 'Enable':
FeiRuoBackup.LoadSetting(data);
break;
}
}
},
LoadSetting: function(type) {
if (!type || type === "Debug") this.Debug = this.GetPrefs(0, "Debug", false);
if (!type || type === "Enable") {
this.Enable = this.GetPrefs(0, "Enable", true);
this.Enable && this.Combination();
$("FeiRuoBackup_set") && $("FeiRuoBackup_set").setAttribute('checked', this.Enable);
}
},
Toggle: function(event) {
this.Enable = !this.Enable;
this.Prefs.setBoolPref("Enable", this.Enable);
},
/*****************************************************************************************/
Combination: function(isAlert) {
var OrgFile, BackFile, FileData, BackData, NewData;
for (var i in this.BackList) {
var Rule = this.BackList[i];
OrgFile = FileUtils.getFile(Rule.Path, [Rule.File]);
BackFile = FileUtils.getFile("UChrm", ["Backups", Rule.File]);
FileData = this.LoadFile(OrgFile, Rule.Type, Rule.ReadFunc);
BackData = this.LoadFile(BackFile, Rule.Type, Rule.ReadFunc);
if (FileData != BackData) {
if (Rule.Backup) {
NewData = Rule.ToStrFunc(FileData, BackData);
if (Rule.BackupOn == 0 || (Rule.BackupOn == 1 && !isAlert) || (Rule.BackupOn == 2 && isAlert))
this.StrToFile(BackFile, NewData);
} else
NewData = Rule.ToStrFunc(BackData);
if (Rule.Recover && (BackData && BackData != "")) {
if (Rule.RecoverOn == 0 || (Rule.RecoverOn == 1 && !isAlert) || (Rule.RecoverOn == 2 && isAlert))
this.StrToFile(OrgFile, NewData);
}
}
}
},
CheckDuplicate: function(data, key) {
if (!data) return;
var obj = {};
var New = [];
key = key || "id";
data.forEach(function(i) {
switch (typeof i) {
case 'object':
if (!obj[i[key]]) {
obj[i[key]] = true;
New.push(i);
}
break;
case 'string':
if (!obj[i]) {
obj[i] = true;
New.push(i);
}
break;
case 'undefined':
break;
default:
break;
}
});
return New;
},
/*****************************************************************************************/
LoadFile: function(aFile, type, func) {
if (!aFile || !aFile.exists() || !aFile.isFile()) return log("File does not exist.");
var fileStream = new FileInputStream(aFile, 0x01, 0444, 0);
var stream = new ConverterInputStream(fileStream, "UTF-8", 16384, Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER);
var data = "";
if (!type || type == "json" || type == "sandbox") {
var str = {};
while (stream.readString(0xffffffff, str) != 0) {
data += str.value;
}
try {
data = decodeURIComponent(escape(data));
} catch (e) {}
}
if (type == "line") {
stream = stream.QueryInterface(Ci.nsIUnicharLineInputStream);
var line = {};
var val = "";
var cont;
data = [];
do {
cont = stream.readLine(line);
val = line.value;
val = val.trim();
val = func(val);
!!val && data.push(val);
} while (cont);
}
stream.close();
if (type == "sandbox") {
var sandbox = new Cu.Sandbox(new XPCNativeWrapper(window));
sandbox.Components = Components;
sandbox.Cc = Cc;
sandbox.Ci = Ci;
sandbox.Cr = Cr;
sandbox.Cu = Cu;
sandbox.Services = Services;
sandbox.locale = Services.prefs.getCharPref("general.useragent.locale");
try {
var lineFinder = new Error();
Cu.evalInSandbox(data, sandbox, "1.8");
} catch (e) {
let line = e.lineNumber - lineFinder.lineNumber - 1;
var errmsg = 'Error: ' + e + "\n请重新检查【" + aFile.leafName + "】文件第 " + line + " 行";
log(errmsg);
}
data = sandbox || null;
}
return data;
},
StrToFile: function(file, data) {
var suConverter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Ci.nsIScriptableUnicodeConverter);
suConverter.charset = 'UTF-8';
data = suConverter.ConvertFromUnicode(data);
var foStream = Cc['@mozilla.org/network/file-output-stream;1'].createInstance(Ci.nsIFileOutputStream);
foStream.init(file, 0x02 | 0x08 | 0x20, 0664, 0);
foStream.write(data, data.length);
foStream.close();
},
GetPrefs: function(type, name, val) {
switch (type) { //https://developer.mozilla.org/en-US/Add-ons/Code_snippets/Preferences
case 0:
if (!this.Prefs.prefHasUserValue(name) || this.Prefs.getPrefType(name) != Ci.nsIPrefBranch.PREF_BOOL) this.Prefs.setBoolPref(name, val ? val : false);
return this.Prefs.getBoolPref(name);
case 1:
if (!this.Prefs.prefHasUserValue(name) || this.Prefs.getPrefType(name) != Ci.nsIPrefBranch.PREF_INT) this.Prefs.setIntPref(name, val ? val : 0);
return this.Prefs.getIntPref(name);
case 2:
if (!this.Prefs.prefHasUserValue(name) || this.Prefs.getPrefType(name) != Ci.nsIPrefBranch.PREF_STRING) this.Prefs.setCharPref(name, val ? val : "");
return this.Prefs.getCharPref(name);
case 3:
if (!this.Prefs.prefHasUserValue(name) || this.Prefs.getPrefType(name) != Ci.nsIPrefBranch.PREF_STRING) this.Prefs.setComplexValue(name, Ci.nsILocalFile, makeURI(val).QueryInterface(Ci.nsIFileURL).file);
return this.Prefs.getComplexValue(name, Ci.nsILocalFile);
case 4:
if (!this.Prefs.prefHasUserValue(name) || this.Prefs.getPrefType(name) != Ci.nsIPrefBranch.PREF_STRING) {
var aFile = Cc["@mozilla.org/pref-relativefile;1"].createInstance(Ci.nsIRelativeFilePref);
aFile.relativeToKey = "UChrm";
var path = Services.io.newFileURI(FileUtils.getDir("UChrm", '')).spec + val.replace(/^(\/\/|\\)/i, '').replace(/\\/ig, '/');
aFile.file = makeURI(path).QueryInterface(Ci.nsIFileURL).file;
this.Prefs.setComplexValue(name, Ci.nsIRelativeFilePref, aFile || this[name]);
}
return this.Prefs.getComplexValue(name, Ci.nsIRelativeFilePref).file;
default:
break;
}
}
};
/*****************************************************************************************/
function log(str) {
if (FeiRuoBackup.Debug) console.log("[FeiRuoBackup Debug] " + str);
}
function $(id) document.getElementById(id);
function $C(name, attr) {
var el = document.createElement(name);
if (attr) Object.keys(attr).forEach(function(n) el.setAttribute(n, attr[n]));
return el;
}
window.FeiRuoBackup = FeiRuoBackup;
FeiRuoBackup.init();
})();