-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: rating badges in search results (#6)
- Loading branch information
Showing
9 changed files
with
132 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import {createSpan, getGameID, getProtonDBRating, ProtonDBRating, ProtonDBSummary, upperCase} from "./utils/utils"; | ||
import {addStoreRatingBadge, addSystemRequirementsRating} from "./utils/store"; | ||
import './styles/steam.less' | ||
|
||
export function checkGamePage() { | ||
const gameId = getGameID(); | ||
|
||
getProtonDBRating(gameId).then(async (rating) => { | ||
addStoreRatingBadge(gameId, rating); | ||
addSystemRequirementsRating(gameId, rating); | ||
|
||
let native = false; | ||
for (const tab of document.getElementsByClassName('sysreq_tab')) { | ||
if (tab.getAttribute('data-os') === 'linux') native = true | ||
} | ||
if (native) addStoreRatingBadge(gameId, ProtonDBRating.NATIVE) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import {createSpan, getGameID, getProtonDBRating, ProtonDBSummary, upperCase} from "./utils/utils"; | ||
|
||
export function checkSearchPage() { | ||
const searchResults = document.getElementById('search_resultsRows'); | ||
const mutationObserver = new window.MutationObserver(async ()=>{ | ||
checkSearchResults() | ||
}); | ||
if (searchResults) { | ||
checkSearchResults() | ||
mutationObserver.observe(searchResults, {childList: true}); | ||
} | ||
} | ||
|
||
function checkSearchResults() { | ||
const games = document.getElementsByClassName('search_result_row') | ||
for (const game of games) { | ||
if (game.getElementsByClassName('tag').length > 0) { continue; } | ||
const name = game.getElementsByClassName('title')[0] | ||
const gameId = getGameID(game.getAttribute('href') || "10"); | ||
getProtonDBRating(gameId).then(async (rating) => { | ||
if (game.getElementsByClassName('tag').length > 0) { return; } | ||
name.appendChild(createSpan(upperCase(rating), ['tag', rating, 'search'])) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,15 @@ | ||
import {ProtonDBRating, ProtonDBSummary} from "./utils/utils"; | ||
import {addStoreRatingBadge, addSystemRequirementsRating} from "./utils/store"; | ||
import './styles/steam.less' | ||
import {checkGamePage} from "./game_page"; | ||
import {checkSearch} from "./store_search"; | ||
import {checkSearchPage} from "./search_page"; | ||
import {ProtonDBRating} from "./utils/utils"; | ||
|
||
function getGameID() { | ||
return window.location.href.substring("https://store.steampowered.com/app/".length).split('/')[0] | ||
} | ||
export const ratings: {[key: string]: ProtonDBRating} = {} | ||
|
||
const gameId = getGameID(); | ||
fetch(`https://www.protondb.com/api/v1/reports/summaries/${gameId}.json`).then(async (res) => { | ||
if (res.ok) { | ||
const json = await res.json() as ProtonDBSummary; | ||
addStoreRatingBadge(gameId, json.tier) | ||
addSystemRequirementsRating(gameId, json.tier) | ||
}else{ | ||
addStoreRatingBadge(gameId, ProtonDBRating.PENDING) | ||
addSystemRequirementsRating(gameId, ProtonDBRating.PENDING) | ||
} | ||
if (window.location.href.startsWith('https://store.steampowered.com/app')) { | ||
checkGamePage() | ||
} | ||
if (window.location.href.startsWith('https://store.steampowered.com/search')) { | ||
checkSearchPage() | ||
} | ||
|
||
let native = false; | ||
for (const tab of document.getElementsByClassName('sysreq_tab')) { | ||
if (tab.getAttribute('data-os') === 'linux') native = true | ||
} | ||
if (native) addStoreRatingBadge(gameId, ProtonDBRating.NATIVE) | ||
}) | ||
checkSearch() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import {createSpan, getGameID, getProtonDBRating, ProtonDBSummary, upperCase} from "./utils/utils"; | ||
|
||
export function checkSearch() { | ||
const searchResults = document.getElementById('search_suggestion_contents'); | ||
const mutationObserver = new window.MutationObserver(()=>{ | ||
const games = document.getElementsByClassName('match_app') | ||
for (const game of games) { | ||
const name = game.getElementsByClassName('match_name')[0] | ||
const gameId = getGameID(game.getAttribute('href') || "10"); | ||
getProtonDBRating(gameId).then(async (rating) => { | ||
name.appendChild(createSpan(upperCase(rating), ['tag', rating])) | ||
}) | ||
} | ||
}); | ||
if (searchResults) { | ||
mutationObserver.observe(searchResults, {childList: true}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,56 @@ | ||
import {ratings} from "../steam"; | ||
|
||
export enum ProtonDBRating { | ||
NATIVE = 'native', | ||
PLATINUM = 'platinum', | ||
GOLD = 'gold', | ||
SILVER = 'silver', | ||
BRONZE = 'bronze', | ||
BORKED = 'borked', | ||
PENDING = 'pending', | ||
UNKNOWN = 'unknown', | ||
} | ||
|
||
export interface ProtonDBSummary { | ||
tier: ProtonDBRating | ||
} | ||
|
||
export function createSpan(text: string) { | ||
export function createSpan(text: string, classes?: string[]) { | ||
const element = document.createElement("span"); | ||
|
||
if (classes) { | ||
for (const prop of classes) { | ||
element.classList.add(prop); | ||
} | ||
} | ||
|
||
element.appendChild(document.createTextNode(text)) | ||
return element | ||
} | ||
|
||
export function upperCase(text: string) { | ||
return text.charAt(0).toUpperCase() + text.substring(1) | ||
} | ||
|
||
export function getGameID(url?: string) { | ||
if (!url) url = window.location.href; | ||
return url.substring("https://store.steampowered.com/app/".length).split('/')[0] | ||
} | ||
|
||
export function getProtonDBRating(gameId: string) { | ||
return new Promise<ProtonDBRating>((resolve, reject) => { | ||
if (ratings[gameId]) { | ||
resolve(ratings[gameId]); | ||
}else{ | ||
fetch(`https://www.protondb.com/api/v1/reports/summaries/${gameId}.json`).then(async (res) => { | ||
if (res.ok) { | ||
const json = await res.json() as ProtonDBSummary; | ||
ratings[gameId] = json.tier; | ||
resolve(json.tier); | ||
}else{ | ||
ratings[gameId] = ProtonDBRating.UNKNOWN; | ||
resolve(ProtonDBRating.UNKNOWN) | ||
} | ||
}).catch(console.error); | ||
} | ||
}) | ||
} |