Biblioteka JavaScript
Użycie na stronie WWW
Prosta biblioteka JavaScript do integracji z API WindowsBASE.pl bezpośrednio na stronie internetowej. Aby rozpocząć integrację twojej strony internetowej z naszą biblioteką, wystarczy do twojej strony internetowej dodać odpowiedni skrypt. Pamiętaj, aby dodać go przed zamknięciem znacznika </body>.
Informacje o wersji:
- Wersja pakietu JavaScript: 1.3.0
- Ostatnia aktualizacja: 09.11.2025r.
Szybki start
Dodaj skrypt do swojej strony:
<script src="https://windowsbase.pl/api/data/apps/windowsbase-api.js"></script>
Przykłady użycia biblioteki JS
Wyszukiwanie pliku
// =============================================================================
// PRZYKŁAD 1: PROSTE WYSZUKIWANIE (nowe API)
// =============================================================================
async function simpleSearch() {
// Wyszukaj pliki zawierające "windows 10"
const results = await searchFile('windows 10');
console.log(`Znaleziono ${results.length} plików:`);
results.forEach(file => {
console.log(`- ${file.name}`);
console.log(` Link: ${file.downloadLink}`);
});
}
// =============================================================================
// PRZYKŁAD 2: WYSZUKIWANIE Z WIELOMA FRAZAMI
// =============================================================================
async function multipleTerms() {
// Wyszukaj pliki zawierające WSZYSTKIE podane frazy
const results = await searchFile(['windows', '10', 'pro', 'x64']);
// Lub używając string ze spacjami (automatycznie rozdzieli na frazy)
const results2 = await searchFile('windows 10 pro x64');
return results;
}
// =============================================================================
// PRZYKŁAD 3: WYSZUKIWANIE Z LIMITEM
// =============================================================================
async function limitedSearch() {
// Zwróć maksymalnie 10 wyników
const top10 = await searchFile('office', { limit: 10 });
// Zwróć WSZYSTKIE wyniki (bez limitu)
const allResults = await searchFile('windows', { limit: 0 });
return { top10, allResults };
}
// =============================================================================
// PRZYKŁAD 4: FILTROWANIE WYNIKÓW
// =============================================================================
async function filterResults() {
// Wyszukaj wszystkie pliki Windows
const allWindows = await searchFile('windows', { limit: 0 });
// Filtruj tylko pliki ISO
const onlyISO = allWindows.filter(file =>
file.name.toLowerCase().endsWith('.iso')
);
// Filtruj tylko Windows 10
const onlyWin10 = allWindows.filter(file =>
file.name.toLowerCase().includes('10')
);
// Filtruj po rozmiarze nazwy (np. duże pliki mają długie nazwy)
const largeFiles = allWindows.filter(file =>
file.name.length > 50
);
return { onlyISO, onlyWin10, largeFiles };
}
// =============================================================================
// PRZYKŁAD 5: SORTOWANIE WYNIKÓW
// =============================================================================
async function sortResults() {
const results = await searchFile('office', { limit: 0 });
// Sortuj alfabetycznie
const alphabetical = [...results].sort((a, b) =>
a.name.localeCompare(b.name)
);
// Sortuj od Z do A
const reverse = [...results].sort((a, b) =>
b.name.localeCompare(a.name)
);
// Sortuj po długości nazwy
const byLength = [...results].sort((a, b) =>
a.name.length - b.name.length
);
return { alphabetical, reverse, byLength };
}
// =============================================================================
// PRZYKŁAD 6: STATYSTYKI WYSZUKIWANIA
// =============================================================================
async function searchStats() {
const query = 'windows';
const results = await searchFile(query, { limit: 0 });
// Zlicz typy plików
const extensions = {};
results.forEach(file => {
const ext = file.name.split('.').pop().toLowerCase();
extensions[ext] = (extensions[ext] || 0) + 1;
});
// Znajdź najdłuższą nazwę
const longest = results.reduce((a, b) =>
a.name.length > b.name.length ? a : b
);
// Znajdź najkrótszą nazwę
const shortest = results.reduce((a, b) =>
a.name.length < b.name.length ? a : b
);
console.log('=== STATYSTYKI WYSZUKIWANIA ===');
console.log('Zapytanie:', query);
console.log('Znaleziono plików:', results.length);
console.log('Typy plików:', extensions);
console.log('Najdłuższa nazwa:', longest.name);
console.log('Najkrótsza nazwa:', shortest.name);
return { extensions, longest, shortest };
}
Pobieranie plików z bazy
// =============================================================================
// PRZYKŁAD 7: POBIERANIE LINKU DO PLIKU
// =============================================================================
async function downloadLink() {
try {
const file = await downloadFile('en_windows_10_pro_10162_x86_dvd.iso');
console.log('Znaleziono plik:');
console.log('Nazwa:', file.name);
console.log('Link do strony pobierania:', file.downloadLink);
console.log('Link bezpośredni:', file.directUrl);
return file;
} catch (error) {
console.error('Nie znaleziono pliku:', error.message);
}
}
// =============================================================================
// PRZYKŁAD 8: PRZYCISK POBIERANIA (nowe API)
// =============================================================================
async function downloadButton(fileName) {
const button = document.querySelector(`button[data-file="${fileName}"]`);
try {
button.textContent = 'Przygotowuję...';
button.disabled = true;
// Globalna funkcja downloadFile()
const file = await downloadFile(fileName);
// Przekieruj na link pobierania
window.location.href = file.directUrl;
button.textContent = 'Pobierz';
button.disabled = false;
} catch (error) {
alert('Nie można pobrać pliku: ' + error.message);
button.textContent = 'Błąd!';
setTimeout(() => {
button.textContent = 'Pobierz';
button.disabled = false;
}, 2000);
}
}
// =============================================================================
Przykładowy HTML dla przycisku:
<button data-file="Windows_10_Pro.iso" onclick="downloadButton('en_windows_10_pro_10162_x86_dvd.iso')">Pobierz</button>
// Zastąp wartość funkcji downloadButton() szulaną nazwą pliku.
Przeglądanie bazy
// =============================================================================
// PRZYKŁAD 9: STATUS BAZY DANYCH
// =============================================================================
async function checkStatus() {
const status = await checkDBStatus();
console.log('=== STATUS SERWERÓW ===');
for (const [ip, server] of Object.entries(status.servers)) {
if (ip === 'error') continue;
console.log(`${ip}: ${server.status} (${server.latency}ms)`);
}
console.log('\n=== STATYSTYKI BAZY ===');
console.log('Wersja:', status.database.version);
console.log('Plików:', status.database.totalFiles);
console.log('Rozmiar:', status.database.totalSize);
console.log('Ostatnia aktualizacja:', status.database.lastUpdate);
console.log('\n=== OSTATNIE ZMIANY ===');
console.log('Dodane pliki:', status.changes.added);
console.log('Usunięte pliki:', status.changes.removed);
console.log('Zaktualizowane:', status.changes.updated);
return status;
}
Caching bazy danych
// =============================================================================
// PRZYKŁAD 10: ZARZĄDZANIE CACHE
// =============================================================================
async function cacheManagement() {
// Sprawdź info o cache
const info = getCacheInfo();
console.log('Cache info:', info);
// Sprawdź wiek cache w godzinach
const cacheAgeHours = info.cacheAge / (1000 * 60 * 60);
console.log(`Cache age: ${cacheAgeHours.toFixed(2)} godzin`);
// Jeśli cache jest stary (>12h), odśwież
if (cacheAgeHours > 12) {
console.log('Odświeżam cache...');
await refreshCache();
}
// Wyczyść cache (następne zapytanie pobierze świeże dane)
// clearCache();
// Preload cache przy starcie aplikacji
if (!info.hasCache) {
console.log('Ładuję cache...');
await refreshCache();
console.log('Cache załadowany!');
}
// Sprawdź ścieżkę do proxy
console.log('Proxy URL:', getProxyUrl());
}