rust-search-extension/extension/redirect.js
2020-06-03 23:50:30 +08:00

31 lines
No EOL
1.5 KiB
JavaScript

(async () => {
let crate = location.search.slice(1).split("=")[1];
try {
let response = await fetch(`https://crates.io/api/v1/crates/${crate}`, { mode: "cors" });
let loading = document.querySelector(".loading");
if (response.status === 200) {
let json = await response.json();
if (json.crate.repository) {
loading.innerHTML = `Obtained the repository url of <b>${crate}</b>.
<span style="vertical-align:sub"><img class="animate__animated animate__fadeIn" src="check.svg" style="padding-left:10px"/></span>`;
loading.insertAdjacentHTML("beforeend", '<h1 class="redirect animate__animated animate__fadeIn">Redirecting...</h1>');
location = json.crate.repository;
} else {
loading.innerHTML = `<div>
<p>Sorry, the crate <b>${crate}</b> has no repository url. <span style="vertical-align:sub"><img class="animate__animated animate__fadeIn" src="error.svg" style="padding-left:10px"/></span></p>
<h2 class="redirect">Go to <a href="https://crates.io/crates/${crate}">crates.io/crates/${crate}</a> or <a href="https://docs.rs/${crate}">docs.rs/${crate}</a></h2>
</div>`;
}
} else {
requestFailed();
}
} catch (e) {
requestFailed();
}
})();
function requestFailed() {
let loading = document.querySelector(".loading");
loading.innerHTML = `Request failed, please try again :(`;
}