add support for custom quote apis in quote packs

This commit is contained in:
David Ralph
2020-09-17 11:47:46 +01:00
parent 9db5150b07
commit cec368ab5f
3 changed files with 24 additions and 1 deletions

View File

@@ -83,6 +83,7 @@ export default class Marketplace extends React.PureComponent {
icon: info.data.screenshot_url
}
});
document.getElementById('marketplace').style.display = 'none';
document.getElementById('seemore').style.display = 'none';
document.getElementById('item').style.display = 'block';
@@ -142,6 +143,7 @@ export default class Marketplace extends React.PureComponent {
installStuff();
break;
case 'quote_packs':
if (this.state.current_data.content.data.quoteapi) localStorage.setItem('quote_api', JSON.stringify(this.state.current_data.content.data.quoteapi));
localStorage.setItem('quote_packs', JSON.stringify(this.state.current_data.content.data.quotes));
installStuff();
break;

View File

@@ -21,7 +21,7 @@ export default class Quote extends React.PureComponent {
}); // Set the quote
}
async getQuote() {
getQuotePack() {
const quotePack = JSON.parse(localStorage.getItem('quote_packs'));
if (quotePack) {
const data = quotePack[Math.floor(Math.random() * quotePack.length)];
@@ -29,6 +29,23 @@ export default class Quote extends React.PureComponent {
quote: '"' + data.quote + '"',
author: data.author
});
} else this.doOffline();
}
async getQuote() {
const quotePackAPI = JSON.parse(localStorage.getItem('quote_api'));
if (quotePackAPI) {
try {
const data = await (await fetch(quotePackAPI.url)).json();
let author = data[quotePackAPI.author];
if (quotePackAPI.authorOverride) author = quotePackAPI.authorOverride;
return this.setState({
quote: '"' + data[quotePackAPI.quote] + '"',
author: author
});
} catch (e) {
return this.getQuotePack();
}
}
if (localStorage.getItem('offlineMode') === 'true') return this.doOffline();

View File

@@ -26,6 +26,10 @@ export default class MarketplaceFunctions {
oldSettings.forEach(item => localStorage.setItem(item.name, item.value));
uninstallStuff();
break;
case 'quote_pack':
localStorage.removeItem('quote_packs');
localStorage.removeItem('quote_api');
break;
default:
try {
localStorage.removeItem(type);