Skip to content
Browse files

Parse and use @match metadata.

  • Loading branch information...
1 parent d313e09 commit 23241da190022e06923a9d381c436da6a05ea0dc @arantius arantius committed Jul 22, 2011
Showing with 54 additions and 12 deletions.
  1. +13 −1 content/config.js
  2. +5 −0 content/install.js
  3. +8 −0 content/install.xul
  4. +26 −11 content/script.js
  5. +1 −0 content/utils.js
  6. +1 −0 locale/en-US/greasemonkey.dtd
View
14 content/config.js
@@ -1,3 +1,5 @@
+Components.utils.import("resource://greasemonkey/third-party/MatchPattern.js");
+
function Config() {
this._saveTimer = null;
this._scripts = null;
@@ -176,6 +178,14 @@ Config.prototype.parse = function(source, uri, updateScript) {
case "exclude":
script._excludes.push(value);
break;
+ case "match":
+ try {
+ var match = new MatchPattern(value);
+ script._matches.push(match);
+ } catch (e) {
+ GM_logError("Ignoring @match pattern " + value + " because:\n" + e);
+ }
+ break;
case "icon":
script._rawMeta += header + '\0' + value + '\0';
try {
@@ -251,7 +261,9 @@ Config.prototype.parse = function(source, uri, updateScript) {
if (!script._namespace && uri) script._namespace = uri.host;
if (!script._description) script._description = "";
if (!script._version) script._version = "";
- if (script._includes.length == 0) script._includes.push("*");
+ if (script._includes.length == 0 && script._matches.length == 0) {
+ script._includes.push("*");
+ }
return script;
};
View
5 content/install.js
@@ -7,6 +7,11 @@ var GMInstall = {
this.setupIncludes("includes", "includes-desc", this.script_.includes);
this.setupIncludes("excludes", "excludes-desc", this.script_.excludes);
+ var matches = [];
+ for (var i = 0, match = null; match = this.script_.matches[i]; i++) {
+ matches.push(match.pattern);
+ }
+ this.setupIncludes("matches", "matches-desc", matches);
this.dialog_ = document.documentElement;
this.extraButton_ = this.dialog_.getButton("extra1");
View
8 content/install.xul
@@ -52,6 +52,14 @@
<description id="includes-desc"
style="font-style:italic" />
</vbox>
+ <vbox id="matches"
+ style="margin-bottom:1em; display:none"
+ >
+ <label
+ value="&install.matches;" />
+ <description id="matches-desc"
+ style="font-style:italic" />
+ </vbox>
<vbox id="excludes"
style="display:none"
>
View
37 content/script.js
@@ -1,4 +1,5 @@
Components.utils.import("resource://greasemonkey/third-party/convert2RegExp.js");
+Components.utils.import("resource://greasemonkey/third-party/MatchPattern.js");
function Script(configNode) {
this._observers = [];
@@ -21,6 +22,7 @@ function Script(configNode) {
this.needsUninstall = false;
this._includes = [];
this._excludes = [];
+ this._matches = [];
this._requires = [];
this._resources = [];
this._unwrap = false;
@@ -32,7 +34,7 @@ function Script(configNode) {
}
Script.prototype.matchesURL = function(url) {
- function test(glob) {
+ function testClude(glob) {
// Do not run in about:blank unless _specifically_ requested. See #1298
if (-1 !== url.indexOf('about:blank')
&& -1 == glob.indexOf('about:blank')
@@ -42,10 +44,13 @@ Script.prototype.matchesURL = function(url) {
return GM_convert2RegExp(glob).test(url);
}
+ function testMatch(matchPattern) {
+ return matchPattern.doMatch(url);
+ }
return GM_isGreasemonkeyable(url)
- && this._includes.some(test)
- && !this._excludes.some(test);
+ && (this._includes.some(testClude) || this._matches.some(testMatch))
+ && !this._excludes.some(testClude);
};
Script.prototype._changed = function(event, data) {
@@ -97,6 +102,9 @@ function Script_getIncludes() { return this._includes.concat(); });
Script.prototype.__defineGetter__('excludes',
function Script_getExcludes() { return this._excludes.concat(); });
+Script.prototype.__defineGetter__('matches',
+function Script_getMatches() { return this._matches.concat(); });
+
Script.prototype.addInclude = function(url) {
this._includes.push(url);
this._changed("edit-include-add", url);
@@ -218,6 +226,9 @@ Script.prototype._loadFromConfigNode = function(node) {
case "Exclude":
this._excludes.push(childNode.textContent);
break;
+ case "Match":
+ this._matches.push(new MatchPattern(childNode.textContent));
+ break;
case "Require":
var scriptRequire = new ScriptRequire(this);
scriptRequire._filename = childNode.getAttribute("filename");
@@ -247,18 +258,21 @@ Script.prototype._loadFromConfigNode = function(node) {
Script.prototype.toConfigNode = function(doc) {
var scriptNode = doc.createElement("Script");
- for (var j = 0; j < this._includes.length; j++) {
- var includeNode = doc.createElement("Include");
- includeNode.appendChild(doc.createTextNode(this._includes[j]));
+ function addNode(name, content) {
+ var node = doc.createElement(name);
+ node.appendChild(doc.createTextNode(content));
scriptNode.appendChild(doc.createTextNode("\n\t\t"));
- scriptNode.appendChild(includeNode);
+ scriptNode.appendChild(node);
}
+ for (var j = 0; j < this._includes.length; j++) {
+ addNode('Include', this._includes[j]);
+ }
for (var j = 0; j < this._excludes.length; j++) {
- var excludeNode = doc.createElement("Exclude");
- excludeNode.appendChild(doc.createTextNode(this._excludes[j]));
- scriptNode.appendChild(doc.createTextNode("\n\t\t"));
- scriptNode.appendChild(excludeNode);
+ addNode('Exclude', this._excludes[j]);
+ }
+ for (var j = 0; j < this._matches.length; j++) {
+ addNode('Match', this._matches[j].pattern);
}
for (var j = 0; j < this._requires.length; j++) {
@@ -390,6 +404,7 @@ Script.prototype.updateFromNewScript = function(newScript, safeWin, chromeWin) {
// Copy new values.
this._includes = newScript._includes;
this._excludes = newScript._excludes;
+ this._matches = newScript._matches;
this._description = newScript._description;
this._unwrap = newScript._unwrap;
this._version = newScript._version;
View
1 content/utils.js
@@ -57,6 +57,7 @@ function GM_logError(e, opt_warn, fileName, lineNumber) {
var flags = opt_warn ? 1 : 0;
+ if ("string" == typeof e) e = new Error(e);
// third parameter "sourceLine" is supposed to be the line, of the source,
// on which the error happened. we don't know it. (directly...)
consoleError.init(e.message, fileName, null, lineNumber,
View
1 locale/en-US/greasemonkey.dtd
@@ -12,6 +12,7 @@
<!ENTITY statusbar.enabled.accesskey "E">
<!ENTITY install.title "Greasemonkey Installation">
<!ENTITY install.runson "runs on:">
+<!ENTITY install.matches "matches:">
<!ENTITY install.butnoton "does not run on:">
<!ENTITY install.warning1 "Malicious scripts can violate your privacy and act on your behalf without your knowledge.">
<!ENTITY install.warning2 "You should only install scripts from sources that you trust.">

0 comments on commit 23241da

Please sign in to comment.
Something went wrong with that request. Please try again.