forked from shoaib-jamal/rebornxp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappstore.js
173 lines (159 loc) · 5.56 KB
/
appstore.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
var installedApps = {};
function updateInstalledApps() {
xp.filesystem.listDir('/Program Files', (name) => {
if (name.charAt(name.length - 1) !== '/') {
updateInstalledApp(xp.filesystem.basename(name));
}
});
}
$(window).on('xpboot', () => {
updateInstalledApps();
});
function updateInstalledApp(name) {
var appName = name.toLowerCase().replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"").replace(/\s{2,}/g," ").replace(/ /g,"-");
xp.applications.add(appName, (args) => {
if (args == undefined || args.length === 0)
args = ['/Program Files', name + '.js'];
loadApp(name, args);
});
xp.startmenu.add(appName, name, 'https://xpstore.glitch.me/appicon?app=' + name);
}
function loadApp(appName, args) {
xp.filesystem.readFile('/Program Files/' + appName + '.js', (text) => {
args = args || [];
eval(text)
});
}
function installApp(name) {
$.ajax({
url: '//xpstore.glitch.me/appcode?app=' + encodeURIComponent(name),
async: true,
success: (text) => {
xp.filesystem.writeFile('/Program Files/' + name + '.js', new Blob([text], {type: 'text/plain'}), (e) => {
if (e) {
xp.dialog('Error', e);
} else {
updateInstalledApp(name);
setTimeout(() => {
$('.appstore_iframe').each(function() {
this.contentWindow.postMessage('reload', '*');
setTimeout(() => this.contentWindow.postMessage('native', '*'), 2000);
})
}, 1000);
}
});
}
});
}
function removeApp(name, callback) {
xp.dialog('Confirm', 'Are you sure you want to uninstall ' + name + '?', () => {
xp.filesystem.deleteFile(xp.filesystem.addPaths('/Program Files', name + '.js'), (e) => {
if (e) {
xp.dialog('Error', e);
} else {
var appname = name.toLowerCase().replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"").replace(/\s{2,}/g," ").replace(/ /g,"-");
xp.applications.remove(appname);
xp.startmenu.remove(appname);
$('.appstore_iframe').each(function() {
this.contentWindow.postMessage('reload', '*');
setTimeout(() => this.contentWindow.postMessage('native', '*'), 2000);
});
if (callback !== undefined) callback();
}
});
}, true);
}
$(window).on('xpboot', () => {
xp.controlpanel.add('Add and Remove Programs', () => {
var el = $.parseHTML(`<window title="Add and Remove Programs" width="500" height="400">
<ul class="menu">
</ul>
</window>`);
document.body.append(el[0]);
$(el).updateWindow();
$(el).on('click', function() {
$(this).find('li').each(function() {
$(this).attr('data-selected', 'false');
$(this).find('div').css('display', 'none');
$(this).css('height', '18px');
});
});
function listDir(el) {
$(el).find('ul').html('');
xp.filesystem.listDir('/Program Files', (name) => {
if (name.charAt(name.length - 1) !== '/') {
var el2 = $.parseHTML(`<li class="menuitem" data-selected="false" style="padding-right:4px;">
` + xp.filesystem.basename(name) + `
<div style="display:none;"><button style="float:right;">Remove</button></div>
</li>`);
$(el2).on('click', function(e) {
e.stopPropagation();
e.preventDefault();
$(el).find('li').each(function() {
$(this).attr('data-selected', 'false');
$(this).find('div').css('display', 'none');
$(this).css('height', '18px');
});
$(this).attr('data-selected', 'true');
$(this).find('div').css('display', 'block');
$(this).css('height', '38px');
});
$(el2).find('button').on('click', function() {
removeApp(xp.filesystem.basename(name), () => listDir(el));
});
$(el).find('ul').append(el2);
}
});
}
listDir(el);
});
xp.applications.add('appstore', () => {
var el = $.parseHTML(`<window width="837" height="425" title="App Store">
<style>
iframe[seamless]{
background-color: transparent;
border: 0px none transparent;
padding: 0px;
overflow: hidden;
}
.frame-container {
position: absolute;
width: 100%;
height: 100%;
overflow: hidden;
padding: 0px;
margin: 0px;
}
</style>
<div class="frame-container">
<iframe height="100%" seamless="seamless" width="100%" src="//xpstore.glitch.me/" class="appstore_iframe"></iframe>
</div>
</window>`);
document.body.append(el[0]);
$(el).updateWindow();
$(el).find('iframe').on('load', function() {
this.contentWindow.postMessage('native', '*');
});
});
window.addEventListener('message', function(e) {
var key = e.message ? 'message' : 'data';
var data = e[key];
console.log(data);
if ((typeof data) === 'object' && data.length !== undefined && data.length === 2) {
if (data[0] === 'installApp') {
installApp(data[1]);
} else if (data[0] === 'removeApp') {
removeApp(data[1]);
} else if (data[0] === 'isInstalled' && data[1] != undefined) {
$('.appstore_iframe').each(function() {
xp.filesystem.listDir('/Program Files', (name) => {
if (xp.filesystem.basename(name).toLowerCase() === data[1].toLowerCase()) {
this.contentWindow.postMessage(['isInstalled', true], '*');
}
});
});
}
}
}, false);
xp.startmenu.add('appstore', 'App Store', '//cdn.glitch.com/c2046fda-a04d-4d3d-a188-6cbb40311aad%2FApp-Store-icon.png?1520720477340');
});