-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathURLProcessor.js
More file actions
71 lines (61 loc) · 1.99 KB
/
URLProcessor.js
File metadata and controls
71 lines (61 loc) · 1.99 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
const HOSTS = 'wikibooks|wikidata|wikimedia|wikinews|wikipedia|wikiquote|wikisource|wikiversity|wikivoyage|wiktionary';
const URL_REGEX = new RegExp(`^https?:\/\/([a-z]+)?\.(m\.)?(:?${HOSTS})\.org`);
class URLProcessor {
constructor (location) {
this.url = location;
this.doRedirect = true;
this.isDesktopURL = null;
this.isMobileURL = null;
let hostMatches = this.url.href.match(URL_REGEX);
this.domain = hostMatches[3];
this.mobileSubdomain = hostMatches[2];
this.subdomain = hostMatches[1];
}
canRedirect () {
let canRedirect = true;
if (this.subdomain == 'wikitech' || this.subdomain == 'www' || this.subdomain == 'upload') {
canRedirect = false;
}
if (this.domain == 'wikidata') {
canRedirect = true;
}
return canRedirect;
}
isDesktop () {
return (this.domain == 'wikidata')
? (this.subdomain == 'www')
: (typeof this.mobileSubdomain == 'undefined');
}
isMobile () {
return (this.domain == 'wikidata')
? (this.subdomain == 'm')
: (this.mobileSubdomain == 'm.');
}
getDesktop () {
let desktopURL = (this.domain == 'wikidata')
? `https://www.${this.domain}.org`
: `https://${this.subdomain}.${this.domain}.org`;
return this.url.href.replace(URL_REGEX, desktopURL);
}
getMobile () {
let mobileURL = (this.domain == 'wikidata')
? `https://m.${this.domain}.org`
: `https://${this.subdomain}.m.${this.domain}.org`;
return this.url.href.replace(URL_REGEX, mobileURL);
}
isEditing () {
return (this.isDesktop && /action=(?:edit|submit)/.test(this.url.search))
|| (this.isMobile && this.url.hash.includes('#/editor/'));
}
getDesktopEditingFromMobile () {
const HOSTNAME_REGEX = new RegExp(`^([a-z]+)?\.(?:(m)\.)?(${HOSTS})\.org`);
let title = (this.url.pathname == '/w/index.php')
? this.url.search.replace(/.*title=([^&]+).*/, "$1")
: this.url.pathname.replace(/\/w(iki)\//, '');
return 'https://' +
this.url.hostname.replace(HOSTNAME_REGEX, "$1.$3.org") +
'/w/index.php?' +
'title=' + title +
'&action=edit';
}
}