@@ -22,50 +22,79 @@ function parseLocationQuery(s) {
2222 return params ;
2323}
2424
25- // identityMatchers is used to generate our declarative page match rules, but also
26- // used to check for matches at runtime. Unfortunately, these mechanisms use different
27- // implementations of regular expressions (re2 vs javascript's regexp) so there's
28- // some redundancy but at least it's all in one place? :D
25+ // identityMatchers is used to generate our declarative page match rules, but
26+ // also used to check for matches at runtime for each `service` that we
27+ // support. They have the following schema:
28+ // {
29+ // "service": Service name, if you add one here update `profileInject` in
30+ // `identities.js` and `User.prototype.href to fully register it.
31+ //
32+ // "getUsername": Function to parse the username from the browser
33+ // `location` object.
34+ //
35+ // "locationMatches": A regular expression used to match a page within the
36+ // service (i.e. news.ycombinator.com/user?id=username matches but not
37+ // news.ycombinator.com/newest).
38+ //
39+ // "originAndPathMatches": A re2 style regex used to match a page within
40+ // the service for declarativeContent matching
41+ // (https://developer.chrome.com/extensions/declarativeContent).
42+ //
43+ // "hostEquals": Used to match that the host is the host we want to run on
44+ // (preventing any regex trickery for `locationMatches` or
45+ // `originAndPathMatches`).
46+ //
47+ // "css": (optional) CSS selector which must be present for declarativeContent
48+ // or the chat button to be injected.
49+ //
50+ // }
51+ //
2952const identityMatchers = [
3053 {
3154 service : "keybase" ,
3255 getUsername : function ( loc ) { return loc . pathname . split ( '/' ) [ 1 ] ; } ,
33- locationMatches : new RegExp ( '\\.keybase\\.(?:io|pub)/([\\w]+)[/]?' ) ,
34- originAndPathMatches : '\\.keybase\\.(io|pub)/[\\w]+[/]?' ,
56+ locationMatches : new RegExp ( '\\.keybase\\.io/([\\w]+)[/]?' ) ,
57+ originAndPathMatches : 'keybase\\.io/[\\w]+[/]?' ,
58+ hostEquals : [ 'keybase.io' ] ,
3559 css : [ '.profile-heading' ]
3660 } ,
3761 {
3862 service : "reddit" ,
3963 getUsername : function ( loc ) { return loc . pathname . split ( '/' ) [ 2 ] ; } ,
40- locationMatches : new RegExp ( '\\.reddit.com/user/([\\w-]+)[/]?$' ) ,
41- originAndPathMatches : '\\.reddit.com/user/[\\w-]+[/]?$' ,
64+ locationMatches : new RegExp ( '\\.reddit\\.com/user/([\\w-]+)[/]?$' ) ,
65+ originAndPathMatches : '\\.reddit\\.com/user/[\\w-]+[/]?$' ,
66+ hostEquals : [ 'www.reddit.com' , 'reddit.com' ]
4267 } ,
4368 {
4469 service : "twitter" ,
4570 getUsername : function ( loc ) { return loc . pathname . split ( '/' ) [ 1 ] ; } ,
4671 locationMatches : new RegExp ( '\\.twitter\\.com/([\\w]+)[/]?$' ) ,
47- originAndPathMatches : '\\.twitter\\.com/[\\w]+[/]?$' ,
72+ originAndPathMatches : 'twitter\\.com/[\\w]+[/]?$' ,
73+ hostEquals : [ 'twitter.com' ] ,
4874 css : [ 'body.ProfilePage' ]
4975 } ,
5076 {
5177 service : "github" ,
5278 getUsername : function ( loc ) { return loc . pathname . split ( '/' ) [ 1 ] ; } ,
5379 locationMatches : new RegExp ( '\\.github\\.com/([\\w\-]+)[/]?$' ) ,
54- originAndPathMatches : '\\.github\\.com/[\\w\-]+[/]?$' ,
80+ originAndPathMatches : 'github\\.com/[\\w\-]+[/]?$' ,
81+ hostEquals : [ 'github.com' ] ,
5582 css : [ 'body.page-profile' ]
5683 } ,
5784 {
5885 service : "facebook" ,
5986 getUsername : function ( loc ) { return loc . pathname . split ( '/' ) [ 1 ] ; } ,
6087 locationMatches : new RegExp ( '\\.facebook\\.com/([\\w\\.]+)[/]?$' ) ,
6188 originAndPathMatches : '\\.facebook\\.com/[\\w\\.]+[/]?$' ,
89+ hostEquals : [ 'facebook.com' , 'www.facebook.com' ] ,
6290 css : [ 'body.timelineLayout' ]
6391 } ,
6492 {
6593 service : "hackernews" ,
6694 getUsername : function ( loc ) { return parseLocationQuery ( loc . search ) [ "id" ] ; } ,
6795 locationMatches : new RegExp ( 'news\\.ycombinator\\.com/user' ) ,
6896 originAndPathMatches : 'news\\.ycombinator\\.com/user' ,
97+ hostEquals : [ 'news.ycombinator.com' ] ,
6998 css : [ 'html[op="user"]' ]
7099 }
71100] ;
@@ -80,7 +109,9 @@ function matchService(loc, doc, forceService) {
80109 for ( const m of identityMatchers ) {
81110 if ( forceService !== undefined && forceService !== m . service ) continue ;
82111
83- const matched = url . match ( m . locationMatches ) ;
112+ const matched = m . hostEquals . some ( function ( hostName ) {
113+ return hostName === loc . hostname
114+ } ) && url . match ( m . locationMatches ) ;
84115 if ( ! matched ) continue ;
85116
86117 const username = safeHTML ( m . getUsername ( loc ) ) ;
0 commit comments