-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearch.js
142 lines (124 loc) · 6.1 KB
/
search.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
async function searchGames() {
const searchInput = document.getElementById('searchInput').value.trim();
if (!searchInput) {
alert('Please enter a game name.');
return;
}
displayResult('Fetching game data', 'white', true);
//const url = `https://api.allorigins.win/raw?url=https://store.steampowered.com/search/results/?term=${encodeURIComponent(searchInput)}&count=10&l=english`;
const url = `https://13584595.xyz/raw?url=https://store.steampowered.com/search/results/?term=${encodeURIComponent(searchInput)}&count=10&l=english`;
try {
const html = await fetchWithRetries(url, 5000, 'text'); // Total of 10 seconds for all attempts
const doc = new DOMParser().parseFromString(html, 'text/html');
const games = doc.querySelectorAll('#search_resultsRows a');
const gameList = document.getElementById('gameList');
gameList.innerHTML = '';
if (games.length === 0) {
const suggestionElement = doc.querySelector('.search_results_spellcheck_suggestion');
const suggestion = suggestionElement ? suggestionElement.textContent.trim() : null;
const correctNameMatch = suggestionElement ? suggestionElement.innerHTML.match(/ReplaceTerm\( "(.+?)" \)/) : null;
const correctName = correctNameMatch ? correctNameMatch[1] : null;
if (suggestion) {
displayResult(`No results found. Did you mean "<a href="#" id="suggestedTerm" style="color: cyan;">${correctName}</a>"?`, 'red', false);
if (correctName) {
setTimeout(() => {
const suggestedTermElement = document.getElementById('suggestedTerm');
if (suggestedTermElement) {
suggestedTermElement.addEventListener('click', (event) => {
event.preventDefault();
correctNameSearch(correctName);
});
}
}, 100); // Delay to ensure the HTML is rendered
}
} else {
displayResult('No results found. Check if game name is spelled correctly.', 'red', false);
}
return;
}
games.forEach((game, index) => {
if (index < 10) { // Limit to first 10 search results
const titleElement = game.querySelector('.title');
if (!titleElement) return; // Skip if title element is not found
const name = titleElement.textContent.trim();
const appId = game.getAttribute('data-ds-appid');
const priceElement = game.querySelector('.discount_final_price');
const price = priceElement ? priceElement.textContent.trim() : 'Price not available';
const logoUrl = game.querySelector('.search_capsule img').src;
const gameItem = document.createElement('div');
gameItem.classList.add('gameItem');
gameItem.innerHTML = `
<div class="searchResult" style="display: flex; align-items: center; cursor: pointer;">
<img src="${logoUrl}" alt="${name}" style="width: 50px; height: auto; max-height: 50px; margin-right: 10px; object-fit: contain;">
<div>
<p>${name}</p>
<p style="font-size: smaller;">AppID: ${appId}</p>
</div>
<div style="margin-left: auto;">
<p style="font-size: smaller;">${price}</p>
<button onclick="selectGame('${appId}')">Check Family Share</button>
</div>
</div>
`;
gameList.appendChild(gameItem);
// displayResult('Game list found and displayed.', 'white', false);
clearDisplayResult();
gameItem.addEventListener('click', () => {
const buttonClicked = event.target.tagName.toLowerCase() === 'button';
if (!buttonClicked) {
searchFieldClicked(appId);
}
});
}
});
} catch (error) {
console.error('Error searching games:', error);
displayResult('An error occurred while fetching game data. Please reload the website and try to search for game again.', 'white', false); // Display error status
}
}
function searchFieldClicked(appId) {
const confirmation = confirm(`Do you want to check family share for the selected game with ID: ${appId}?`);
if (confirmation) {
selectGame(appId);
}
}
function selectGame(appId) {
const transformedAppId = `https://store.steampowered.com/app/${appId}/`;
document.getElementById('appLinkInput').value = transformedAppId;
checkFamilyShare();
clearSearchResults();
}
function clearSearchResults() {
const gameList = document.getElementById('gameList');
document.getElementById('searchInput').value = '';
gameList.innerHTML = '';
}
let intervalId; // Define intervalId outside the function
function displayResult(message, color, animate = false) {
const searchStatus = document.getElementById('searchStatus'); // Get search status element
searchStatus.textContent = message;
searchStatus.style.color = color;
// Clear any existing animation interval
clearInterval(intervalId);
if (animate) {
let dots = '';
intervalId = setInterval(() => {
dots += '.';
searchStatus.textContent = message + dots;
if (dots.length === 4) {
dots = '';
}
}, 500);
} else {
searchStatus.innerHTML = message; // Set innerHTML to display HTML content
}
}
function clearDisplayResult() {
displayResult();
const searchStatus = document.getElementById('searchStatus');
searchStatus.textContent = ''; // Clear the text content
}
function correctNameSearch(correctName) {
document.getElementById('searchInput').value = correctName;
searchGames();
}