forked from greasemonkey/greasemonkey
-
Notifications
You must be signed in to change notification settings - Fork 1
/
install-dialog.js
124 lines (99 loc) · 3.76 KB
/
install-dialog.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
let gBtnInstall = document.getElementById('btn-install');
let gDetails = null;
let gInstallCountdown = 9;
let gProgressBar = document.querySelector('progress');
let gRvDetails = {
// Error message.
'errorHeader': '',
'errorList': [],
// Script details.
'excludes': [],
'grants': [],
'iconUrl': defaultIconUrl,
'includes': [],
'matches': [],
};
rivets.bind(document.body, gRvDetails);
let gUserScriptUrl = unescape(document.location.search.substr(1));
let gDownloader = new UserScriptDownloader().setScriptUrl(gUserScriptUrl);
/******************************* PROGRESS BAR ********************************/
gDownloader.addProgressListener(() => {
gProgressBar.value = gDownloader.progress;
});
/****************************** DETAIL DISPLAY *******************************/
gDownloader.scriptDetails.then(scriptDetails => {
gDetails = scriptDetails;
document.title = _('NAME_greasemonkey_user_script', gDetails.name);
// Apply the onerror event for the img tag. CSP does not allow it to be done
// directly in HTML.
let iconEl = document.querySelector('#install header img');
iconEl.onerror = () => { gRvDetails.iconUrl = defaultIconUrl; };
// Rivets will mutate its second parameter to have getters and setters,
// these will break our attempt to pass `gDetails` to background. So
// make a second copy of `gDetails`, for Rivets to own.
let rvDetails = JSON.parse(JSON.stringify(gDetails));
Object.assign(gRvDetails, rvDetails);
document.body.className = 'install';
});
/******************************* CANCEL BUTTON *******************************/
function finish() {
chrome.tabs.getCurrent(curTab => {
chrome.tabs.remove(curTab.id);
});
}
document.getElementById('btn-cancel').addEventListener('click', finish, true);
/****************************** INSTALL BUTTON *******************************/
function onClickInstall(event) {
gProgressBar.removeAttribute('value');
let disabled = document.getElementById('install-disabled').checked;
let openEditor = document.getElementById('open-editor-after').checked;
gDownloader.install(disabled, openEditor).then(finish).catch(err => {
gRvDetails.errorHeader = _('install_failed');
gRvDetails.errorList = [err.message];
document.body.className = 'error';
});
}
function maybeEnableInstall() {
if (gInstallCountdown) return;
if (gDownloader.progress < 1) return;
gBtnInstall.addEventListener('click', onClickInstall, true);
gBtnInstall.removeAttribute('disabled');
gBtnInstall.focus();
}
(() => {
let installCounter = document.createElement('span');
installCounter.textContent = ' ' + gInstallCountdown;
gBtnInstall.appendChild(installCounter);
let installTimer = setInterval(function() {
gInstallCountdown--;
if (gRvDetails.errorList.length > 0) {
clearInterval(installTimer);
installCounter.textContent = '';
gBtnInstall.setAttribute('disabled', '');
} else if (gInstallCountdown) {
installCounter.textContent = ' ' + gInstallCountdown;
} else {
clearInterval(installTimer);
maybeEnableInstall();
installCounter.parentNode.removeChild(installCounter);
}
}, 250);
})();
/*****************************************************************************/
gDownloader.start().then(() => {
gProgressBar.style.display = 'none';
maybeEnableInstall();
}).catch(e => {
gRvDetails.errorHeader = _('download_failed');
if (e instanceof DownloadError) {
gRvDetails.errorList = e.failedDownloads.map(
d => _('ERROR_at_URL', d.error, d.url));
} else if (e.message) {
gRvDetails.errorList = [e.message];
} else {
// Log the unknown error.
console.error('Unknown save error in install dialog', e);
gRvDetails.errorList = [_('download_error_unknown')];
}
document.body.className = 'error';
});