-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
156 lines (125 loc) · 4.45 KB
/
script.js
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
class GoLocal {
/** @type {HTMLDialogElement} */
#dialog;
/** @type {URL} */
#targetURL;
/**
*
* @param {Event} event
*/
constructor(event) {
// decide if a dialog should be shown
if (localStorage.getItem('golocal')) {
// continue without dialog
return;
}
// still here? continue with dialog
this.#targetURL = event.target.href;
event.preventDefault();
// create HTML and attach to document
this.#dialog = this.createDialog();
const parent = document.body.querySelector('.dokuwiki') || document.body;
parent.appendChild(this.#dialog);
this.#dialog.showModal();
}
/**
* Create the dialog HTML and attach event listeners
*
* @returns {HTMLDialogElement}
*/
createDialog() {
const dialog = document.createElement('dialog');
dialog.className = 'golocal-dialog';
const main = document.createElement('main');
dialog.appendChild(main);
const intro = document.createElement('div');
intro.innerHTML = LANG.plugins.golocal.dialog_intro;
main.appendChild(intro);
const url = new URL(window.location.href);
url.searchParams.set('do', 'golocal');
const install = document.createElement('a');
install.href = url.toString();
install.className = 'install';
install.textContent = LANG.plugins.golocal.dialog_install;
main.appendChild(install);
const footer = document.createElement('footer');
dialog.appendChild(footer);
const rememberLabel = document.createElement('label');
const rememberCheckbox = document.createElement('input');
rememberCheckbox.type = 'checkbox';
rememberLabel.appendChild(rememberCheckbox);
const rememberSpan = document.createElement('span');
rememberSpan.textContent = LANG.plugins.golocal.dialog_remember;
rememberLabel.appendChild(rememberSpan);
footer.appendChild(rememberLabel);
const continueButton = document.createElement('button');
continueButton.textContent = LANG.plugins.golocal.dialog_continue;
continueButton.addEventListener('click', (ev) => this.onContinue(ev, rememberCheckbox));
continueButton.autofocus = true;
footer.appendChild(continueButton);
dialog.addEventListener('close', this.onClose.bind(this));
return dialog;
}
/**
* Continue to the target URL
* @param {Event} ev The click event
* @param {HTMLInputElement} rememberCheckbox The checkbox to remember the choice (if available)
*/
onContinue(ev, rememberCheckbox = null) {
if (rememberCheckbox && rememberCheckbox.checked) {
localStorage.setItem('golocal', 'true');
}
this.#dialog.close();
window.location.href = this.#targetURL;
}
onClose(e) {
this.#dialog.remove();
}
}
class GoLocalArch {
constructor() {
const ul = document.querySelector('.golocal-download');
if (!ul) {
return;
}
const os = this.getOs();
ul.querySelectorAll(`div.li.os-${os}`).forEach(li => {
li.classList.add('active');
});
}
/**
* Get the OS of the user
*
* @link https://tecadmin.net/javascript-detect-os
*/
getOs() {
const userAgent = window.navigator.userAgent;
const platform = window.navigator.platform;
const macosPlatforms = ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K', 'darwin'];
const windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'];
const iosPlatforms = ['iPhone', 'iPad', 'iPod'];
let os = null;
if (macosPlatforms.indexOf(platform) !== -1) {
os = 'macos';
} else if (iosPlatforms.indexOf(platform) !== -1) {
os = 'ios';
} else if (windowsPlatforms.indexOf(platform) !== -1) {
os = 'windows';
} else if (/Android/.test(userAgent)) {
os = 'android';
} else if (/Linux/.test(platform)) {
os = 'linux';
}
return os;
}
}
jQuery(function () {
jQuery('a.windows').each(function () {
// fix up all standard windows share links
let $this = jQuery(this);
$this.attr('href', $this.attr('href').replace('file:///', 'golocal://'));
}).click(function (e) {
new GoLocal(e);
});
new GoLocalArch();
});