Permalink
Browse files

Add MatchPattern module.

  • Loading branch information...
1 parent 9d401c1 commit a2bfded35e393703000168a3ea8ca470a6f10a2f @arantius arantius committed Jul 22, 2011
Showing with 140 additions and 3 deletions.
  1. +12 −3 CREDITS
  2. +4 −0 locale/en-US/greasemonkey.properties
  3. +124 −0 modules/third-party/MatchPattern.js
View
@@ -1,15 +1,24 @@
Greasemonkey is Open Source, released under the terms of the MIT license;
see the LICENSE.mit file.
+Greasemonkey contains code derived from AdBlock ( http://adblock.mozdev.org/ ),
+reused under the terms of the MPL license; see LICENSE.mpl.
+
Greasemonkey contains code derived from FireBug ( http://getfirebug.com/ ),
reused under the terms of the BSD license; see LICENSE.bsd.
Greasemonkey contains code derived from SlipperyMonkey, originally by
Dave Townsend, reused under the terms of the MIT license.
( http://www.oxymoronical.com/blog/2010/07/How-to-extend-the-new-Add-ons-Manager )
+Greasemonkey contains code derived from Scriptish
+( https://github.com/scriptish/scriptish ), reused under the terms of the MIT
+license.
+
Greasemonkey also contains code derived from Mozilla projects, including
-Firefox ( http://mozilla.org/ ). This code is reused under the MPL license;
-see the LICENSE.mpl file. All such code is located in the content/third-party/
-directory, and all such files contain the appropriate licensing disclaimers
+Firefox ( http://mozilla.org/ ), reused under the MPL license; see the
+LICENSE.mpl file.
+
+All MPL code is located in the content/third-party/ and modules/third-party/
+directories, and all such files contain the appropriate licensing disclaimers
and notifications.
@@ -1 +1,5 @@
extensions.{e4a8a97b-f2ed-450b-b12d-ee082ba24781}.description=A User Script Manager for Firefox
+error.matchPattern.parse=@match: Could not parse the pattern.
+error.matchPattern.scheme=@match: Invalid scheme specified.
+error.matchPattern.host=@match: Invalid host specified.
+error.matchPattern.path=@match: Invalid path specified.
@@ -0,0 +1,124 @@
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is Page Modifications code.
+ *
+ * The Initial Developer of the Original Code is Mozilla.
+ * Portions created by the Initial Developer are Copyright (C) 2009
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ * David Dahl <ddahl@mozilla.com>
+ * Drew Willcoxon <adw@mozilla.com>
+ * Erik Vold <erikvvold@gmail.com>
+ * Nils Maier <maierman@web.de>
+ * Anthony Lieuallen <arantius@gmail.com>
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+var EXPORTED_SYMBOLS = ['MatchPattern'];
+
+Components.utils.import("resource://gre/modules/NetUtil.jsm");
+Components.utils.import("resource://greasemonkey/third-party/convert2RegExp.js");
+Components.utils.import("resource://greasemonkey/utils.js");
+
+var validSchemes = ['http', 'https', 'ftp', 'file'];
+var REG_HOST = /^(?:\*\.)?[^*\/]+$|^\*$|^$/;
+var getString = (function() {
+ var stringBundleService = Components.classes["@mozilla.org/intl/stringbundle;1"]
+ .getService(Components.interfaces.nsIStringBundleService);
+ return stringBundleService.createBundle(
+ 'chrome://greasemonkey/locale/greasemonkey.properties')
+ .GetStringFromName;
+})();
+
+// For the format of "pattern", see:
+// http://code.google.com/chrome/extensions/match_patterns.html
+function MatchPattern(pattern) {
+ this._pattern = pattern;
+
+ // Special case "<all_urls>".
+ if (pattern == "<all_urls>") {
+ this._all = true;
+ this._scheme = "all_urls";
+ return;
+ }
+
+ // Special case wild scheme.
+ if (pattern[0] == "*") {
+ this._wildScheme = true;
+ // Forge http, to satisfy the URI parser, and get a host.
+ pattern = "http" + pattern.slice(1);
+ }
+
+ var uri = GM_uriFromUrl(pattern);
+ if (!uri) {
+ throw new Error(getString("error.matchPattern.parse"));
+ }
+
+ var scheme = this._wildScheme ? "all" : uri.scheme;
+ if (scheme != "all" && validSchemes.indexOf(scheme) == -1) {
+ throw new Error(getString("error.matchPattern.scheme"));
+ }
+
+ var host = uri.host;
+ if (!REG_HOST.test(host)) {
+ throw new Error(getString("error.matchPattern.host"));
+ }
+
+ var path = uri.path;
+ if (path[0] !== "/") {
+ throw new Error(getString("error.matchPattern.path"));
+ }
+
+ this._scheme = scheme;
+ if (host) {
+ this._hostExpr = GM_convert2RegExp(host.replace(/^\*\./, "*"));
+ } else {
+ // If omitted, then it means "", an alias for localhost.
+ this._hostExpr = /^$/;
+ }
+ this._pathExpr = GM_convert2RegExp(path, false, true);
+}
+
+MatchPattern.prototype.__defineGetter__('pattern',
+function MatchPattern_getPattern() { return '' + this._pattern; });
+
+MatchPattern.prototype.doMatch = function(uriSpec) {
+ var matchURI = GM_uriFromUrl(uriSpec);
+
+ if (validSchemes.indexOf(matchURI.scheme) == -1) {
+ return false;
+ }
+
+ if (this._all) {
+ return true;
+ }
+ if (!this._wildScheme && this._scheme != matchURI.scheme) {
+ return false;
+ }
+ return this._hostExpr.test(matchURI.host)
+ && this._pathExpr.test(matchURI.path);
+};

0 comments on commit a2bfded

Please sign in to comment.