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
|
'use strict';
var file = require("sdk/io/file");
var url = require("sdk/url");
var convert2RegExp = require("./scriptish/convert-2-regexp").convert2RegExp;
var userscriptParser = require("./scriptish/userscript-header-parser").parse;
var manager = require("./scriptish/userscript-manager");
const JSVersions = ['1.6', '1.7', '1.8'/*, '1.8.1'*/];
var UserScript = exports.UserScript = function UserScript(aURL) {
var script = new Script(aURL);
manager.register(script);
return {
destory: function() {
manager.unregister(script);
},
get enabled() script.enabled,
set enabled(aVal) script.enabled = !!aVal
};
}
function Script(aURL) {
this._url = url.URL(aURL);
this._filepath = url.toFilename(aURL);
this._source = file.read(this._filepath);
var header = userscriptParser(this._source);
this._name = (header.name && header.name[0]) || Script.parseScriptName(aURL);
this._namespace = (header.namespace && header.namespace[0]) || this.url.host;
this._description = (header.description && header.description[0]) || "";
this.enabled = true;
this._includes = (header.include || []).map(convert2RegExp);
this._excludes = (header.exclude || []).map(convert2RegExp);
this._requires = (header.require || []);
this._resources = (header.resource || []);
if (header.jsversion) {
for (var i = header.jsversion.length - 1; ~i; i--) {
let val = header.jsversion[i];
if (~JSVersions.indexOf(val)) {
this.jsversion = val;
break;
}
}
}
}
Script.prototype = {
get prefPrefix () {
return ["greasemonkey.scriptvals.",
this._namespace,
"/",
this._name,
"."].join("");
},
// TODO: actually implement this!
matchesDomain: function() {
return true;
},
matchesURL: function(url) {
var test = function(pattern) {
return pattern.test(url);
}
return this.enabled
&& this._includes.some(test)
&& !this._excludes.some(test);
},
_changed: function(event, data) {
if(this._config) {
this._config._changed(this, event, data);
}
},
get name() { return this._name; },
get namespace() { return this._namespace; },
get description() { return this._description; },
get enabled() { return this._enabled; },
set enabled(enabled) {
this._enabled = enabled;
this._changed("edit-enabled", enabled);
},
get includes() { return this._includes.concat(); },
get excludes() { return this._excludes.concat(); },
addInclude: function(url) {
this._includes.push(url);
this._changed("edit-include-add", url);
},
removeIncludeAt: function(index) {
this._includes.splice(index, 1);
this._changed("edit-include-remove", index);
},
addExclude: function(url) {
this._excludes.push(url);
this._changed("edit-exclude-add", url);
},
removeExcludeAt: function(index) {
this._excludes.splice(index, 1);
this._changed("edit-exclude-remove", index);
},
get requires() { return this._requires.concat(); },
get resources() { return this._resources.concat(); },
get unwrap() { return this._unwrap; }
};
Script.parseScriptName = function(aURL) ((
/\/([^\/]+)\.user(?:-\d+)?\.js(?:[\?#].*)?$/.test(aURL || "")) ? RegExp.$1 : "");
|