-
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix web_resources match patterns (#178)
* Fix web_resources match patterns
- Loading branch information
1 parent
d829630
commit 85d2bc0
Showing
3 changed files
with
67 additions
and
1 deletion.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
...ams/develop/webpack/plugin-extension/feature-web-resources/__spec__/clean-matches.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
import { cleanMatches } from "../clean-matches"; | ||
|
||
describe('cleanMatches', () => { | ||
it('does not handle non-urls', () => { | ||
expect(cleanMatches(['<all_urls>'])).toEqual(['<all_urls>']); | ||
}); | ||
|
||
it('handles wildcards', () => { | ||
expect( | ||
cleanMatches([ | ||
'*://*/some/path', | ||
'https://*.google.com/foo*bar', | ||
]) | ||
).toEqual([ | ||
'*://*/*', | ||
'https://*.google.com/*', | ||
]); | ||
}); | ||
|
||
it('cleans up all types of pathnames', () => { | ||
expect( | ||
cleanMatches([ | ||
'https://example.com', | ||
'https://example.com/some/path/*', | ||
'https://example.com/some/path', | ||
]) | ||
).toEqual([ | ||
'https://example.com/*', | ||
'https://example.com/*', | ||
'https://example.com/*', | ||
]); | ||
}); | ||
}); |
29 changes: 29 additions & 0 deletions
29
programs/develop/webpack/plugin-extension/feature-web-resources/clean-matches.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
|
||
/** | ||
* From the docs at https://developer.chrome.com/docs/extensions/reference/manifest/web-accessible-resources#manifest_declaration | ||
* > Google Chrome emits an "Invalid match pattern" error if the pattern has a path other than '/*'. | ||
* | ||
* We need to ensure that paths are cleaned up from the matches to avoid this error. | ||
*/ | ||
export function cleanMatches(matches: string[]) { | ||
return matches.map(match => { | ||
try { | ||
const url = new URL( | ||
// Using a wildcard for the scheme (`*://`) is supported, but URL cannot parse it | ||
match.replace(/^\*:\/\//, 'https://') | ||
); | ||
|
||
// URL.pathname will return `/` even if the URL is just `https://example.com` | ||
if (match.endsWith(url.pathname)) { | ||
return `${match.substring(0, match.length - url.pathname.length)}/*`; | ||
} else if (url.pathname === '/') { | ||
return `${match}/*`; | ||
} | ||
|
||
return match; | ||
} catch { | ||
// Special cases like <all_urls> will fail the URL parse, but we can ignore them | ||
return match; | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters