Browse Source

fix: ignore query string and hash for match patterns

close #426
Gerald 7 years ago
parent
commit
a342714815
2 changed files with 19 additions and 2 deletions
  1. 4 2
      src/background/utils/tester.js
  2. 15 0
      test/background/tester.js

+ 4 - 2
src/background/utils/tester.js

@@ -53,14 +53,16 @@ export function testScript(url, script) {
   const inc = mergeLists(custom.origInclude && meta.include, custom.include);
   const exc = mergeLists(custom.origExclude && meta.exclude, custom.exclude);
   const excMat = mergeLists(custom.origExcludeMatch && meta.excludeMatch, custom.excludeMatch);
+  // Match patterns works only on scheme, host and path.
+  const baseUrl = url.split('#')[0].split('?')[0];
   // match all if no @match or @include rule
   let ok = !mat.length && !inc.length;
   // @match
-  ok = ok || testMatch(url, mat);
+  ok = ok || testMatch(baseUrl, mat);
   // @include
   ok = ok || testGlob(url, inc);
   // @exclude-match
-  ok = ok && !testMatch(url, excMat);
+  ok = ok && !testMatch(baseUrl, excMat);
   // @exclude
   ok = ok && !testGlob(url, exc);
   return ok;

+ 15 - 0
test/background/tester.js

@@ -110,6 +110,21 @@ test('path', t => {
     q.end();
   });
 
+  t.test('should ignore query string and hash', q => {
+    const script = buildScript({
+      meta: {
+        match: [
+          'https://www.google.com/a',
+        ],
+      },
+    });
+    q.ok(testScript('https://www.google.com/a', script), 'should match without query and hash');
+    q.ok(testScript('https://www.google.com/a#hash', script), 'should match with hash');
+    q.ok(testScript('https://www.google.com/a?query', script), 'should match with query');
+    q.ok(testScript('https://www.google.com/a?query#hash', script), 'should match with query and hash');
+    q.end();
+  });
+
   t.end();
 });