/
newscript.js
131 lines (109 loc) · 4.07 KB
/
newscript.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
Components.utils.import("resource://scriptish/constants.js");
Components.utils.import("resource://scriptish/prefmanager.js");
Components.utils.import("resource://scriptish/scriptish.js");
Components.utils.import("resource://scriptish/utils/Scriptish_createUserScriptSource.js");
Components.utils.import("resource://scriptish/utils/Scriptish_localizeDOM.js");
Components.utils.import("resource://scriptish/utils/Scriptish_stringBundle.js");
var $ = function(aID) document.getElementById(aID);
var $$ = function(q) document.querySelector(q);
var $$$ = function(q) document.querySelectorAll(q);
Scriptish_localizeOnLoad(this);
var scriptContent = "";
try {
scriptContent = window.arguments[0]
.QueryInterface(Components.interfaces.nsIDialogParamBlock)
.GetString(0);
} catch(e) {}
window.addEventListener("load", function() {
$("scriptish").addEventListener("dialogaccept", function(evt) {
try {
if (doInstall()) {
return true;
}
} catch (e) {}
evt.preventDefault();
evt.stopProgapation();
return false;
}, false);
// load defaults
$("id").value = Scriptish_prefRoot.getValue("newscript_id", "");
$("namespace").value = Scriptish_prefRoot.getValue("newscript_namespace", "");
$("author").value = Scriptish_prefRoot.getValue("newscript_author", "");
(function considerLocation() {
if (!window.opener.gBrowser) {
return;
}
let contentLocation = window.opener.gBrowser.selectedBrowser.contentWindow.location;
$("includes").value = contentLocation.href;
if (!contentLocation.host) {
return;
}
let host = contentLocation.host;
let generateIdButton = $('generate-id');
generateIdButton.hidden = false;
generateIdButton.addEventListener("command", function generateId() {
let gid = contentLocation.host;
gid += "-" + Services.uuid.generateUUID().toString().slice(1, -1);
gid += "@" + ($("namespace").value || "scriptish").replace(/^@/, "");
$("id").value = gid;
}, false);
})();
}, false);
function doInstall() {
// validate some
var ok = Array.reduce($$$("*[required]"), function(p,v) {
var vok = !!v.value;
v.setAttribute("invalid", !vok);
return p & vok;
}, true);
if (!ok) {
return false;
}
var tools = {};
Components.utils.import("resource://scriptish/utils/Scriptish_openInEditor.js", tools);
Components.utils.import("resource://scriptish/utils/Scriptish_getTempFile.js", tools);
Components.utils.import("resource://scriptish/utils/Scriptish_getWriteStream.js", tools);
var script = createScriptSource();
if (!script) return false;
// put this created script into a file -- only way to install it
var tempFile = tools.Scriptish_getTempFile();
var foStream = tools.Scriptish_getWriteStream(tempFile);
foStream.write(script, script.length);
foStream.close();
Scriptish.getConfig(function(config) {
// create a script object with parsed metadata,
script = config.parse(script);
// make sure entered details will not ruin an existing file
if (config.installIsUpdate(script)) {
var overwrite = confirm(Scriptish_stringBundle("newscript.exists"));
if (!overwrite) return false;
}
// finish making the script object ready to install
script.setDownloadedFile(tempFile);
// install this script
config.install(script);
// and fire up the editor!
tools.Scriptish_openInEditor(script, window);
// persist values
Scriptish_prefRoot.setValue("newscript_namespace", script.namespace);
Scriptish_prefRoot.setValue("newscript_author", script.author);
})
return true;
}
// assemble the XUL fields into a script template
function createScriptSource() {
var header = {
id: $("id").value,
name: $("name").value,
namespace: $("namespace").value,
author: $("author").value,
description: $("description").value,
include: $("includes").value ? $("includes").value.match(/.+/g) : [],
exclude: $("excludes").value ? $("excludes").value.match(/.+/g) : []
}
try {
return Scriptish_createUserScriptSource(header, scriptContent);
} catch (e) {
alert(e.message);
}
}