mirror of
https://github.com/the1812/Bilibili-Evolved.git
synced 2025-11-04 21:22:45 +08:00
Add Resource class
This commit is contained in:
parent
e0c142cbf4
commit
b64b4bfdb1
@ -52,10 +52,11 @@
|
||||
GM_setValue(key, newSettings[key]);
|
||||
}
|
||||
}
|
||||
function downloadText(url, done)
|
||||
function downloadText(url, load, error)
|
||||
{
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.addEventListener("load", () => done(xhr.responseText));
|
||||
xhr.addEventListener("load", () => load && load(xhr.responseText));
|
||||
xhr.addEventListener("error", () => error && error(xhr.responseText));
|
||||
xhr.open("GET", url);
|
||||
xhr.send();
|
||||
}
|
||||
@ -341,6 +342,45 @@
|
||||
return new ResourceType("unknown");
|
||||
}
|
||||
}
|
||||
class Resource
|
||||
{
|
||||
static get root()
|
||||
{
|
||||
return "https://raw.githubusercontent.com/the1812/Bilibili-Evolved/preview/";
|
||||
}
|
||||
get downloaded()
|
||||
{
|
||||
return this.text !== null;
|
||||
}
|
||||
constructor(key, url, dependencies)
|
||||
{
|
||||
this.key = key;
|
||||
this.url = this.root + url;
|
||||
this.dependencies = dependencies || [];
|
||||
this.text = null;
|
||||
}
|
||||
download()
|
||||
{
|
||||
return new Promise((resolve, reject) =>
|
||||
{
|
||||
if (this.download)
|
||||
{
|
||||
resolve(this.text);
|
||||
}
|
||||
else
|
||||
{
|
||||
Promise.all(this.dependencies.map(r => r.download())).then(() =>
|
||||
{
|
||||
downloadText(this.url, text =>
|
||||
{
|
||||
this.text = text;
|
||||
resolve(this.text);
|
||||
}, error => reject(error));
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
class ResourceManager
|
||||
{
|
||||
constructor()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user