-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpentadactyl.penta
88 lines (77 loc) · 3.42 KB
/
pentadactyl.penta
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
js <<EOF
/**
* 命令 reinstall
* 脚本功能:
* 1、自动下载 pentadactyl.xpi 最新版
* 2、更改RC文件读取位置,放到profile文件夹里,默认未开启(By 心清无痕)。
* 3、如果下面的 addonsJSM_Path 路径存在,则替换修改后的 addons.jsm
*/
var addonsJSM_Path = "~/pentadactyl/pentadactyl.xpi/addons.jsm";
var changeXPI_RC = false; // (非必要)pentadactyl 默认支持按顺序读取 HOME、用户配置
var mDactyl = {
reInstall_dactyl: function() {
var self = this;
var aURLToDownload = "http://5digits.org/nightly/pentadactyl-latest.xpi";
// var aURLToDownload = "https://addons.mozilla.org/firefox/downloads/file/249096/pentadactyl-1.1-fx.xpi";
self.download(aURLToDownload, function(file) {
if(!file.exists()){
dactyl.echoerr("下载的 pentadactyl.xpi 不存在");
return;
}
self.replaceAddonsFile(file);
if(changeXPI_RC){
self.changeRc(file);
}
dactyl.execute(":extadd " + file.path);
});
},
download: function(aURLToDownload, callback) {
var targetFile, obj_URI, persist;
targetFile = Services.dirsvc.get("TmpD", Ci.nsIFile);
targetFile.append("pentadactyl.xpi");
obj_URI = Services.io.newURI(aURLToDownload, null, null);
persist = services.Persist();
persist.persistFlags = persist.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
persist.progressListener = {
onProgressChange: function(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress) {
},
onStateChange: function(progress, request, flags, status) {
if ((flags & Ci.nsIWebProgressListener.STATE_STOP) && status == 0) {
callback(targetFile);
}
}
};
persist.saveURI(obj_URI, null, null, null, "", targetFile, null);
},
replaceAddonsFile: function(xpiFile) {
// 如果存在则替换 addons.jsm
var file = File(addonsJSM_Path).file;
if(!file.exists()){
return;
}
var zipW = services.ZipWriter(xpiFile, File.MODE_RDWR);
zipW.removeEntry("chrome/common/modules/addons.jsm", false);
zipW.addEntryFile("chrome/common/modules/addons.jsm", Ci.nsIZipWriter.COMPRESSION_DEFAULT,
file, false);
zipW.close();
},
changeRc: function(xpiFile) { //更改RC文件读取位置
const ZIP_DACTYL_PATH = "chrome/common/content/dactyl.js";
// 读取
var zipR = services.ZipReader(xpiFile);
var tmpCont = File.readStream(zipR.getInputStream(ZIP_DACTYL_PATH));
tmpCont = tmpCont.replace(/io\.getRCFile\("~"\)/g, 'io.getRCFile(services.directory.get("ProfD", Ci.nsIFile).path)');
zipR.close();
// 转化
var stream = services.CharsetConv("UTF-8")
.convertToInputStream(tmpCont);
// 写入
var zipW = services.ZipWriter(xpiFile, File.MODE_RDWR);
zipW.removeEntry(ZIP_DACTYL_PATH, false);
zipW.addEntryStream(ZIP_DACTYL_PATH, Date.now(), Ci.nsIZipWriter.COMPRESSION_DEFAULT,
stream, false);
zipW.close();
}
};
EOF
command! reinstall -description "install the lasted pentadactyl and change rc path add modified addon.jsm" -js mDactyl.reInstall_dactyl();