forked from ImranR98/apps.obtainium.imranr.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
171 lines (155 loc) · 6.81 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
let data = null
const langCode = ((navigator.language || navigator.userLanguage) || 'en-US').split('-')[0]
function getString(key){
return getLocalString(data.strings[key]);
}
function getCategoriesSelectorHTML(categories, selectedCategories) {
let selectHTML = `
<select id="catSelect" class="select is-fullwidth" style="min-height: 6em;" multiple>`
for (const key in categories) {
const category = categories[key]
const displayName = getLocalString(category) || key
const isSelected = selectedCategories.includes(key)
selectHTML += `<option value="${key}" ${isSelected ? 'selected' : ''}>${displayName}</option>`
}
selectHTML += `</select>`
const buttonHTML = `<a class="button is-fullwidth is-primary" style="height: 100%;" href="javascript:void(0);" onclick="reloadWithSelected()">${getString('go')}</a>`
const searchHTML = `<input placeholder="${getString('search')}" type="search" class="input is-fullwidth" oninput="search(event)">`
const html = `<div class="container">
<label for="catSelect" class="label">${getString('categorySelect')}:</label>
<div class="columns">
<div class="column">
<div class="field is-grouped">
<div class="control is-expanded">
${selectHTML}
</div>
<div class="control">
${buttonHTML}
</div>
</div>
<div class="field">
<div class="control">
${searchHTML}
</div>
</div>
</div>
</div>
</div>`
return html
}
function search(event){
const regex = new RegExp(event.target.value,'ims');
document.querySelectorAll('#apps > *').forEach((element,appIndex) => {
const app = data.selectedApps[appIndex];
element.style.display = regex.test([
app.config.id,
app.config.name,
Object.values(app.description||{}).join('\n')
].join('\n'))?'':'none';
});
}
function reloadWithSelected() {
var selectElement = document.querySelector('#catSelect')
var selectedValues = Array.from(selectElement.selectedOptions).map(option => option.value).join(',')
window.location.href = `?categories=${selectedValues}`;
}
function getIconHTML(url, name) {
const placeholderImage = "https://raw.githubusercontent.com/ImranR98/Obtainium/main/assets/graphics/icon_small.png"
const placeholderStyle = "transform: rotate(0.31rad); opacity: 0.3;"
const src = url ? url : placeholderImage
const style = url ? '' : placeholderStyle
return url ? `<img src="${src}" alt="${name || 'App'} Icon" style="max-width: 0.9em; max-height: 0.9em; border-radius: 5px; ${style}">` : '<div></div>'
}
function getLocalString(langObject){
return langObject ? (langObject[langCode] || langObject.en||'') : '';
}
function getAppConfigString(appJson){
const config = appJson.config;
const description = getLocalString(appJson.description);
if(description){
const settings = JSON.parse(config.additionalSettings);
if(!settings.about) settings.about = description;
config.additionalSettings = JSON.stringify(settings);
}
return JSON.stringify(config);
}
function copyToClipboard(text) {
navigator.clipboard.writeText(text).then(() => {
alert('Copied!')
}).catch(err => {
console.error(err)
})
}
function copyAppToClipboard(appIndex) {
if (data) {
let app = data.selectedApps[appIndex]
if (app) {
copyToClipboard(getAppConfigString(app))
}
}
}
function getAppEntryHTML(appJson, appIndex, allCategories) {
const description = getLocalString(appJson.description);
const appCats = appJson.categories.map(category =>
`<a href="?categories=${encodeURIComponent(category)}" style="text-decoration: underline;">${getLocalString(allCategories[category])}</a>`).join(', ')
return `<div class="card mt-4">
<div class="card-content">
<p class="title is-flex is-justify-content-space-between">
<a href="${appJson.config.url}" style="text-decoration: underline; color: inherit;">${appJson.config.name}</a>
${getIconHTML(appJson.icon, appJson.config.name)}
</p>
<p class="subtitle">${description}</p>
<a class="button is-primary" href="obtainium://app/${encodeURIComponent(getAppConfigString(appJson))}">
${getString('addToObtainium')}
</a>
<a class="button is-secondary" href="javascript:void(0);" onclick="copyAppToClipboard('${appIndex}')">
${getString('copyAppConfig')}
</a>
<p class="is-size-7 mt-4" style="color: #555;">${getString('categories')}: ${appCats}</p>
</div>
</div>`
}
function getAppEntriesHTML(appsJson, allCategories, selectedCategories) {
appsJson = appsJson.map(app => {
app.categories = app.categories || []
app.categories = app.categories.filter(c => Object.keys(allCategories).indexOf(c) >= 0) // Ignore any non-existent categories
if (app.categories.length == 0) {
app.categories = ['other'] // Use the default if no cats were specified
}
return app
}).filter(app =>
app.categories.some(item => selectedCategories.includes(item))
)
data.selectedApps = appsJson;
if (appsJson.length > 0) {
return appsJson.map((appJson,appIndex) => getAppEntryHTML(appJson, appIndex, allCategories)).join('\n')
} else {
return '<strong>No Apps Found!</strong>'
}
}
function render() {
let selectedCategories = ((new URLSearchParams(window.location.search)).get('categories') || '').split(',').filter(c => c.trim())
if (!selectedCategories || selectedCategories.length == 0) {
selectedCategories = Object.keys(data.categories)
}
document.querySelector('#categories').innerHTML = getCategoriesSelectorHTML(data.categories, selectedCategories)
document.querySelector('#apps').innerHTML = getAppEntriesHTML(data.apps, data.categories, selectedCategories)
document.querySelector('#title').innerHTML = getString('title')
document.querySelector('#subtitle').innerHTML = getString('subtitle')
}
async function fetchAsync(url) {
let response = await fetch(url)
let data = await response.json()
return data
}
window.addEventListener('load', () => {
fetchAsync(`/data.json?rand=${Math.random() * 10000}`) // Prevent caching of this file
.then((d) => {
data = d
render()
}).catch((err) => {
console.error(err)
alert('Error! See console for details.')
})
})
// For local testing: python -m http.server 8080