Repository navigation
Expand file tree
/
Copy pathidentities.js
More file actions
195 lines (182 loc) · 6.18 KB
/
Copy pathidentities.js
File metadata and controls
195 lines (182 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// All of our identity services and matchers are defined here.
// identityMatchers is used to generate our declarative page match rules, but
// also used to check for matches at runtime for each `service` that we
// support. They have the following schema:
// {
// "service": Service name, if you add one here update `profileInject` in
// `identities.js` and `User.prototype.href to fully register it.
//
// "getUsername": Function to parse the username from the browser
// `location` object.
//
// "pathMatches": A regular expression used to match the pathname within the
// service (i.e. news.ycombinator.com/user?id=username matches but not
// news.ycombinator.com/newest).
//
// "originAndPathMatches": A re2 style regex used to match a page within
// the service for declarativeContent matching
// (https://developer.chrome.com/extensions/declarativeContent).
//
// "subdomains": Subdomains that the host is considered valid on.
//
// "host": Used to match that the host is the host we want to run on
// (preventing any regex trickery for `pathMatches` or
// `originAndPathMatches`).
//
// "css": (optional) CSS selector which must be present for declarativeContent
// or the chat button to be injected.
//
// }
//
const identityMatchers = [
{
service: "keybase",
getUsername: function(loc) { return loc.pathname.split('/')[1]; },
pathMatches: new RegExp('^/([\\w]+)[/]?$'),
originAndPathMatches: '^https://keybase\\.io/[\\w]+[/]?$',
subdomains: [],
host: 'keybase.io',
css: ['.profile-heading']
},
{
service: "reddit",
getUsername: function(loc) { return loc.pathname.split('/')[2]; },
pathMatches: new RegExp('^/user/([\\w-]+)[/]?$'),
originAndPathMatches: '^https://[\\w.-]*?\\.reddit\\.com/user/[\\w-]+[/]?$',
subdomains: ['np', 'ssl', 'blog', 'fr', 'pay', 'es', 'en-us', 'en', 'ru',
'us', 'de', 'dd', 'no', 'pt', 'ww', 'ss', '4x', 'sv', 'nl', 'hw', 'hr',
'www'],
host: 'reddit.com'
},
{
service: "twitter",
getUsername: function(loc) { return loc.pathname.split('/')[1]; },
pathMatches: new RegExp('^/([\\w]+)[/]?$'),
originAndPathMatches: '^https://twitter\\.com/[\\w]+[/]?$',
subdomains: [],
host: 'twitter.com',
css: ['body.ProfilePage']
},
{
service: "github",
getUsername: function(loc) { return loc.pathname.split('/')[1]; },
pathMatches: new RegExp('^/([\\w\-]+)[/]?$'),
originAndPathMatches: '^https://github\\.com/[\\w\-]+[/]?$',
subdomains: [],
host: 'github.com',
css: ['body.page-profile']
},
{
service: "facebook",
getUsername: function(loc) { return loc.pathname.split('/')[1]; },
pathMatches: new RegExp('^/([\\w\\.]+)[/]?$'),
originAndPathMatches: '^https://(www)?\\.facebook\\.com/[\\w\\.]+[/]?$',
subdomains: ['www'],
host: 'facebook.com',
css: ['body.timelineLayout']
},
{
service: "hackernews",
getUsername: function(loc) { return document.querySelector('.hnuser').text; },
pathMatches: new RegExp('^/user'),
originAndPathMatches: '^https://news\\.ycombinator\\.com/user',
subdomains: [],
host: 'news.ycombinator.com',
css: ['html[op="user"]']
}
];
function getServiceHosts(service) {
hosts = [service.host]
for (const subdomain of service.subdomains) {
hosts.push(subdomain + '.' + service.host)
}
return hosts
}
// Match a window.location and document against a service profile and return
// a User instance. Will skip matching CSS if no document is provided.
function matchService(loc, doc, forceService) {
for (const m of identityMatchers) {
if (forceService !== undefined && forceService !== m.service) continue;
const matched = getServiceHosts(m).some(function(hostName) {
return hostName === loc.hostname
}) && loc.pathname.match(m.pathMatches);
if (!matched) continue;
const username = safeHTML(m.getUsername(loc));
if (!username) continue;
if (doc === undefined || m.css === undefined) return new User(username, m.service);
for (const css of m.css) {
if (doc.querySelector(css) !== null) {
return new User(username, m.service);
}
}
}
}
// User keeps track of the original query and which services we resolved for
// this user. It also handles formatting strings for each service.
function User(username, service) {
if (service === undefined) service = "keybase";
this.origin = service;
this.services = {};
this.services[service] = username;
this.extraReplyCls = "";
}
// Return a fresh copy equivalent to how it was initialized.
User.prototype.clone = function() {
user = new User(this.services[this.origin], this.origin);
user.extraReplyCls = this.extraReplyCls
return user
}
User.prototype.query = function() {
const name = this.services[this.origin];
if (this.origin === "keybase") {
return name;
}
return `${name}@${this.origin}`;
}
User.prototype.display = function(service) {
if (service === undefined) service = this.origin;
const name = this.services[this.origin];
switch (this.origin) {
case "reddit":
return `/u/${name}`;
case "twitter":
return `@${name}`;
default:
return name;
}
}
User.prototype.href = function(service) {
if (service === undefined) service = this.origin;
const name = this.services[this.origin];
switch (service) {
case "keybase":
return `https://keybase.io/${name}`;
case "reddit":
return `https://www.reddit.com/user/${name}`;
case "twitter":
return `https://twitter.com/${name}`;
case "facebook":
return `https://facebook.com/${name}`;
case "github":
return `https://github.com/${name}`;
case "hackernews":
return `https://news.ycombinator.com/user?id=${name}`;
default:
throw `unknown service: ${this.origin}`;
}
}
// Convert a user input into a string that is safe for inlining into HTML.
function safeHTML(s) {
if (!s) return "";
return s.replace(/[&'"<>\/]/g, function (c) {
// Per https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet#RULE_.231_-_HTML_Escape_Before_Inserting_Untrusted_Data_into_HTML_Element_Content
return {
'&': "&",
'"': """,
"'": "'",
'/': "/",
'<': "<",
'>': ">"
}[c];
});
}